Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions valid-anagram/taekwon-dev.java
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)로 개선할 수 있고,
Copy link
Contributor

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) 으로 개선될 것 같습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dev-jonghoonpark 오 그러네요! 리뷰 감사합니다 ㅎㅎ 👍

* -> 시간 복잡도도 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;
}
}