Skip to content

Commit d38b41d

Browse files
committed
design add and search words data structure
1 parent 0fe4362 commit d38b41d

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
# μ—°κ΄€ 링크
2+
- [문제 풀이 μŠ€μΌ€μ€„](https://github.com/orgs/DaleStudy/projects/6/views/5)
3+
- [λ‹΅μ•ˆ μ½”λ“œ μ œμΆœλ²•](https://github.com/DaleStudy/leetcode-study/wiki/%EB%8B%B5%EC%95%88-%EC%A0%9C%EC%B6%9C-%EA%B0%80%EC%9D%B4%EB%93%9C)
4+
5+
# Problem
6+
- 문제 링크 : https://leetcode.com/problems/design-add-and-search-words-data-structure/description/
7+
- 문제 이름 : design add and search words data structure
8+
- 문제 번호 : 211
9+
- λ‚œμ΄λ„ : medium
10+
- μΉ΄ν…Œκ³ λ¦¬ :
11+
12+
# 문제 μ„€λͺ…
13+
- λ°μ΄ν„°λ² μ΄μŠ€μ— input wordλ₯Ό λ”ν•˜κ³  μ°ΎλŠ” 자료ꡬ쑰 λ””μžμΈν•˜κΈ°
14+
15+
# 아이디어
16+
- 이전 μ£Όμ°¨ word search와 λ™μΌν•œ μ ‘κ·Ό λ°©λ²•μœΌλ‘œ 풀이 κ°€λŠ₯
17+
### Trie
18+
```cpp
19+
class TrieNode{
20+
public:
21+
bool isCompleteWord;
22+
TrieNode* children[26];
23+
24+
TrieNode() {
25+
isCompleteWord = false;
26+
memset(children, 0, sizeof(children));
27+
}
28+
};
29+
```
30+
31+
- κ°€μ§€ 쀑간인지, μ•„λ‹ˆλ©΄ μ™„κ²°λœ 단어인지λ₯Ό 확인할 수 있음.
32+
- ν•΄λ‹Ή λ‘œμ§μ„ 톡해, Prefix ν™•μΈν•˜λŠ” λ©”μ„œλ“œ λ˜ν•œ μ‰½κ²Œ λ§Œλ“€ 수 있음.
33+
34+
# βœ… μ½”λ“œ (Solution)
35+
36+
## Trie - wrong answer
37+
```cpp
38+
class TrieNode{
39+
public:
40+
bool isCompleteWord;
41+
TrieNode* children[26];
42+
43+
TrieNode() {
44+
isCompleteWord = false;
45+
memset(children, 0, sizeof(children));
46+
}
47+
};
48+
49+
class WordDictionary {
50+
TrieNode* root;
51+
public:
52+
WordDictionary() {
53+
root = new TrieNode();
54+
}
55+
56+
void addWord(string word) {
57+
auto cur = root;
58+
for(auto c: word){
59+
if(!cur->children[c-'a']){
60+
cur->children[c-'a'] = new TrieNode();
61+
}
62+
cur = cur->children[c-'a'];
63+
}
64+
cur->isCompleteWord = true;
65+
}
66+
67+
bool search(string word) {
68+
auto cur = root;
69+
for(auto c: word){
70+
if(!cur->children[c-'a']){
71+
return false;
72+
}
73+
cur = cur->children[c-'a'];
74+
}
75+
return cur->isCompleteWord;
76+
}
77+
};
78+
79+
/**
80+
* Your WordDictionary object will be instantiated and called as such:
81+
* WordDictionary* obj = new WordDictionary();
82+
* obj->addWord(word);
83+
* bool param_2 = obj->search(word);
84+
*/
85+
```
86+
87+
- 잘λͺ» μž‘μ„±ν•œ μ½”λ“œ
88+
- 자료ꡬ쑰 적용 쀑, edge case(`.` - dot)을 κ³ λ €ν•˜μ§€ μ•ŠμŒ
89+
- μ•„λŠ” 문제라고 μ‰½κ²Œ μƒκ°ν•œ 것이 잘λͺ».
90+
91+
```cpp
92+
class TrieNode {
93+
public:
94+
bool isCompleteWord;
95+
TrieNode* children[26];
96+
97+
TrieNode() {
98+
isCompleteWord = false;
99+
memset(children, 0, sizeof(children));
100+
}
101+
};
102+
103+
class WordDictionary {
104+
TrieNode* root;
105+
106+
bool dfs(TrieNode* node, const string& word, int index) {
107+
if (!node) return false;
108+
if (index == word.size()) return node->isCompleteWord;
109+
110+
char c = word[index];
111+
if (c == '.') {
112+
// '.'μ΄λ―€λ‘œ λͺ¨λ“  μžμ‹μ„ μ‹œλ„ν•΄λ³Έλ‹€
113+
for (int i = 0; i < 26; ++i) {
114+
if (dfs(node->children[i], word, index + 1)) {
115+
return true;
116+
}
117+
}
118+
return false;
119+
} else {
120+
return dfs(node->children[c - 'a'], word, index + 1);
121+
}
122+
}
123+
124+
public:
125+
WordDictionary() {
126+
root = new TrieNode();
127+
}
128+
129+
void addWord(string word) {
130+
TrieNode* cur = root;
131+
for (char c : word) {
132+
int idx = c - 'a';
133+
if (!cur->children[idx]) {
134+
cur->children[idx] = new TrieNode();
135+
}
136+
cur = cur->children[idx];
137+
}
138+
cur->isCompleteWord = true;
139+
}
140+
141+
bool search(string word) {
142+
return dfs(root, word, 0);
143+
}
144+
};
145+
146+
```
147+
148+
- dfs둜 κ΅¬ν˜„
149+
150+
# πŸ” μ½”λ“œ μ„€λͺ…
151+
152+
153+
# μ΅œμ ν™” 포인트 (Optimality Discussion)
154+
β€’ μ΅œμ ν™”ν•œ μ΄μœ μ™€ 원리
155+
β€’ 더 쀄일 수 μžˆλŠ” μ—¬μ§€λŠ” μžˆλŠ”κ°€?
156+
β€’ κΈ°μ‘΄ 방법 λŒ€λΉ„ μ–Όλ§ˆλ‚˜ νš¨μœ¨μ μ΄μ—ˆλŠ”μ§€
157+
158+
# πŸ§ͺ ν…ŒμŠ€νŠΈ & μ—£μ§€ μΌ€μ΄μŠ€
159+
160+
# πŸ“š κ΄€λ ¨ 지식 볡슡
161+
162+
# πŸ” 회고
163+
164+

0 commit comments

Comments
Β (0)