-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValid_Anagram.java
More file actions
35 lines (30 loc) · 878 Bytes
/
Copy pathValid_Anagram.java
File metadata and controls
35 lines (30 loc) · 878 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
import java.util.HashMap;
public class Valid_Anagram{
public static boolean isAnagram(String s, String t){
HashMap<Character, Integer> map = new HashMap<>();
for( int i=0; i<s.length();i++){
char ch = s.charAt(i);
map.put(ch, map.getOrDefault(ch, 0)+1);
}
for(int i=0;i<t.length();i++){
char ch = t.charAt(i);
if(map.get(ch) != null){
if(map.get(ch) == 1){
map.remove(ch);
}
else{
map.put(ch,map.get(ch) -1);
}
}
else{
return false;
}
}
return map.isEmpty();
}
public static void main(String[] args) {
String s = "keen";
String t = "neek";
System.out.println(isAnagram(s, t));
}
}