File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed
Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .util .HashMap ;
2+ import java .util .Map ;
3+
4+ class Solution {
5+ public boolean isAnagram (String s , String t ) {
6+
7+ // s ์์ ๋ฌธ์๋ค์ด t ์๋ ๋์ผํ ํ์๋ก ๋ฑ์ฅํ๋ ์ง ํ์ธ
8+ if (s .length () != t .length ()) return false ; // ๋ ๋ฌธ์์ด์ ๊ธธ์ด๊ฐ ๋ค๋ฅด๋ค๋ฉด ์๋๊ทธ๋จ์ด ์๋๋ค.
9+
10+ // ๋ฌธ์๋ณ ํ์ ์ ์ฅ map
11+ Map <Character , Integer > sAlphabetCountMap = new HashMap <>();
12+ for (char c : s .toCharArray ()) { // ์๊ฐ๋ณต์ก๋: O(n)
13+ sAlphabetCountMap .put (c , sAlphabetCountMap .getOrDefault (c , 0 ) + 1 );
14+ }
15+
16+ for (char c : t .toCharArray ()) { // ์๊ฐ๋ณต์ก๋: O(n)
17+ if (!sAlphabetCountMap .containsKey (c )) return false ; // s์ t๊ฐ ๊ฐ์ง ๋ฌธ์์ด์ด ์๋ค๋ฉด ์๋๊ทธ๋จ์ด ์๋๋ค.
18+
19+ int count = sAlphabetCountMap .get (c ) - 1 ;
20+ if (count == 0 ) sAlphabetCountMap .remove (c );
21+ else sAlphabetCountMap .put (c , count );
22+ }
23+
24+ // ๋ชจ๋ ๋ฌธ์๊ฐ ์ผ์นํ๋ฉด ํด์๋งต์ด ๋น์ด ์์ด์ผ ํจ
25+ return sAlphabetCountMap .isEmpty ();
26+ }
27+ }
You canโt perform that action at this time.
0 commit comments