일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- LeetCode
- Generator
- 시바견
- 43. Multiply Strings
- Python Code
- Convert Sorted List to Binary Search Tree
- data science
- Protocol
- 파이썬
- 운영체제
- 315. Count of Smaller Numbers After Self
- iterator
- 프로그래머스
- Python Implementation
- 밴픽
- DWG
- Decorator
- Regular Expression
- t1
- shiba
- Python
- kaggle
- 109. Convert Sorted List to Binary Search Tree
- Substring with Concatenation of All Words
- 30. Substring with Concatenation of All Words
- attribute
- 컴퓨터의 구조
- Class
- concurrency
- 715. Range Module
Archives
- Today
- Total
Scribbling
[Java 101] 211. Design Add and Search Words Data Structure - trie 본문
Computer Science/Java
[Java 101] 211. Design Add and Search Words Data Structure - trie
focalpoint 2023. 3. 11. 01:09class TrieNode {
public boolean isWord = false;
public TrieNode[] children = new TrieNode[26];
}
class Trie {
public TrieNode root;
public Trie() {
root = new TrieNode();
}
public void insert(String word) {
TrieNode cur = root;
for (int i=0; i<word.length(); i++) {
char c = word.charAt(i);
if (cur.children[c - 'a'] == null) cur.children[c - 'a'] = new TrieNode();
cur = cur.children[c - 'a'];
}
cur.isWord = true;
}
public boolean search(String word) {
return _search(root, word);
}
public boolean _search(TrieNode node, String word) {
if (word.isEmpty()) {
return node.isWord;
}
char c = word.charAt(0);
if (c == '.') {
for (char l='a'; l<='z'; l++) {
if (node.children[l - 'a'] != null) {
if (_search(node.children[l - 'a'], word.substring(1))) {
return true;
}
}
}
return false;
} else {
if (node.children[c - 'a'] == null) return false;
return _search(node.children[c - 'a'], word.substring(1));
}
}
}
class WordDictionary {
Trie t;
public WordDictionary() {
t = new Trie();
}
public void addWord(String word) {
t.insert(word);
}
public boolean search(String word) {
return t.search(word);
}
}
'Computer Science > Java' 카테고리의 다른 글
[Java] LeetCode: 1606. Find Servers That Handled Most Number of Requests (0) | 2023.08.27 |
---|---|
[Java 101] 435. Non-overlapping Intervals - Comparator (0) | 2023.03.15 |
[Java101] Trie Implementation (0) | 2023.03.08 |
[Java 101] 102. Binary Tree Level Order Traversal - Queue (0) | 2023.03.04 |
[Java101] LeetCode: 23. Merge k Sorted Lists: Priority Queue (0) | 2023.03.02 |