-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
40 lines (32 loc) · 989 Bytes
/
Copy pathSolution.java
File metadata and controls
40 lines (32 loc) · 989 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package org.example.problems.valid_anagram;
import org.example.problems.SolutionInterface;
import java.util.HashMap;
import java.util.Map;
public class Solution implements SolutionInterface {
@Override
public String getName() {
return "Valid Anagram";
}
@Override
public String getUrl() {
return "https://leetcode.com/problems/valid-anagram/";
}
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) {
return false;
}
Map<Character, Integer> charmap = new HashMap<>();
for (char c: s.toCharArray()) {
int currentNum = charmap.getOrDefault(c, 0);
charmap.put(c, currentNum + 1);
}
for (char c: t.toCharArray()) {
int currentNum = charmap.getOrDefault(c, 0);
if (currentNum < 1) {
return false;
}
charmap.put(c, currentNum - 1);
}
return true;
}
}