Repository navigation
Expand file tree
/
Copy pathAnagram.java
More file actions
61 lines (51 loc) · 1.53 KB
/
Copy pathAnagram.java
File metadata and controls
61 lines (51 loc) · 1.53 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.HashMap;
import java.util.Map;
public class Anagram {
public static void main(String[] args) {
String s= "aabbbb";
String t= "aaaabb";
Map <Character,Integer> ss= new HashMap<>();
Map <Character,Integer> tt= new HashMap<>();
if(s.length() != t.length())
{
System.out.println("Not Anagram!");
}
else{
// hashmap of 1st string
// hashmap of 2nd string
// mapping of string 1
for(int i=0; i< s.length(); i++)
{
if(!ss.containsKey(s.charAt(i)))
{
ss.put(s.charAt(i), 1);
}
else{
ss.put(s.charAt(i), ss.get(s.charAt(i))+1);
}
}
// mapping of string 2
for(int i=0; i< t.length(); i++)
{
if(!tt.containsKey(t.charAt(i)))
{
tt.put(t.charAt(i), 1);
}
else{
tt.put(t.charAt(i), tt.get(t.charAt(i))+1);
}
}
System.out.println(ss);
System.out.println(tt);
// check if ss and tt are equal?
if(ss.equals(tt))
{
System.out.println("Yes! given string is Anagram");
}
else
{
System.out.println("No! given string is not Anagram");
}
}
}
}