java 676.实施Magic Dictionary(#)。java
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 676.实施Magic Dictionary(#)。java相关的知识,希望对你有一定的参考价值。
/*
The implementation is a simple Trie, with the method relaxedSearch.
relaxedSearch searches for a word, with one deviation from a normal trie.
If there is a match with the current character, it proceeds as usual in that branch.
But for all the non matched characters, it still continues searching, by incrementing the changedTimes variable, which maintains how many times a character was changed in the word search from the root.
Any search that involves changedTimes > 1, is immediately terminated by returning false as we are allowed to change only one character.
The solution is reached, when we find word in the trie and the changedTimes is exactly == 1.
*/
class MagicDictionary {
Trie trie;
public MagicDictionary() {
trie = new Trie(256);
}
public void buildDict(String[] dict) {
Arrays.stream(dict).forEach(s -> trie.insert(s));
}
public boolean search(String word) {
return trie.relaxedSearch(word);
}
class Trie {
private int R;
private TrieNode root;
public Trie(int R) {
this.R = R;
root = new TrieNode();
}
public boolean relaxedSearch(String word) {
return relaxedSearch(root, word, 0);
}
private boolean relaxedSearch(TrieNode root, String word, int changedTimes) {
if (root == null || (!root.isWord && word.isEmpty()) || changedTimes > 1) return false;
if (root.isWord && word.isEmpty()) return changedTimes == 1;
return Arrays.stream(root.next).anyMatch(nextNode -> relaxedSearch(nextNode, word.substring(1),
root.next[word.charAt(0)] == nextNode ? changedTimes : changedTimes+1));
}
// Inserts a word into the trie.
public void insert(String word) {
insert(root, word);
}
private void insert(TrieNode root, String word) {
if (word.isEmpty()) { root.isWord = true; return; }
if (root.next[word.charAt(0)] == null) root.next[word.charAt(0)] = new TrieNode();
insert(root.next[word.charAt(0)], word.substring(1));
}
private class TrieNode {
private TrieNode[] next = new TrieNode[R];
private boolean isWord;
}
}
}
class MagicDictionary {
private Map<String, List<int[]>> map;
/** Initialize your data structure here. */
public MagicDictionary() {
map = new HashMap<>();
}
/** Build a dictionary through a list of words */
public void buildDict(String[] dict) {
map.clear();
for (String word : dict) {
char[] w = word.toCharArray();
for (int i = 0; i < w.length; i++) {
char temp = w[i];
w[i] = '_';
String key = new String(w);
int[] pair = new int[] {i, temp};
//List<int[]> val = map.getOrDefault(key, new ArrayList<>());
//val.add(pair);
//map.put(key, val);
map.putIfAbsent(key, new ArrayList<>());
map.get(key).add(pair);
w[i] = temp;
}
}
}
/** Returns if there is any word in the trie that equals to the given word after modifying exactly one character */
public boolean search(String word) {
//if (set.contains(word)) return false;
char[] w = word.toCharArray();
for(int i = 0; i < w.length; i++) {
char temp = w[i];
w[i] = '_';
String key = new String(w);
if (map.containsKey(key)) {
for (int[] pair : map.get(key)) {
if (pair[0] == i && pair[1] != temp) return true;
}
}
w[i] = temp;
}
return false;
}
}
/**
* Your MagicDictionary object will be instantiated and called as such:
* MagicDictionary obj = new MagicDictionary();
* obj.buildDict(dict);
* boolean param_2 = obj.search(word);
*/
/*
Input:
["MagicDictionary", "buildDict", "search", "search", "search", "search"]
[[], [["hello","hallo","leetcode"]], ["hello"], ["hhllo"], ["hell"], ["leetcoded"]]
Output:
[null,null,false,true,false,false]
Expected:
[null,null,true,true,false,false]
*/
class MagicDictionary {
private HashSet<String> set;
/** Initialize your data structure here. */
public MagicDictionary() {
set = new HashSet<>();
}
/** Build a dictionary through a list of words */
public void buildDict(String[] dict) {
set.clear();
for (String word : dict) {
set.add(word);
}
}
/** Returns if there is any word in the trie that equals to the given word after modifying exactly one character */
public boolean search(String word) {
char[] w = word.toCharArray();
for(int i = 0; i < w.length; i++) {
char temp = w[i];
for (char j = 'a'; j <= 'z'; j++) {
if (temp != j) {
w[i] = j;
if (set.contains(new String(w))) {
return true;
}
}
}
w[i] = temp;
}
return false;
}
}
/**
* Your MagicDictionary object will be instantiated and called as such:
* MagicDictionary obj = new MagicDictionary();
* obj.buildDict(dict);
* boolean param_2 = obj.search(word);
*/
以上是关于java 676.实施Magic Dictionary(#)。java的主要内容,如果未能解决你的问题,请参考以下文章
[leetcode-676-Implement Magic Dictionary]
LC 676. Implement Magic Dictionary
[LeetCode] 676. Implement Magic Dictionary 实现神奇字典
java . 请在小于99999的正整数中找符合下列条件的数,它既是完全平方数,又有两位数字相同,如:144,676。
https://stackoverflow.com/questions/3232943/update-value-of-a-nested-dictionary-of-varying-depth(示例代