-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
eunhwa99
committed
Jan 5, 2025
1 parent
0bc0ac8
commit 6341418
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
// insert ์, ๋ฌธ์์ด์ prefix๋ฅผ ๋ค Map์ ์ ์ฅํ๊ณ , ํด๋น ๋ฌธ์์ด์ prefix ์ด๋ฏ๋ก boolean false ๋ก ์ค์ | ||
// prefix๊ฐ ์๋ ์จ์ ํ ๋ฌธ์์ด ์ฝ์ ์ true ๋ก ์ ์ฅ | ||
|
||
// search ์, Map์ ํด๋น ๋จ์ด๊ฐ ์๋์ง ํ์ธํ๊ณ , boolean ๊ฐ์ด true ์ธ์ง ํ์ธ | ||
// startsWith๋ ๊ทธ๋ฅ Map ์ ํด๋น ๋ฌธ์์ด์ด ์๋์ง ํ์ธํ๋ฉด ๋๋ค. | ||
|
||
// ๊ณต๊ฐ ๋ณต์ก๋: Map ํฌ๊ธฐ -> O(N) | ||
// ์๊ฐ ๋ณต์ก๋: ์ ์ฒด ํธ์ถ ์ * String ๊ธธ์ด -> O(N*M) | ||
// ์ฐธ๊ณ ) ์ต๋ ์๊ฐ ๋ณต์ก๋ : 2000 * 3*10^4 = 6*10^7 | ||
class Trie { | ||
|
||
Map<String,Boolean> stringSet; | ||
public Trie() { | ||
stringSet = new HashMap<>(); | ||
} | ||
|
||
public void insert(String word) { | ||
for(int i=0;i<word.length();i++){ | ||
stringSet.putIfAbsent(word.substring(0, i), false); | ||
} | ||
stringSet.put(word, true); | ||
} | ||
|
||
public boolean search(String word) { | ||
return stringSet.containsKey(word) && stringSet.get(word)==true; | ||
} | ||
|
||
public boolean startsWith(String prefix) { | ||
|
||
return stringSet.containsKey(prefix); | ||
} | ||
} |