Ksh*_*jee 4 algorithm map trie
给定一组字符串(大集合)和输入字符串,您需要有效地找到输入字符串的所有字符.您将使用什么数据结构.使用它,你会如何找到字谜?
我想到的是这些:
使用地图
a)消除所有字母多于/少于输入的字母.
b)将输入字符放在地图中
c)遍历每个字符串的地图,看看是否所有字母都带有计数.
使用尝试
a)将所有具有正确字符数的字符串放入trie中.
b)遍历每个分支,如果输入中包含字母,则更深入.
c)如果叶子到达这个词是一个字谜
有人能找到更好的解决方案吗?
您在上述方法中发现了哪些问题?
从每个单词构建频率图并比较这些地图.
伪代码:
class Word
string word
map<char, int> frequency
Word(string w)
word = w
for char in word
int count = frequency.get(char)
if count == null
count = 0
count++
frequency.put(char, count)
boolean is_anagram_of(that)
return this.frequency == that.frequency
Run Code Online (Sandbox Code Playgroud)