当前位置:网站首页>Sword finger offer II 064. magic Dictionary (medium dictionary tree string design)
Sword finger offer II 064. magic Dictionary (medium dictionary tree string design)
2022-07-28 22:25:00 【Calm in the wind and rain】
The finger of the sword Offer II 064. Magic dictionary
Design a data structure initialized with word list , Words in the word list Different from each other . If you give a word , Please decide whether you can replace only one letter of this word with another , Make the new words exist in the built magic Dictionary .
Realization MagicDictionary class :
MagicDictionary() Initialize object
void buildDict(String[] dictionary) Use string arrays dictionary Set the data structure ,dictionary Strings in are different from each other
bool search(String searchWord) Given a string searchWord , Determine whether you can only put... In the string One Replace one letter with another , So that the new string formed can match any string in the dictionary . If possible , return true ; otherwise , return false .
Example :
Input
inputs = [“MagicDictionary”, “buildDict”, “search”, “search”, “search”, “search”]
inputs = [[], [[“hello”, “leetcode”]], [“hello”], [“hhllo”], [“hell”], [“leetcoded”]]
Output
[null, null, false, true, false, false]
explain
MagicDictionary magicDictionary = new MagicDictionary();
magicDictionary.buildDict([“hello”, “leetcode”]);
magicDictionary.search(“hello”); // return False
magicDictionary.search(“hhllo”); // Put the second ‘h’ Replace with ‘e’ Can match “hello” , So back True
magicDictionary.search(“hell”); // return False
magicDictionary.search(“leetcoded”); // return False
Tips :
1 <= dictionary.length <= 100
1 <= dictionary[i].length <= 100
dictionary[i] It's only made up of lowercase letters
dictionary All the strings in Different from each other
1 <= searchWord.length <= 100
searchWord It's only made up of lowercase letters
buildDict Only in search Before calling once
Call at most 100 Time search
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/US1pGT
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
analysis
First, construct prefix tree , Put the words in the dictionary into the prefix tree , And then use it dfs Search all the letters of the target word , Yes 26 Judge every letter , Use a variable cnt Record the number of letter substitutions , If the replacement is more than one letter, it returns false,dfs Backtracking will jump to the judgment of the next letter .
Answer key (Java)
class MagicDictionary {
static class TrieNode {
public TrieNode[] children;
public boolean isWord;
public TrieNode() {
children = new TrieNode[26];
}
}
private TrieNode root;
/** Initialize your data structure here. */
public MagicDictionary() {
root = new TrieNode();
}
public void buildDict(String[] dictionary) {
// Put the words of the dictionary into the prefix tree
for (String word : dictionary) {
TrieNode node = root;
for (char ch : word.toCharArray()) {
if (node.children[ch - 'a'] == null) {
node.children[ch - 'a'] = new TrieNode();
}
node = node.children[ch - 'a'];
}
node.isWord = true;
}
}
public boolean search(String searchWord) {
return dfs(root, searchWord, 0, 0);
}
private boolean dfs(TrieNode root, String searchWord, int i, int cnt) {
if (root == null) {
// The current letter does not exist in the tree
return false;
}
if (root.isWord && i == searchWord.length() && cnt == 1) {
// After a replacement, the target word is the word in the tree
return true;
}
if (i < searchWord.length() && cnt < 2) {
boolean found = false;
for (int j = 0; j < 26 && !found; j++) {
//found yes true There is no need to judge the subsequent letters
int next = j == searchWord.charAt(i) - 'a' ? cnt : cnt + 1;
found = dfs(root.children[j], searchWord, i + 1, next);
}
return found;
}
return false;
}
}
/** * Your MagicDictionary object will be instantiated and called as such: * MagicDictionary obj = new MagicDictionary(); * obj.buildDict(dictionary); * boolean param_2 = obj.search(searchWord); */
边栏推荐
- Add DNS server to LAN for domain name resolution
- HCIP(11)
- Can the MySQL create statement be used to create a table structure and append new records
- HCIP(12)
- Data visualization news, different forms of news reports
- 2021 mathematical modeling group B exercise
- lotus 1.16.0 延长扇区过期时间
- internet的基本服务中文件传输命令是哪个
- 学习 Kotlin - 扩展函数
- The difference between get and post
猜你喜欢
随机推荐
SQL injection less38 (Stack Injection)
ssh 免密码登录
深度学习必备:对数据集的拆分、根据拆分图片拆分labels、对全部标注标签进行区间检查
[LiteratureReview]Object Detection and Mapping with Bounding Box Constraints
CDN工作原理
Typeof principle
SQL injection less34 (post wide byte injection + Boolean blind injection)
Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
C语言编程规范学习笔记和总结
What is time complexity
Can the MySQL create statement be used to create a table structure and append new records
[NLP] generate word cloud
vuejs中如何实现动态路由切换及路由的缓存
Aimbetter insight into your database, DPM and APM solutions
HCIP(13)
[cloud native kubernetes] mapping external service under kubernetes cluster eendpoint
静态成员static详解
Basic introduction of Rockwell AB PLC rslogix digital quantity IO module
From Web3 to web2.5, is it backward or another way?
Form validation and cascading drop-down lists (multiple implementations)









