Skip to content

Commit 9715128

Browse files
week9 mission - Design Add and Search Words Data Structure
1 parent 7bfc4a0 commit 9715128

File tree

1 file changed

+198
-0
lines changed

1 file changed

+198
-0
lines changed
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
- https://leetcode.com/problems/design-add-and-search-words-data-structure
2+
- https://algorithm.jonghoonpark.com/2024/06/30/leetcode-211
3+
4+
## brute force
5+
6+
์•„์Šฌ์•„์Šฌํ•˜๊ฒŒ ํ†ต๊ณผํ•œ๋‹ค.
7+
8+
```java
9+
class WordDictionary {
10+
11+
Set<String> wordSet;
12+
13+
public WordDictionary() {
14+
wordSet = new HashSet<>();
15+
}
16+
17+
public void addWord(String word) {
18+
wordSet.add(word);
19+
}
20+
21+
public boolean search(String word) {
22+
Deque<String> queue = new ArrayDeque<>();
23+
queue.push(word);
24+
25+
while (queue.getFirst().contains(".")) {
26+
String _word = queue.removeFirst();
27+
String pre = _word.substring(0, _word.indexOf("."));
28+
String post = _word.substring(_word.indexOf(".") + 1);
29+
30+
for (char c = 'a'; c <= 'z'; c++) {
31+
queue.addLast(pre + c + post);
32+
}
33+
}
34+
35+
while (!queue.isEmpty()) {
36+
String _word = queue.removeFirst();
37+
if (wordSet.contains(_word)) {
38+
return true;
39+
}
40+
}
41+
42+
return false;
43+
}
44+
}
45+
```
46+
47+
### TC, SC
48+
49+
- `.` ์ด ์—†์„ ๋•Œ
50+
- ์‹œ๊ฐ„ ๋ณต์žก๋„ : `O(1)`
51+
- ๊ณต๊ฐ„ ๋ณต์žก๋„ : `O(1)`
52+
- `.` ์ด ์žˆ์„ ๋•Œ
53+
- ์‹œ๊ฐ„ ๋ณต์žก๋„ : `O(26^N)`
54+
- ๊ณต๊ฐ„ ๋ณต์žก๋„ : `O(26^N)`
55+
- ์—ฌ๊ธฐ์„œ N์€ `.` ์˜ ์ˆ˜
56+
57+
## trie
58+
59+
[208. Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree/) ๋ฌธ์ œ ์—์„œ ์‚ฌ์šฉํ•œ Trie ์žฌ์‚ฌ์šฉ.
60+
61+
```java
62+
class WordDictionary {
63+
64+
Trie trie; // Trie ๊ตฌํ˜„์€ ์ƒ๋žต
65+
66+
public WordDictionary() {
67+
trie = new Trie();
68+
}
69+
70+
public void addWord(String word) {
71+
trie.insert(word);
72+
}
73+
74+
public boolean search(String word) {
75+
if (word.contains(".")) {
76+
String pre = word.substring(0, word.indexOf("."));
77+
String post = word.substring(word.indexOf(".") + 1);
78+
79+
if (trie.startsWith(pre)) {
80+
for (char c = 'a'; c <= 'z'; c++) {
81+
if (search(pre + c + post)) {
82+
return true;
83+
}
84+
}
85+
}
86+
87+
return false;
88+
}
89+
90+
return trie.search(word);
91+
}
92+
93+
94+
}
95+
```
96+
97+
### TC, SC
98+
99+
์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ `L`, `.` ์˜ ์ˆ˜๋ฅผ `N` ์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ
100+
101+
addWord ๋ฉ”์†Œ๋“œ์˜ ๊ฒฝ์šฐ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” `O(L)`์ด๋‹ค.
102+
search ๋ฉ”์†Œ๋“œ์˜ ๊ฒฝ์šฐ ์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ n ์ด๋ผ ํ•˜์˜€์„ ๋•Œ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” `O(L * 26 ^ N)`์ด๋‹ค.
103+
104+
๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” Trie ๊ตฌ์กฐ๋ฅผ ๋งŒ๋“œ๋Š”๋ฐ ์‚ฌ์šฉ๋œ ๊ณต๊ฐ„์ด๋‹ค. `insert๋œ ๋ฌธ์ž์—ด ๊ธธ์ด์˜ ํ‰๊ท ` ๋ฅผ `avg(L)`์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ `O(avg(L) * 26)`์ด๋‹ค. 26์€ ๊ณ„์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค.
105+
106+
## trie ๊ฐœ์„ 
107+
108+
์ด ๋ฌธ์ œ์— ์ ํ•ฉํ•˜๋„๋ก search๋ฅผ ์ˆ˜์ •ํ•˜์˜€๋‹ค.
109+
110+
```java
111+
class WordDictionary {
112+
113+
Trie trie;
114+
115+
public WordDictionary() {
116+
trie = new Trie();
117+
}
118+
119+
public void addWord(String word) {
120+
trie.insert(word);
121+
}
122+
123+
public boolean search(String word) {
124+
return trie.search(word);
125+
}
126+
127+
128+
}
129+
130+
class Trie {
131+
132+
Node root = new Node();
133+
134+
public Trie() {
135+
136+
}
137+
138+
public void insert(String word) {
139+
Node currentNode = root;
140+
for (char c : word.toCharArray()) {
141+
if (currentNode.nodes[c - 97] == null) {
142+
currentNode.nodes[c - 97] = new Node();
143+
}
144+
currentNode = currentNode.nodes[c - 97];
145+
}
146+
currentNode.val = word;
147+
}
148+
149+
public boolean search(String word) {
150+
return search(root, word, 0);
151+
}
152+
153+
public boolean search(Node node, String word, int index) {
154+
if (node == null) {
155+
return false;
156+
}
157+
158+
if (node.val != null && node.val.length() == word.length()) {
159+
return true;
160+
}
161+
162+
if (index >= word.length()) {
163+
return false;
164+
}
165+
166+
char c = word.charAt(index);
167+
168+
if (c == '.') {
169+
for (char _c = 'a'; _c <= 'z'; _c++) {
170+
if (search(node.nodes[_c - 97], word, index + 1)) {
171+
return true;
172+
}
173+
}
174+
return false;
175+
} else if (node.nodes[c - 97] == null) {
176+
return false;
177+
}
178+
179+
return search(node.nodes[c - 97], word, index + 1);
180+
}
181+
}
182+
183+
class Node {
184+
String val;
185+
Node[] nodes = new Node[26];
186+
}
187+
```
188+
189+
### TC, SC
190+
191+
์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ `L`, `.` ์˜ ์ˆ˜๋ฅผ `N` ์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ
192+
193+
addWord ๋ฉ”์†Œ๋“œ์˜ ๊ฒฝ์šฐ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” `O(L)`์ด๋‹ค.
194+
search ๋ฉ”์†Œ๋“œ์˜ ๊ฒฝ์šฐ ์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ n ์ด๋ผ ํ•˜์˜€์„ ๋•Œ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” `O(L * 26 ^ N)`์ด๋‹ค.
195+
๊ฐœ์„  ์ „๊ณผ ๋น„๊ตํ•ด๋ดค์„ ๋•Œ ํ‘œ๊ธฐ์ƒ์œผ๋กœ๋Š” ์ฐจ์ด๊ฐ€ ์—†์œผ๋‚˜, ๋ถˆํ•„์š”ํ•œ ๊ณผ์ •์„ ์ œ๊ฑฐํ•˜๊ฒŒ๋˜์–ด์„œ ์‹œ๊ฐ„์ด ๋งค์šฐ ๋‹จ์ถ•๋œ๋‹ค.
196+
(`trie.startsWith(pre)`์ด ์‚ฌ๋ผ์กŒ๊ณ , search์˜ ํ˜ธ์ถœ ํšŸ์ˆ˜๊ฐ€ ์ค„์–ด๋“ฌ.)
197+
198+
๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” Trie ๊ตฌ์กฐ๋ฅผ ๋งŒ๋“œ๋Š”๋ฐ ์‚ฌ์šฉ๋œ ๊ณต๊ฐ„์ด๋‹ค. `insert๋œ ๋ฌธ์ž์—ด ๊ธธ์ด์˜ ํ‰๊ท ` ๋ฅผ `avg(L)`์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ `O(avg(L) * 26)`์ด๋‹ค. 26์€ ๊ณ„์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค.

0 commit comments

Comments
ย (0)