LeetCode刷题日记2021-12-28/472. 连接词-字典树&深度优先遍历

题目描述

给你一个 不含重复 单词的字符串数组 words ,请你找出并返回 words 中的所有 连接词

连接词 定义为:一个完全由给定数组中的至少两个较短单词组成的字符串。

示例 1:

输入:words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
输出:["catsdogcats","dogcatsdog","ratcatdogcat"]
解释:"catsdogcats" 由 "cats", "dog" 和 "cats" 组成; 
     "dogcatsdog" 由 "dog", "cats" 和 "dog" 组成; 
     "ratcatdogcat" 由 "rat", "cat", "dog" 和 "cat" 组成。

示例 2:

输入:words = ["cat","dog","catdog"]
输出:["catdog"]

提示:

  • 1 <= words.length <= 104
  • 0 <= words[i].length <= 1000
  • words[i] 仅由小写字母组成
  • 0 <= sum(words[i].length) <= 105

题解思路

我们定义一个字典树 并将words中的单词按照长度从小到大排序 遍历

  • 如果该单词未出现在字典树中 我们将其加入到字典树中

  • 如果该单词出现在字典树中 且是组合词我们将其添加到ans中

如何判断是否该单词是否是一个组合词:

  • 遍历该单词的每一个字符
    • 如果遍历到某一个字符对应已有的单词结尾 则从下一个字符接着遍历
    • 如果遍历到某一个字符对应的不存在字典树 则退出遍历

题解代码

class Trie:
    def __init__(self):
        self.children=[None]*26
        self.isEnd=False
    def insert(self,word):
        node=self
        for w in word:
            w=ord(w)-ord('a')
            if not node.children[w]:
                node.children[w]=Trie()
            node=node.children[w]
        node.isEnd=True
    def dfs(self,word,start):
        if start==len(word):
            return True
        node=self
        for i in range(start,len(word)):
            node=node.children[ord(word[i])-ord('a')]
            if node is None:
                return False
            if node.isEnd and self.dfs(word,i+1):
                return True
        return False
class Solution:
    def findAllConcatenatedWordsInADict(self, words: List[str]) -> List[str]:
        ans=[]
        words.sort(key=len)
        root=Trie()
        for word in words:
            if word=='':
                continue
            if root.dfs(word,0):
                ans.append(word)
            else:
                root.insert(word)
        return ans
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值