Skip to content

Commit 1fc0b70

Browse files
committed
- group anagrams
- implement trie prefix tree - word break
1 parent c2ceebd commit 1fc0b70

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import java.util.*;
2+
3+
public class Geegong {
4+
5+
/**
6+
* case 1. HashMap<String, List<String>> ์œผ๋กœ velues ๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ๋ฐฉ๋ฒ•
7+
* key ๊ฐ’์€ strs ์˜ ๊ฐ str ๋“ค์„ char array ๋กœ ๋ณ€ํ™˜ํ•œ ํ›„ sorting ํ•œ๋‹ค.
8+
* ๊ทธ๋Ÿผ ๋™์ผํ•œ letter ๋“ค์„ ๊ฐ–๋Š” ์•„์ด๋“ค์€ ์†ŒํŒ… ํ›„์— ๋™์ผํ•œ char array ๋ฅผ ๊ฐ€์ง€๋ฏ€๋กœ ๊ทธ๋ฃนํ•‘์ด ๊ฐ€๋Šฅํ•จ
9+
*
10+
* TimeComplexity:
11+
* O(N * L log L) = O(N)
12+
* -> N ์€ strs ์˜ ๊ฐฏ์ˆ˜
13+
* -> L ์€ str ์˜ ๊ธธ์ด
14+
* -> L long L ์€ str์˜ sorting ์‹œ๊ฐ„ ๋ณต์žก๋„
15+
*
16+
* space complexity: O(N)
17+
*
18+
* case 2. key : lower letter ๋“ค๋งŒ ๋ณด์žฅ์ด ๋œ ๊ฒฝ์šฐ char[26] ์ธ ๋ฐฐ์—ด๋“ค์— ๋Œ€ํ•ด์„œ ์ธ๋ฑ์Šค๊ฐ€ 0 ~ 25 ๊นŒ์ง€ 'a' ~ 'z' ์œ„ ๊ฐ’์„ ์˜๋ฏธํ•œ๋‹ค๊ณ  ํ•˜๋ฉด
19+
* letter ๋ณ„๋กœ ์นด์šดํŠธ๋ฅผ ํ•ด๋‹น๋˜๋Š” ์ธ๋ฑ์Šค์— +1 ์”ฉ ํ•˜๊ณ  ๊ตฌ๋ถ„์ž('#")๋กœ ๊ฐ letter ๋ณ„ ํšŸ์ˆ˜๋ฅผ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋Š” key ๋ฅผ ๋งŒ๋“ค์–ด
20+
* ๊ฒฐ๊ตญ case1๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ str ์— ํ•ด๋‹น๋˜๋Š” key ๋ฅผ ๊ฐ–๋Š” List<String> ์„ ๊ฐ–๋Š” hashMap ์„ ๊ฐ€์ง€๊ฒŒ ํ•˜๋Š” ๋ฐฉ๋ฒ•
21+
*
22+
* Time complexity :
23+
* O(L * N) = O(N)
24+
*
25+
* Space complexity :
26+
* O(N)
27+
*
28+
* @param strs
29+
* @return
30+
*/
31+
public List<List<String>> groupAnagrams(String[] strs) {
32+
// case 1.
33+
// Map<String, List<String>> resultMap = new HashMap<>();
34+
//
35+
// for (String str : strs) {
36+
// char[] chars = str.toCharArray();
37+
// // sort
38+
// Arrays.sort(chars);
39+
// String key = new String(chars);
40+
// // key : sorted str, value : strs
41+
// resultMap.computeIfAbsent(key, k -> new ArrayList<>()).add(str);
42+
// }
43+
//
44+
// return new ArrayList<>(resultMap.values());
45+
46+
// case 2.
47+
Map<String, List<String>> resultMap = new HashMap<>();
48+
for (String str : strs) {
49+
char[] chars = str.toCharArray();
50+
char[] numberOfLetters = new char[26]; // 'a' ~ 'z'
51+
// Arrays.copyOf()
52+
53+
for (char eachChar : chars) {
54+
numberOfLetters[eachChar - 'a']++; // ๊ฐ character ์— ํ•ด๋‹น๋˜๋Š” ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค์— 1์”ฉ ๊ฐ’์„ ๋”ํ•ด๊ฐ„๋‹ค ex) aba => ['2', '0', '0', ... ,'0'] / abfcb = ['1', '2', '1', '0', '0', '1', '0'...'0']
55+
}
56+
57+
StringBuilder keyMaker = new StringBuilder(2 * 26);
58+
for (char numberOfLetter : numberOfLetters) {
59+
keyMaker.append(numberOfLetter).append("#");
60+
}
61+
62+
resultMap.computeIfAbsent(keyMaker.toString(), input -> new ArrayList<>()).add(str);
63+
}
64+
65+
return new ArrayList<>(resultMap.values());
66+
}
67+
68+
69+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
public class Geegong {
2+
3+
/**
4+
* ๊ฐ ๊ธ€์ž๋งˆ๋‹ค Node ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€์ง€๊ณ  ๊ทธ ์•ˆ์— Node[] ๋ฐฐ์—ด์„ ๊ฐ–๊ณ  ์žˆ๋Š” ํ˜•ํƒœ
5+
* ์ฐพ๋Š”๊ฒƒ์€ ๋งค์šฐ ๋น ๋ฅด๋‚˜ (์ธ๋ฑ์Šค๋ฅผ ๊ธ€์ž - 'a' )
6+
* ๋ถˆํ•„์š”ํ•œ ๊ณต๊ฐ„์„ 26 byte์˜ ๋ฐฐ์—ด์„ ๊ฐ€์ง€๊ณ  ์žˆ์Œ
7+
*
8+
* N์€ word ์˜ ๊ธธ์ด๋ผ๊ณ  ์น˜๋ฉด
9+
* Time complexity
10+
* search, startsWith : O(N)
11+
*
12+
* Space complexity :
13+
* O(26 * N)
14+
* = O(N)
15+
*/
16+
class Trie {
17+
private Node node;
18+
19+
public static class Node {
20+
private Node[] next = new Node[26]; // ๊ธ€์ž a ~ z, A ~ Z
21+
private boolean end; // ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์ธ์ง€์˜ ์—ฌ๋ถ€
22+
}
23+
24+
public Trie() {
25+
this.node = new Node();
26+
}
27+
28+
public void insert(String word) {
29+
30+
Node current = this.node;
31+
for (char wordChar : word.toCharArray()) {
32+
int index = wordChar - 'a'; // word ๊ฐ€ 'a' ~ 'Z' ๊นŒ์ง€๊ฐ€ ๋ณด์žฅ๋œ๋‹ค๋ฉด
33+
if (current.next[index] == null) {
34+
current.next[index] = new Node();
35+
}
36+
current = current.next[index];
37+
}
38+
current.end = true;
39+
}
40+
41+
public boolean search(String word) {
42+
Node current = this.node;
43+
for (char wordChar : word.toCharArray()) {
44+
int index = wordChar - 'a';
45+
if (current.next[index] == null) {
46+
return false;
47+
}
48+
current = current.next[index];
49+
}
50+
51+
return current != null && current.end;
52+
}
53+
54+
public boolean startsWith(String prefix) {
55+
Node current = this.node;
56+
for (char wordChar : prefix.toCharArray()) {
57+
int index = wordChar - 'a';
58+
if (current.next[index] == null) {
59+
return false;
60+
}
61+
current = current.next[index];
62+
}
63+
64+
return current != null;
65+
}
66+
}
67+
68+
}

โ€Žword-break/Geegong.javaโ€Ž

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.Arrays;
2+
import java.util.HashSet;
3+
import java.util.List;
4+
5+
public class Geegong {
6+
7+
/**
8+
* string segmentation ์„ ์ด์šฉํ•ด์„œ ํ’€์–ดํ–ํ•œ๋‹ค.
9+
* dp๋ฅผ ์ด์šฉํ•ด segmentation ์ด ๋œ ์ง€์ ์„ true ๋กœ ์ €์žฅํ•˜๋ฉด์„œ s ์ด length ์ธ๋ฑ์Šค์˜ ๊ฐ’์ด true ๋กœ ๋˜์–ด์žˆ๋Š”์ง€ ์ฒดํฌ
10+
* Time complexity : O (N^3) ๐Ÿฅฒ
11+
* -> ์ด์ค‘ ๋ฃจํ”„ (N^2) * contains(substring) (N)
12+
*
13+
* Space complexity : O(N)
14+
* @param s
15+
* @param wordDict
16+
* @return
17+
*/
18+
public boolean wordBreak(String s, List<String> wordDict) {
19+
HashSet<String> distinctWords = new HashSet<>(wordDict);
20+
21+
// 1 = true, 0 = false
22+
int[] dp = new int[s.length() + 1];
23+
Arrays.fill(dp, 0);
24+
dp[0] = 1;
25+
26+
for (int rightIndex = 1; rightIndex <= s.length() ; rightIndex++) {
27+
for (int leftIndex = 0; leftIndex < rightIndex; leftIndex++) {
28+
if (dp[leftIndex] == 1 && distinctWords.contains(s.substring(leftIndex, rightIndex))) {
29+
dp[rightIndex] = 1;
30+
}
31+
}
32+
}
33+
34+
return dp[s.length()] == 1;
35+
}
36+
37+
}

0 commit comments

Comments
ย (0)