尝试解决下面的问题复合词,无法找到返回 python 中单词的所有变体的逻辑..任何帮助将不胜感激
复合词问题,这是我在下面尝试过的代码,但这不起作用,它不会返回所有可能的对,它只返回一对单词 ex:
"verylongjump": ["very", "long", "jump"])
但不会返回两者:
"verylongjump": ["very", "long", "jump"]
"verylongjump": ["very","longjump"]
问题输入:
输入:
["long", "very", "jump", "verylongjump", "longjump",
"iconstar", "star", "icon", "icons", "tar", "stars,"iconstars"]
预期输出:
"verylongjump": ["very", "long", "jump"]
"verylongjump": ["very","longjump"]
"iconstar": ["icon",""star"]
"iconstar":["icons", "tar"]
"iconstars":["icon","stars"]
def concatenatedWords(words):
# Intitialize empty array for result set
result = []
# Helper function for Work Break II without DP but accepted by LeetCode
def helper(idx, word, wordSet, curr, result):
if idx == len(word):
result.append(curr)
return
if idx > len(word):
return
for i in range(idx+1, len(word)+1):
if word[idx:i] in wordSet:
helper(i, word, wordSet, curr + [word[idx:I]], result)
return
# Convert the list of words into a HashSet
wordSet = set(words)
# Find word breaks for each word
for word in words:
# Initialize current word split result set
curr = []
helper(0, word, wordSet, [], curr)
# Word Break solution captures the word themselves as they are present in the
# hash set; Filter them out by checking for length of current splits more than 1
if len(curr) > 1:
res = []
# Flatten the result list
for elem in curr:
res.extend(elem)
result.append(res)
return result
Compound words problem, Here's the code I have tried below, but this doesn't work, it won't return all the possible pairs, it only returns one pair of a word ex:
"verylongjump": ["very", "long", "jump"])
but it will not return both:
"verylongjump": ["very", "long", "jump"]
"verylongjump": ["very","longjump"]
Question Input:
Input:
["long", "very", "jump", "verylongjump", "longjump",
"iconstar", "star", "icon", "icons", "tar", "stars,"iconstars"]
Expected Output:
"verylongjump": ["very", "long", "jump"]
"verylongjump": ["very","longjump"]
"iconstar": ["icon",""star"]
"iconstar":["icons", "tar"]
"iconstars":["icon","stars"]
def concatenatedWords(words):
# Intitialize empty array for result set
result = []
# Helper function for Work Break II without DP but accepted by LeetCode
def helper(idx, word, wordSet, curr, result):
if idx == len(word):
result.append(curr)
return
if idx > len(word):
return
for i in range(idx+1, len(word)+1):
if word[idx:i] in wordSet:
helper(i, word, wordSet, curr + [word[idx:I]], result)
return
# Convert the list of words into a HashSet
wordSet = set(words)
# Find word breaks for each word
for word in words:
# Initialize current word split result set
curr = []
helper(0, word, wordSet, [], curr)
# Word Break solution captures the word themselves as they are present in the
# hash set; Filter them out by checking for length of current splits more than 1
if len(curr) > 1:
res = []
# Flatten the result list
for elem in curr:
res.extend(elem)
result.append(res)
return result
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论