[PS] 99ํด๋ฝ ์ฝํ ์คํฐ๋ 15์ผ์ฐจ TIL (Prefix and Suffix Search)
ํ๊ทธ: 99ํด๋ฝ, PS, TIL, ์์ ํ์, ์ฝ๋ฉํ ์คํธ์ค๋น, ํญํด99
์นดํ ๊ณ ๋ฆฌ: PS
๋ฌธ์
์ค๋ช
๋ฌธ์์ด ๋ฐฐ์ด๊ณผ prefix ๋จ์ด, suffix ๋จ์ด๊ฐ ์ ๋ ฅ์ผ๋ก ์ฃผ์ด์ก์๋, ํด๋น prefix ์ suffix ๋ฅผ ๋์์ ๊ฐ์ง๊ณ ์๋ ๋จ์ด ์ธ๋ฑ์ค๋ฅผ ๋ฐํํด์ผํ๋ค. ๋ง์ฝ ์ ํจํ ์ธ๋ฑ์ค๊ฐ ์ฌ๋ฌ๊ฐ ์ด๋ฉด ์ต๋๊ฐ์ ๋ฐํํ๋ค.
ํ์ด (HashMap)
๊ฐ๋ฅํ ๋ชจ๋ ์ ๋์ฌ, ์ ๋ฏธ์ฌ ์กฐํฉ์ HashMap ์ key ๋ก ๋ด์๋๋ค. ๊ทธ๋ฆฌ๊ณ ๋ฐฐ์ด์์ ๋ช๋ฒ์งธ ๋จ์ด์ ํด๋นํ๋์ง ์ธ๋ฑ์ค๋ฅผ value ๋ก ๋ด์๋๋ค.
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
String[] words = {"apple"};
// hashMap
{
"a#e": 0,
"a#le": 0,
"a#ple": 0,
"a#pple": 0,
"a#apple": 0,
"ap#e": 0,
"ap#le": 0,
"ap#ple": 0,
"ap#pple": 0,
"ap#apple": 0,
"app#e": 0,
"app#le": 0,
"app#ple": 0,
"app#pple": 0,
"app#apple": 0,
"appl#e": 0,
"appl#le": 0,
"appl#ple": 0,
"appl#pple": 0,
"appl#apple": 0,
"apple#e": 0,
"apple#le": 0,
"apple#ple": 0,
"apple#pple": 0,
"apple#apple": 0
}
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
class WordFilter {
private Map<String, Integer> map = new HashMap<>();
public WordFilter(String[] words) {
for (int i = 0; i < words.length; i++) {
String word = words[i];
int length = word.length();
for (int j = 1; j <= length; j++) {
for (int k = 1; k <= length; k++) {
String key = word.substring(0, j) + "{" + word.substring(length - k);
map.put(key, i);
}
}
}
}
public int f(String pref, String suff) {
String s = pref + "{" + suff;
return map.getOrDefault(s, -1);
}
}
/**
* Your WordFilter object will be instantiated and called as such:
* WordFilter obj = new WordFilter(words);
* int param_1 = obj.f(pref,suff);
*/
์๊ฐ๋ณต์ก๋
-
์์ฑ์(
WordFilter
) ๋จ์ด์ ๊ธธ์ด๊ฐ L ์ด๋ฉด ํ ๋จ์ด์ ๋ํด ๊ฐ๋ฅํ ์กฐํฉ ์๋ ์ ๋์ฌ์ ์ ๋ฏธ์ฌ๊ฐ ๊ฐ๊ฐ L ๊ฐ ์ด๋ฏ๋ก L^2 ์ด ๋๋ค. N ๊ฐ์ ๋จ์ด๊ฐ ์์๋ ์๊ฐ ๋ณต์ก๋๋ O(N * L^2) ์ด ๋๋ค. -
์ฟผ๋ฆฌ ๋ฉ์๋(
f
) ํด์๋งต ํค์ ์ ํํ ๋ง๋ ๊ฐ์ผ๋ก ์กฐํํ๊ธฐ ๋๋ฌธ์ O(1) ์ด๋ค.
ํ์ด (Trie)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class TrieNode {
Map<Character, TrieNode> children;
int index;
public TrieNode() {
children = new HashMap<>();
index = -1;
}
}
class WordFilter {
private TrieNode root;
public WordFilter(String[] words) {
root = new TrieNode();
int n = words.length;
for (int i = 0; i < n; i++) {
String word = words[i];
int len = word.length();
// ๋ชจ๋ prefix์ suffix ์กฐํฉ์ ์ ์ฅ
for (int j = 0; j <= len; j++) {
String modifiedWord = word.substring(j) + "{" + word;
insert(modifiedWord, i);
}
}
}
private void insert(String word, int index) {
TrieNode current = root;
for (char ch : word.toCharArray()) {
current = current.children.computeIfAbsent(ch, c -> new TrieNode());
current.index = index; // ํ์ฌ ๋
ธ๋์ ๊ฐ์ฅ ํฐ ์ธ๋ฑ์ค ์ ์ฅ
}
}
public int f(String prefix, String suffix) {
String searchWord = suffix + "{" + prefix;
TrieNode current = root;
for (char ch : searchWord.toCharArray()) {
current = current.children.get(ch);
if (current == null) {
return -1;
}
}
return current.index;
}
}
๋๊ธ๋จ๊ธฐ๊ธฐ