Skip to content

Commit 1c18865

Browse files
committed
feat: valid-anagram 문제 풀이
1 parent 3fb558a commit 1c18865

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

valid-anagram/haxr369.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
/**
3+
* s의 문자 개수와 r의 문자 개수가 동일한지 체크
4+
* 배열1,2에 각각 문자 개수를 저장하기
5+
* a-z까지 26개
6+
*/
7+
public boolean isAnagram(String s, String t) {
8+
/**
9+
* Runtime: 2 ms (Beats 98.15%)
10+
* Memory: 44.74 MB (Beats 38.46%)
11+
* Space Complexity: O(N)
12+
* - 26 크기의 배열 2개 사용으로 2*O(26)
13+
* > O(1)
14+
* Time Complexity: O(N)
15+
* - 문자열 s와 t의 문자를 하나하나 탐색 => 2*O(N)
16+
* - 두 문자 사용건수 저장 배열 탐색 => O(26)
17+
* > O(N)
18+
*/
19+
int[] scount = new int[26];
20+
int[] tcount = new int[26];
21+
22+
char A = 'a';
23+
for (char c : s.toCharArray()) {
24+
scount[c - A] += 1;
25+
}
26+
27+
for (char c : t.toCharArray()) {
28+
tcount[c - A] += 1;
29+
}
30+
31+
for (int i = 0; i < 26; i++) {
32+
if (scount[i] != tcount[i]) {
33+
return false;
34+
}
35+
}
36+
37+
return true;
38+
39+
}
40+
}

0 commit comments

Comments
 (0)