-
Notifications
You must be signed in to change notification settings - Fork 122
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
[jaejeong1] WEEK 02 Solutions #340
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d21d14e
valid anagram solution
wad-jangjaejeong b82a897
counting bits solution
wad-jangjaejeong 0fe2d97
line break
wad-jangjaejeong 933b354
encode and decode strings solution
wad-jangjaejeong cade7d3
encode and decode strings solution
wad-jangjaejeong acf5f5a
encode and decode strings solution
wad-jangjaejeong 5991bb9
피드백 반영
wad-jangjaejeong 012e60c
Merge remote-tracking branch 'origin/main'
wad-jangjaejeong 93deaff
피드백 반영
wad-jangjaejeong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,26 @@ | ||
class SolutionCountingBits { | ||
public int[] countBits(int n) { | ||
// 0 ~ n 까지의 수를 이진수로 변환한다음, 1의 개수를 카운트해 배열로 반환 | ||
// 홀수/짝수 여부를 나눠서 1의 개수를 구함 | ||
// 홀수: 이전 값 + 1, 짝수: i / 2의 1의 개수와 같은 값 | ||
// 시간복잡도: O(N), 공간복잡도: O(N) | ||
|
||
int[] countingBits = new int[n + 1]; | ||
countingBits[0] = 0; | ||
|
||
for (int i=1; i<=n; i++) { | ||
if (isOddNumber(i)) { | ||
countingBits[i] = countingBits[i - 1] + 1; | ||
} else { | ||
countingBits[i] = countingBits[i / 2]; | ||
} | ||
} | ||
|
||
return countingBits; | ||
} | ||
|
||
// 시간복잡도: O(1) | ||
private boolean isOddNumber(int n) { | ||
return n % 2 == 1; | ||
} | ||
} |
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,45 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
class SolutionEncodeAndDecodeStrings { | ||
|
||
private static final char SEPERATOR = '/'; | ||
/* | ||
* @param strs: a list of strings | ||
* @return: encodes a list of strings to a single string. | ||
*/ | ||
// 풀이: 문자열 길이를 구분자와 함께 encode해 decode시 문자열 길이를 참고할 수 있도록 한다 | ||
// 시간복잡도: O(N), 공간복잡도: O(1) | ||
public String encode(List<String> strs) { | ||
// write your code here | ||
var answer = new StringBuilder(); | ||
|
||
for (var str : strs) { | ||
answer.append(SEPERATOR) | ||
.append(str.length()) | ||
.append(str); | ||
} | ||
|
||
return answer.toString(); | ||
} | ||
|
||
/* | ||
* @param str: A string | ||
* @return: decodes a single string to a list of strings | ||
*/ | ||
// 풀이: 문자열 길이를 구분자와 함께 encode해 decode시 문자열 길이를 참고할 수 있도록 한다 | ||
// 시간복잡도: O(N), 공간복잡도: O(N) | ||
public List<String> decode(String str) { | ||
// write your code here | ||
List<String> answer = new ArrayList<>(); | ||
var i = 0; | ||
while (i < str.length()) { | ||
var seperatorIdx = str.indexOf(SEPERATOR, i) + 1; | ||
var size = Integer.parseInt(str.substring(seperatorIdx, seperatorIdx + 1)); | ||
i = seperatorIdx + size + 1; | ||
answer.add(str.substring(seperatorIdx + 1, i)); | ||
} | ||
|
||
return answer; | ||
} | ||
} |
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,42 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
class SolutionValidAnagram { | ||
public boolean isAnagram(String s, String t) { | ||
// 풀이: 해시맵을 사용해 s와 t의 문자 별 빈도수를 저장한다 | ||
// 두 빈도수의 모든 키와 값이 같고, 크기가 같은지 비교한다. | ||
// 다르다면 false를 반환, 모두 같다면 true를 반환한다. | ||
// 시간복잡도: O(N), 공간복잡도: O(1) | ||
|
||
Map<Character, Integer> sAnagram = createAnagramMap(s); | ||
Map<Character, Integer> tAnagram = createAnagramMap(t); | ||
|
||
// 두 해시맵의 크기가 같은지 확인 | ||
if (sAnagram.size() != tAnagram.size()) { | ||
return false; | ||
} | ||
|
||
// sAnagram과 tAnagram의 모든 키와 값을 비교 | ||
for (Map.Entry<Character, Integer> entry : sAnagram.entrySet()) { | ||
var key = entry.getKey(); | ||
int value = entry.getValue(); | ||
|
||
// tAnagram에 key가 존재하지 않거나, 그에 대응하는 value가 다르면 false 반환 | ||
if (!tAnagram.containsKey(key) || !tAnagram.get(key).equals(value)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private Map<Character, Integer> createAnagramMap(String text) { | ||
Map<Character, Integer> anaGramMap = new HashMap<>(); | ||
|
||
for (var c: text.toCharArray()) { | ||
anaGramMap.put(c, anaGramMap.getOrDefault(c, 0) + 1); | ||
} | ||
|
||
return anaGramMap; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
map을 사용하지 않고 주어진 s, t를 Charactor Array로 변환 후 정렬해서 각 인덱스를 비교하는 방법도 추천 드립니다!
대신 정렬을 사용하면 O(n log n)이 되어서, TC적인 면에서는 Map 사용이 더 효율적인것 같아요 👍
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.
@TonyKim9401 sorting 을 사용하지 않고자 map을 사용했는데, 좋은 대안 제시해주셔서 감사합니다! 다른 문제 풀이 시 참고하겠습니다 :)