-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidAngramTwo.java
61 lines (42 loc) · 1.21 KB
/
ValidAngramTwo.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package abc;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
public class ValidAngramTwo {
public boolean isAnagram(String s, String t) {
if(s.length() == t.length()) {
String []s1 = s.split("");
String []s2 = t.split("");
Map<Character, Integer>m1 = new HashMap<Character, Integer>();
Map<Character, Integer>m2 = new HashMap<Character, Integer>();
for(int i=0; i<s.length(); i++) {
if(m1.containsKey(s1[i])) {
m1.put(s1[i].charAt(0), m1.get(s1[i])+1);
}
else
m1.put(s1[i].charAt(0), 1);
if(m2.containsKey(s2[i])) {
m2.put(s2[i].charAt(0), m2.get(s2[i])+1);
}
else
m2.put(s2[i].charAt(0), 1);
}
for(char ch : m1.keySet()) {
if(!(m1.get(ch).equals(m2.get(ch))))
return false;
}
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
String t = sc.next();
ValidAngramTwo v =new ValidAngramTwo();
boolean b = v.isAnagram(s, t);
System.out.println(b);
}
}