-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprblm242.java
More file actions
56 lines (49 loc) · 1.55 KB
/
prblm242.java
File metadata and controls
56 lines (49 loc) · 1.55 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
import java.util.*;
public class prblm242 {
public static void main(String[] args) {
String s = "anagram";
String t = "nagaram";
System.out.println(isAnagram(s, t));
String s1 = "rat";
String t1 = "car";
System.out.println(isAnagram(s1, t1));
String s2 = "ab";
String t2 = "a";
System.out.println(isAnagram2(s2, t2));
String s3 = "aacc";
String t3 = "ccac";
System.out.println(isAnagram2(s3, t3));
}
public static boolean isAnagram(String s, String t) {
if(s.length() != t.length()){
return false;
}
Map<Character, Integer> sMap = new HashMap<>();
Map<Character, Integer> tMap = new HashMap<>();
for (Character ch : s.toCharArray()) {
sMap.put(ch, sMap.getOrDefault(ch, 0) + 1);
}
// System.out.println(sMap);
for (Character ch : t.toCharArray()) {
tMap.put(ch, tMap.getOrDefault(ch, 0) + 1);
}
// System.out.println(tMap);
return sMap.equals(tMap);
}
public static boolean isAnagram2(String s, String t) {
if(s.length() != t.length()){
return false;
}
int[] count = new int[26];
for (int i = 0; i < s.length(); i++) {
count[s.charAt(i) - 'a']++;
count[t.charAt(i) - 'a']--;
}
for (int i = 0; i < count.length; i++) {
if (count[i] != 0) {
return false;
}
}
return true;
}
}