解法一:哈希表
建立哈希表,判断重复和前缀
class Trie {
HashSet<String> set = new HashSet<>();
public Trie() {
}
public void insert(String word) {
set.add(word);
}
public boolean search(String word) {
return set.contains(word);
}
public boolean startsWith(String prefix) {
for (String s : set) {
if(s.startsWith(prefix)) return true;
}
return false;
}
}
9/8/23About 1 min