Skip to content

Commit 62f6663

Browse files
committedDec 18, 2024·
valid anagram solution
·
v4.13.0v3.2.0
1 parent e0d7d23 commit 62f6663

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
 

‎valid-anagram/5YoonCheol.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.HashMap;
2+
3+
public class Solution {
4+
public boolean isAnagram(String s, String t) {
5+
//HashMap 자료구조를 통해 아나그램 여부 판별
6+
//s,t 문자열에서 문자인 것만 HashMap에 넣어준다.
7+
//이때 중복인 경우 value 값을 1씩 증가시킨다.
8+
//Character 타입의 개수가 맞지 않으면 false
9+
//두 개의 Map이 서로 동일하면 아나그램이다. -> true
10+
//그 외의 경우는 Character의 개수가 맞지 않기 때문에 false
11+
HashMap<Character, Integer> sMap = new HashMap<>();
12+
HashMap<Character, Integer> tMap = new HashMap<>();
13+
for (Character c : s.toCharArray()) {
14+
if (Character.isLetter(c)) {
15+
sMap.put(c, sMap.getOrDefault(c, 0) + 1);
16+
}
17+
}
18+
19+
for (Character c : t.toCharArray()) {
20+
if (Character.isLetter(c)) {
21+
tMap.put(c, tMap.getOrDefault(c, 0) + 1);
22+
}
23+
}
24+
25+
if (sMap.size() != tMap.size()) return false;
26+
else if (sMap.equals(tMap)) return true;
27+
else return false;
28+
}
29+
}

0 commit comments

Comments
 (0)
Please sign in to comment.