-
Notifications
You must be signed in to change notification settings - Fork 2
/
Que242.java
46 lines (31 loc) · 1.01 KB
/
Que242.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
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Map;
import java.util.TreeMap;
/**
* @author: hyl
* @date: 2019/08/02
**/
public class Que242 {
public boolean isAnagram(String s, String t) {
char[] sArray = s.toCharArray();
char[] tArray = t.toCharArray();
Arrays.sort(s.toCharArray());
Arrays.sort(t.toCharArray());
return Arrays.toString(sArray).equals(Arrays.toString(tArray));
}
public boolean isAnagram1(String s, String t) {
Map<Character,Integer> sMap = new TreeMap<Character, Integer>();
Map<Character,Integer> tMap = new TreeMap<Character, Integer>();
for (char c : s.toCharArray()) {
sMap.put(c,sMap.getOrDefault(c,0)+1);
}
for (char c : t.toCharArray()) {
tMap.put(c,tMap.getOrDefault(c,0)+1);
}
return sMap.entrySet().equals(tMap.entrySet());
}
public static void main(String[] args) {
new Que242().isAnagram1("ab","a") ;
}
}