-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanagramHashMap.java
More file actions
43 lines (38 loc) · 996 Bytes
/
anagramHashMap.java
File metadata and controls
43 lines (38 loc) · 996 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
41
42
43
class Solution
{
//Function is to check whether two strings are anagram of each other or not.
public static boolean isAnagram(String a,String b)
{
// Your code here
HashMap<Character, Integer> map = new HashMap<>();
int n = a.length();
int p = b.length();
if(n!=p)
return false;
for(int i=0;i<n;i++)
{
char ch = a.charAt(i);
if(map.containsKey(ch) == false)
map.put(ch,1);
else
map.put(ch,map.get(ch) + 1);
}
for(int i=0;i<p;i++)
{
char ch= b.charAt(i);
if(map.containsKey(ch) == false)
{
return false;
}
if(map.get(ch)==1)
{
map.remove(ch);
}
else
{
map.put(ch, map.get(ch) - 1);
}
}
return true;
}
}