-
-
Notifications
You must be signed in to change notification settings - Fork 247
[geegong] WEEK 05 solutions #1847
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import java.util.*; | ||
|
||
public class Geegong { | ||
|
||
/** | ||
* case 1. HashMap<String, List<String>> 으로 velues 를 리턴하는 방법 | ||
* key 값은 strs 의 각 str 들을 char array 로 변환한 후 sorting 한다. | ||
* 그럼 동일한 letter 들을 갖는 아이들은 소팅 후에 동일한 char array 를 가지므로 그룹핑이 가능함 | ||
* | ||
* TimeComplexity: | ||
* O(N * L log L) = O(N) | ||
* -> N 은 strs 의 갯수 | ||
* -> L 은 str 의 길이 | ||
* -> L long L 은 str의 sorting 시간 복잡도 | ||
* | ||
* space complexity: O(N) | ||
* | ||
* case 2. key : lower letter 들만 보장이 된 경우 char[26] 인 배열들에 대해서 인덱스가 0 ~ 25 까지 'a' ~ 'z' 위 값을 의미한다고 하면 | ||
* letter 별로 카운트를 해당되는 인덱스에 +1 씩 하고 구분자('#")로 각 letter 별 횟수를 구분할 수 있는 key 를 만들어 | ||
* 결국 case1과 유사하게 str 에 해당되는 key 를 갖는 List<String> 을 갖는 hashMap 을 가지게 하는 방법 | ||
* | ||
* Time complexity : | ||
* O(L * N) = O(N) | ||
* | ||
* Space complexity : | ||
* O(N) | ||
* | ||
* @param strs | ||
* @return | ||
*/ | ||
public List<List<String>> groupAnagrams(String[] strs) { | ||
// case 1. | ||
// Map<String, List<String>> resultMap = new HashMap<>(); | ||
// | ||
// for (String str : strs) { | ||
// char[] chars = str.toCharArray(); | ||
// // sort | ||
// Arrays.sort(chars); | ||
// String key = new String(chars); | ||
// // key : sorted str, value : strs | ||
// resultMap.computeIfAbsent(key, k -> new ArrayList<>()).add(str); | ||
// } | ||
// | ||
// return new ArrayList<>(resultMap.values()); | ||
|
||
// case 2. | ||
Map<String, List<String>> resultMap = new HashMap<>(); | ||
for (String str : strs) { | ||
char[] chars = str.toCharArray(); | ||
char[] numberOfLetters = new char[26]; // 'a' ~ 'z' | ||
// Arrays.copyOf() | ||
|
||
for (char eachChar : chars) { | ||
numberOfLetters[eachChar - 'a']++; // 각 character 에 해당되는 배열의 인덱스에 1씩 값을 더해간다 ex) aba => ['2', '0', '0', ... ,'0'] / abfcb = ['1', '2', '1', '0', '0', '1', '0'...'0'] | ||
} | ||
|
||
StringBuilder keyMaker = new StringBuilder(2 * 26); | ||
for (char numberOfLetter : numberOfLetters) { | ||
keyMaker.append(numberOfLetter).append("#"); | ||
} | ||
|
||
resultMap.computeIfAbsent(keyMaker.toString(), input -> new ArrayList<>()).add(str); | ||
} | ||
|
||
return new ArrayList<>(resultMap.values()); | ||
} | ||
|
||
|
||
} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Node class를 내부 클래스로 구현하셨군요. 내부 클래스를 사용한 이유는 Trie class 에서만 사용하기 위해 그렇게 구현하신건지 궁금합니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 네넵~ 딱히 내부 클래스로 구현하려고 한건 아닙니다ㅎㅎ 단순히 제 이름 클래스 안에서 문제 풀이하려다보니 그렇게 작성되었네요😅 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
public class Geegong { | ||
|
||
/** | ||
* 각 글자마다 Node 클래스 인스턴스를 가지고 그 안에 Node[] 배열을 갖고 있는 형태 | ||
* 찾는것은 매우 빠르나 (인덱스를 글자 - 'a' ) | ||
* 불필요한 공간을 26 byte의 배열을 가지고 있음 | ||
* | ||
* N은 word 의 길이라고 치면 | ||
* Time complexity | ||
* search, startsWith : O(N) | ||
* | ||
* Space complexity : | ||
* O(26 * N) | ||
* = O(N) | ||
*/ | ||
class Trie { | ||
private Node node; | ||
|
||
public static class Node { | ||
private Node[] next = new Node[26]; // 글자 a ~ z, A ~ Z | ||
private boolean end; // 마지막 노드인지의 여부 | ||
} | ||
|
||
public Trie() { | ||
this.node = new Node(); | ||
} | ||
|
||
public void insert(String word) { | ||
|
||
Node current = this.node; | ||
for (char wordChar : word.toCharArray()) { | ||
int index = wordChar - 'a'; // word 가 'a' ~ 'Z' 까지가 보장된다면 | ||
if (current.next[index] == null) { | ||
current.next[index] = new Node(); | ||
} | ||
current = current.next[index]; | ||
} | ||
current.end = true; | ||
} | ||
|
||
public boolean search(String word) { | ||
Node current = this.node; | ||
for (char wordChar : word.toCharArray()) { | ||
int index = wordChar - 'a'; | ||
if (current.next[index] == null) { | ||
return false; | ||
} | ||
current = current.next[index]; | ||
} | ||
|
||
return current != null && current.end; | ||
} | ||
|
||
public boolean startsWith(String prefix) { | ||
Node current = this.node; | ||
for (char wordChar : prefix.toCharArray()) { | ||
int index = wordChar - 'a'; | ||
if (current.next[index] == null) { | ||
return false; | ||
} | ||
current = current.next[index]; | ||
} | ||
|
||
return current != null; | ||
} | ||
} | ||
|
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
|
||
public class Geegong { | ||
|
||
/** | ||
* string segmentation 을 이용해서 풀어햐한다. | ||
* dp를 이용해 segmentation 이 된 지점을 true 로 저장하면서 s 이 length 인덱스의 값이 true 로 되어있는지 체크 | ||
* Time complexity : O (N^3) 🥲 | ||
* -> 이중 루프 (N^2) * contains(substring) (N) | ||
* | ||
* Space complexity : O(N) | ||
* @param s | ||
* @param wordDict | ||
* @return | ||
*/ | ||
public boolean wordBreak(String s, List<String> wordDict) { | ||
HashSet<String> distinctWords = new HashSet<>(wordDict); | ||
|
||
// 1 = true, 0 = false | ||
int[] dp = new int[s.length() + 1]; | ||
Arrays.fill(dp, 0); | ||
dp[0] = 1; | ||
|
||
for (int rightIndex = 1; rightIndex <= s.length() ; rightIndex++) { | ||
for (int leftIndex = 0; leftIndex < rightIndex; leftIndex++) { | ||
if (dp[leftIndex] == 1 && distinctWords.contains(s.substring(leftIndex, rightIndex))) { | ||
dp[rightIndex] = 1; | ||
} | ||
} | ||
} | ||
|
||
return dp[s.length()] == 1; | ||
} | ||
|
||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Java의 경우 문자의 뺄셈 연산은 자동으로 숫자로 변경되는 건가요? 그렇다면 편리하네요!