Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8c2b30a

Browse files
authored
add 211-Design-Add-and-Search-Words-Data-Structure
1 parent1b59ccb commit8c2b30a

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
classTrieNode{
2+
constructor(){
3+
this.children={};
4+
this.endOfWord=false;
5+
}
6+
}
7+
8+
classWordDictionary{
9+
constructor(){
10+
this.root=newTrieNode();
11+
}
12+
13+
addWord(word){
14+
letcur=this.root;
15+
16+
for(constcofword){
17+
if(!(cincur.children)){
18+
cur.children[c]=newTrieNode();
19+
}
20+
cur=cur.children[c];
21+
}
22+
cur.endOfWord=true;
23+
}
24+
25+
search(word){
26+
functiondfs(j,root){
27+
letcur=root;
28+
29+
for(leti=j;i<word.length;i++){
30+
constc=word[i];
31+
32+
if(c==="."){
33+
for(constkeyincur.children){
34+
if(Object.prototype.hasOwnProperty.call(cur.children,key)){
35+
constchild=cur.children[key];
36+
37+
if(dfs(i+1,child)){
38+
returntrue;
39+
}
40+
}
41+
}
42+
returnfalse;
43+
}else{
44+
if(!(cincur.children)){
45+
returnfalse;
46+
}
47+
cur=cur.children[c];
48+
}
49+
}
50+
51+
returncur.endOfWord;
52+
}
53+
returndfs(0,this.root);
54+
}
55+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp