File tree Expand file tree Collapse file tree 1 file changed +18
-5
lines changed Expand file tree Collapse file tree 1 file changed +18
-5
lines changed Original file line number Diff line number Diff line change 4
4
*
5
5
* ๊ณต๊ฐ ๋ณต์ก๋: O(N)
6
6
*
7
+ * ์ฒ์ ๋ฌธ์ ๋ฅผ ๋ณด๊ณ ๋ค์๋ ์๊ฐ: ์ ๋ ฌ ์์ผ์ ๊ฐ์ผ๋ฉด anagram?
8
+ * -> ์, ๊ทธ๋ฌ๋ฉด ๋ฑ์ฅํ ๋ฌธ์์ ๋น๋์๊ฐ ๊ฐ๋ค?
9
+ * -> ๊ฒฐ๊ตญ 26 ์ฌ์ด์ฆ๊ฐ ์ธํ์ ์ํฅ์ ๋ฐ์ง ์์ผ๋ฏ๋ก ๊ณต๊ฐ ๋ณต์ก๋๋ฅผ O(1)๋ก ๊ฐ์ ํ ์ ์๊ณ ,
10
+ * -> ์๊ฐ ๋ณต์ก๋๋ O(N)์ผ๋ก ๊ฐ์ ํ ์ ์๊ฒ ๋ค.
7
11
*/
8
12
class Solution {
9
13
public boolean isAnagram (String s , String t ) {
10
- char [] sChars = s .toCharArray ();
11
- char [] tChars = t .toCharArray ();
14
+ if (s .length () != t .length ()) return false ;
12
15
13
- Arrays .sort (sChars );
14
- Arrays .sort (tChars );
16
+ int [] charCount = new int [26 ];
15
17
16
- return Arrays .equals (sChars , tChars );
18
+ for (char c : s .toCharArray ()) {
19
+ charCount [c - 'a' ]++;
20
+ }
21
+
22
+ for (char c : t .toCharArray ()) {
23
+ charCount [c - 'a' ]--;
24
+ if (charCount [c - 'a' ] < 0 ) {
25
+ return false ;
26
+ }
27
+ }
28
+
29
+ return true ;
17
30
}
18
31
}
You canโt perform that action at this time.
0 commit comments