forked from riya2001-cloud/java-hacktober
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob242_valid_Anagram.java
37 lines (27 loc) · 1.01 KB
/
prob242_valid_Anagram.java
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
// Take two hashmap to store the character and its frequency.
// At last compare both of them if they are same then return true else false.
class Solution {
public boolean isAnagram(String s, String t) {
HashMap<Character, Integer> stores = new HashMap<>();
HashMap<Character, Integer> storet = new HashMap<>();
char chars_s[] = s.toCharArray();
char chars_t[] = t.toCharArray();
// inserting values into hashmap stores
for(char ch : chars_s){
if(stores.containsKey(ch)){
stores.put(ch, stores.get(ch)+1);
}
else
stores.put(ch, 1);
}
// inserting values into hashmap storet
for(char ch : chars_t){
if(storet.containsKey(ch)){
storet.put(ch, storet.get(ch)+1);
}
else
storet.put(ch, 1);
}
return stores.equals(storet);
}
}