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 44 *
55 * ๊ณต๊ฐ ๋ณต์ก๋: O(N)
66 *
7+ * ์ฒ์ ๋ฌธ์ ๋ฅผ ๋ณด๊ณ ๋ค์๋ ์๊ฐ: ์ ๋ ฌ ์์ผ์ ๊ฐ์ผ๋ฉด anagram?
8+ * -> ์, ๊ทธ๋ฌ๋ฉด ๋ฑ์ฅํ ๋ฌธ์์ ๋น๋์๊ฐ ๊ฐ๋ค?
9+ * -> ๊ฒฐ๊ตญ 26 ์ฌ์ด์ฆ๊ฐ ์ธํ์ ์ํฅ์ ๋ฐ์ง ์์ผ๋ฏ๋ก ๊ณต๊ฐ ๋ณต์ก๋๋ฅผ O(1)๋ก ๊ฐ์ ํ ์ ์๊ณ ,
10+ * -> ์๊ฐ ๋ณต์ก๋๋ O(N)์ผ๋ก ๊ฐ์ ํ ์ ์๊ฒ ๋ค.
711 */
812class Solution {
913 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 ;
1215
13- Arrays .sort (sChars );
14- Arrays .sort (tChars );
16+ int [] charCount = new int [26 ];
1517
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 ;
1730 }
1831}
You canโt perform that action at this time.
0 commit comments