File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .util .Arrays ;
2+ import java .util .HashMap ;
3+ import java .util .Map ;
4+
5+ class Solution {
6+ //์ ํจํ ์ ๋๊ทธ๋จ์ ์กฐ๊ฑด์ด ๋ฌด์์ผ๊น?
7+ //1. a์ b์ ์ํ๋ฒณ๋ณ ๋น๋์๊ฐ ์ ํํ ๋์ผํด์ผ ํ๋ค.
8+ //2. ๋จ a์ b์ ์ํ๋ฒณ ๊ตฌ์ฑ ์์๋ ๋ค๋ฅผ ์ ์๋ค.
9+
10+ // map ์๋ฃ๊ตฌ์กฐ๋ฅผ ํ์ฉํด์ s์ ์ํ๋ฒณ ๋น๋์๋ฅผ ์ ์ฅํ๊ณ ์ดํ t๋ฅผ ์ํํ๋ฉด์ map์ ์กด์ฌํ๋ ์ํ๋ฒณ์ผ ๊ฒฝ์ฐ
11+ // 1. ๋น๋์๋ฅผ ๊น๋๋ค. 2. ๋น๋์๊ฐ 0์ผ ๊ฒฝ์ฐ map์์ ์ญ์ ํ๋ค.
12+ public boolean isAnagramByHashMap (String s , String t ) {
13+ Map <Character , Integer > map = new HashMap <>();
14+ for (char c : s .toCharArray ()) {
15+ map .put (c , map .getOrDefault (c , 0 ) + 1 );
16+ }
17+
18+ for (char c : t .toCharArray ()) {
19+ Integer v = map .get (c );
20+
21+ if (v == null )
22+ return false ;
23+
24+ if (v - 1 == 0 ) {
25+ map .remove (c );
26+ } else
27+ map .put (c , v - 1 );
28+ }
29+ return map .size () < 1 ;
30+ }
31+
32+ // ๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ผ๋ก๋ ๋จ์ํ ๋ ๋ฌธ์์ด์ ์ ๋ ฌํ๊ณ ๋ฌธ์์ด ๋ด์ฉ ์ผ์น ๋น๊ต๋ฅผ ์ํํ๋ ๋ฐฉ๋ฒ์ด ์๋ค.
33+ // ์ด ๋ฐฉ๋ฒ์ ์ฌ์ฉํ ๋ ์ฒ์์๋ ๋ฌด์กฐ๊ฑด ์ ๋ ฌ์ ์ํํ์ผ๋,
34+ // ๋ฌธ์์ด์ ๊ฐ์๊ฐ ๋ค๋ฅด๋ค๋ฉด 1๋ฒ ์กฐ๊ฑด์ ๋ง์กฑํ์ง ๋ชปํ๋ฏ๋ก ๊ธธ์ด ๋น๊ต ๋ก์ง์ ์ ๋ ฌ ์ ์ ์ถ๊ฐํจ์ผ๋ก์ ์๊ฐ ์ฑ๋ฅ์ ๋์๋ค.
35+ public boolean isAnagram (String s , String t ) {
36+ if (s .length () != t .length ()) return false ;
37+ char [] sChars = s .toCharArray ();
38+ char [] tChars = t .toCharArray ();
39+
40+ Arrays .sort (sChars );
41+ Arrays .sort (tChars );
42+
43+ return new String (sChars ).equals (new String (tChars ));
44+ }
45+ }
You canโt perform that action at this time.
0 commit comments