Skip to content

Commit eb56524

Browse files
author
王俊超
committed
commit
1 parent 66b3a79 commit eb56524

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

Diff for: .idea/modules.xml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: 【242】【Valid Anagram】/src/Solution.java

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
/**
5+
* @author: wangjunchao(王俊超)
6+
* @time: 2018-10-10 10:28
7+
**/
8+
public class Solution {
9+
public boolean isAnagram(String s, String t) {
10+
11+
if (s == null && t == null) {
12+
return true;
13+
} else if (s == null || t == null) {
14+
return false;
15+
}
16+
17+
Map<Character, Integer> map = new HashMap<>();
18+
19+
for (int i = 0; i < s.length(); i++) {
20+
Character c = s.charAt(i);
21+
if (map.containsKey(c)) {
22+
map.put(c, map.get(c) + 1);
23+
} else {
24+
map.put(c, 1);
25+
}
26+
}
27+
28+
for (int i = 0; i < t.length(); i++) {
29+
Character c = t.charAt(i);
30+
Integer count = map.get(c);
31+
// t有s中不包含的字符串
32+
if (count == null) {
33+
return false;
34+
} else {
35+
map.put(c, count - 1);
36+
}
37+
}
38+
39+
// map中的值有非0的说明不合法
40+
for (Integer i : map.values()) {
41+
if (i != 0) {
42+
return false;
43+
}
44+
}
45+
46+
return true;
47+
}
48+
}

Diff for: 【242】【Valid Anagram】/src/Test.java

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @author: wangjunchao(王俊超)
3+
* @time: 2018-10-10 10:35
4+
**/
5+
public class Test {
6+
public static void main(String[] args) {
7+
Solution solution = new Solution();
8+
9+
System.out.println(solution.isAnagram("anagram", "nagaram"));
10+
System.out.println(solution.isAnagram("rat", "car"));
11+
}
12+
}

0 commit comments

Comments
 (0)