File tree Expand file tree Collapse file tree 1 file changed +10
-13
lines changed Expand file tree Collapse file tree 1 file changed +10
-13
lines changed Original file line number Diff line number Diff line change 1
1
/**
2
- * 시간 복잡도: O(NlogN)
3
- * - Arrays.sort() > Dual-Pivot QuickSort
4
- *
5
- * 공간 복잡도: O(N)
6
- *
7
2
* 처음 문제를 보고 들었던 생각: 정렬 시켜서 같으면 anagram?
8
- * -> 아, 그러면 등장한 문자의 빈도수가 같네?
9
- * -> 결국 26 사이즈가 인풋에 영향을 받지 않으므로 공간 복잡도를 O(1)로 개선할 수 있고,
10
- * -> 시간 복잡도도 O(N)으로 개선할 수 있겠다.
3
+ * 근데, 정렬 시켜서 같다면, 결국 문자열을 구성하는 각 문자의 빈도수가 같다.
4
+ *
5
+ * 시간 복잡도: O(N)
6
+ * 공간 복잡도: O(1)
11
7
*/
12
8
class Solution {
13
9
public boolean isAnagram (String s , String t ) {
14
10
if (s .length () != t .length ()) return false ;
15
11
16
12
int [] charCount = new int [26 ];
17
13
18
- for (char c : s . toCharArray () ) {
19
- charCount [c - 'a' ]++;
14
+ for (int i = 0 ; i < s . length (); i ++ ) {
15
+ charCount [s . charAt ( i ) - 'a' ]++;
20
16
}
21
17
22
- for (char c : t .toCharArray ()) {
23
- charCount [c - 'a' ]--;
24
- if (charCount [c - 'a' ] < 0 ) {
18
+ for (int i = 0 ; i < t .length (); i ++) {
19
+ int index = t .charAt (i ) - 'a' ;
20
+ charCount [index ]--;
21
+ if (charCount [index ] < 0 ) {
25
22
return false ;
26
23
}
27
24
}
You can’t perform that action at this time.
0 commit comments