Skip to content

Commit bdf91d1

Browse files
donghyeon95donghyeon95
authored andcommitted
feat: Valid Anagram DaleStudy#218
1 parent 5487b35 commit bdf91d1

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

valid-anagram/donghyeon95.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.util.HashMap;
2+
import java.util.HashSet;
3+
import java.util.Map;
4+
5+
class Solution {
6+
public boolean isAnagram(String s, String t) {
7+
boolean result = false;
8+
9+
char[] chars = s.toCharArray();
10+
Map<Character, Integer> counter = new HashMap<>();
11+
for (char c: chars) {
12+
counter.put(c, counter.getOrDefault(c, 0)+1);
13+
}
14+
15+
char[] tChars = t.toCharArray();
16+
for (char c: tChars) {
17+
if (!counter.containsKey(c)) return false;
18+
counter.put(c, counter.get(c)-1);
19+
20+
if (counter.get(c) == 0)
21+
counter.remove(c);
22+
}
23+
24+
return counter.isEmpty();
25+
}
26+
}
27+

0 commit comments

Comments
 (0)