给一个词典,找出其中所有最长的单词。

题目

描述
给一个词典,找出其中所有最长的单词。

您在真实的面试中是否遇到过这个题?
样例
在词典

{
“dog”,
“google”,
“facebook”,
“internationalization”,
“blabla”
}
中, 最长的单词集合为 [“internationalization”]

在词典

{
“like”,
“love”,
“hate”,
“yes”
}
中,最长的单词集合为 [“like”, “love”, “hate”]

挑战
遍历两次的办法很容易想到,如果只遍历一次你有没有什么好办法?

解答

只需要遍历一次

public class LongestWord {
    /*
     * @param dictionary: an array of strings
     * @return: an arraylist of strings
     */
    public List<String> longestWords(String[] dictionary) {
        // write your code here
        int maxLength = 0;
        Map<Integer, List<String>> countMap = new HashMap<Integer, List<String>>();
        for (int i = 0; i < dictionary.length; i++) {
            // 保存最大长度值
            if (dictionary[i].length() > maxLength) {
                maxLength = dictionary[i].length();
            }
            // 按照长度保存为list
            if (countMap.get(dictionary[i].length()) == null) {
                List countList = new ArrayList();
                countList.add(dictionary[i]);
                countMap.put(dictionary[i].length(), countList);
            } else {
                List countList = countMap.get(dictionary[i].length());
                countList.add(dictionary[i]);
            }
        }
        return countMap.get(maxLength);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Trie树来解决这个问题。我们可以将这N个英文单词插入到Trie树,然后在Trie树上搜索包含给定字符串的所有单词。 具体地,我们可以从给定字符串的第一个字符开始,在Trie树上进行深度优先搜索。每到达一个节点,我们就检查该节点是否表示一个单词,如果是,则将该单词加入到结果列表。然后,我们继续向下搜索,直到搜索完整个字符串。 下面是使用Python实现的代码: ```python class TrieNode: def __init__(self): self.children = {} self.is_word = False class Trie: def __init__(self): self.root = TrieNode() def insert(self, word): node = self.root for ch in word: if ch not in node.children: node.children[ch] = TrieNode() node = node.children[ch] node.is_word = True def search(self, word): node = self.root for ch in word: if ch not in node.children: return [] node = node.children[ch] return self._dfs(node, word) def _dfs(self, node, prefix): res = [] if node.is_word: res.append(prefix) for ch in node.children: res.extend(self._dfs(node.children[ch], prefix + ch)) return res # 示例 words = ["apple", "banana", "orange", "pineapple"] trie = Trie() for word in words: trie.insert(word) prefix = "app" res = trie.search(prefix) print(res) # ['apple'] ``` 在上面的代码,我们首先定义了一个TrieNode类和一个Trie类。TrieNode类表示Trie树的一个节点,包含一个字典children,用来存储子节点,以及一个布尔变量is_word,用来表示该节点是否表示一个单词。Trie类包含一个根节点root,以及insert方法和search方法。 insert方法用来将一个单词插入到Trie树。我们从根节点开始遍历单词的每个字符,如果当前字符不在当前节点的子节点,则创建一个新的节点,并将其添加到子节点。最终,我们将最后一个节点标记为单词的结尾。 search方法用来查找包含给定字符串的所有单词。我们从根节点开始遍历字符串的每个字符,如果当前字符不在当前节点的子节点,则说明不存在以该字符串为前缀的单词,直接返回空列表。否则,我们继续向下搜索。如果当前节点表示一个单词,则将该单词加入到结果列表。然后,我们继续向下搜索,直到搜索完整个字符串。最后,返回结果列表。 在示例,我们首先创建一个包含4个单词的Trie树。然后,我们搜索以"app"为前缀的所有单词,得到结果["apple"]。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值