Skip to content

Commit 481adc8

Browse files
committed
valid anagram solution
1 parent eae0c5a commit 481adc8

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

valid-anagram/ohgyulim.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
/* 시간 복잡도: O(N)
3+
* - for 루프: O(N)
4+
*
5+
* 공간 복잡도: O(52)
6+
*/
7+
public boolean isAnagram(String s, String t) {
8+
if (s.length() != t.length()) return false;
9+
10+
int[] sCounts = new int[26];
11+
int[] tCounts = new int[26];
12+
for (int i = 0; i < s.length(); i++) {
13+
sCounts[s.charAt(i) - 'a'] += 1;
14+
tCounts[t.charAt(i) - 'a'] += 1;
15+
}
16+
17+
for (int i = 0; i < 26; i++) {
18+
if (sCounts[i] != tCounts[i]) return false;
19+
}
20+
21+
return true;
22+
}
23+
}

0 commit comments

Comments
 (0)