網站首頁 編程語言 正文
python:實現從給定的子串列表返回包含所有可能的列表算法
from __future__ import annotations
def all_construct(target: str, word_bank: list[str] | None = None) -> list[list[str]]:
word_bank = word_bank or []
# create a table
table_size: int = len(target) + 1
table: list[list[list[str]]] = []
for i in range(table_size):
table.append([])
# seed value
table[0] = [[]] # because empty string has empty combination
# iterate through the indices
for i in range(table_size):
# condition
if table[i] != []:
for word in word_bank:
# slice condition
if target[i : i + len(word)] == word:
new_combinations: list[list[str]] = [
[word] + way for way in table[i]
]
# adds the word to every combination the current position holds
# now,push that combination to the table[i+len(word)]
table[i + len(word)] += new_combinations
# combinations are in reverse order so reverse for better output
for combination in table[len(target)]:
combination.reverse()
return table[len(target)]
if __name__ == "__main__":
print(all_construct("jwajalapa", ["jwa", "j", "w", "a", "la", "lapa"]))
print(all_construct("rajamati", ["s", "raj", "amat", "raja", "ma", "i", "t"]))
print(
all_construct(
"hexagonosaurus",
["h", "ex", "hex", "ag", "ago", "ru", "auru", "rus", "go", "no", "o", "s"],
)
)
原文鏈接:https://blog.csdn.net/it_xiangqiang/article/details/125919814
相關推薦
- 2022-07-14 關于C#?dynamic裝箱問題_C#教程
- 2022-11-26 詳解Python中的with語句和上下文管理器_python
- 2022-04-22 數組去重并找到所有重復的項的索引位置
- 2022-05-25 ASP.NET?Core?6.0對熱重載的支持實例詳解_實用技巧
- 2022-08-18 Go?Web編程添加服務器錯誤和訪問日志_Golang
- 2022-08-21 caffe的python接口繪制loss和accuracy曲線_python
- 2022-06-18 Redis實現單設備登錄的場景分析_Redis
- 2022-03-29 python自動化之re模塊詳解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支