尝试解决下面的问题复合词,无法找到返回 python 中单词的所有变体的逻辑..任何帮助将不胜感激

发布于 2025-01-12 02:59:26 字数 1778 浏览 2 评论 0原文

复合词问题,这是我在下面尝试过的代码,但这不起作用,它不会返回所有可能的对,它只返回一对单词 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文