This action will force synchronization from 编程语言算法集/Python, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
"""Program to list all the ways a target string can beconstructed from the given list of substrings"""from __future__ import annotationsdef all_construct(target: str, word_bank: list[str] | None = None) -> list[list[str]]:"""returns the list containing all the possiblecombinations a string(target) can be constructed fromthe given list of substrings(word_bank)>>> all_construct("hello", ["he", "l", "o"])[['he', 'l', 'l', 'o']]>>> all_construct("purple",["purp","p","ur","le","purpl"])[['purp', 'le'], ['p', 'ur', 'p', 'le']]"""word_bank = word_bank or []# create a tabletable_size: int = len(target) + 1table: list[list[list[str]]] = []for i in range(table_size):table.append([])# seed valuetable[0] = [[]] # because empty string has empty combination# iterate through the indicesfor i in range(table_size):# conditionif table[i] != []:for word in word_bank:# slice conditionif 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 outputfor 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"],))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。