-
-
Notifications
You must be signed in to change notification settings - Fork 245
[윤태권] Week2 문제 풀이 #353
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
Merged
Merged
[윤태권] Week2 문제 풀이 #353
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3f815c0
valid-anagram
taekwon-dev 800725b
docs: Add explanation of time and space complexity
taekwon-dev d75317c
fix: valid-anagram
taekwon-dev 95d5f23
counting-bits
taekwon-dev ccd7b0d
fix: valid-anagram
taekwon-dev a0fc24d
construct-binary-tree-from-preorder-and-inorder-traversal with soluti…
taekwon-dev 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 hidden or 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,31 @@ | ||
/** | ||
* 시간 복잡도: O(NlogN) | ||
* - Arrays.sort() > Dual-Pivot QuickSort | ||
* | ||
* 공간 복잡도: O(N) | ||
* | ||
* 처음 문제를 보고 들었던 생각: 정렬 시켜서 같으면 anagram? | ||
* -> 아, 그러면 등장한 문자의 빈도수가 같네? | ||
* -> 결국 26 사이즈가 인풋에 영향을 받지 않으므로 공간 복잡도를 O(1)로 개선할 수 있고, | ||
* -> 시간 복잡도도 O(N)으로 개선할 수 있겠다. | ||
*/ | ||
class Solution { | ||
public boolean isAnagram(String s, String t) { | ||
if (s.length() != t.length()) return false; | ||
|
||
int[] charCount = new int[26]; | ||
|
||
for (char c : s.toCharArray()) { | ||
charCount[c - 'a']++; | ||
} | ||
|
||
for (char c : t.toCharArray()) { | ||
charCount[c - 'a']--; | ||
if (charCount[c - 'a'] < 0) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
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.
s.toCharArray()
와t.toCharArray()
에서 배열을 위한 공간을 추가로 요구하게 되기때문에charAt
을 사용하도록 변경해야 O(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.
@dev-jonghoonpark 오 그러네요! 리뷰 감사합니다 ㅎㅎ 👍