Skip to content

Commit b94b78b

Browse files
committed
add 242
1 parent 6d1898c commit b94b78b

File tree

3 files changed

+109
-1
lines changed

3 files changed

+109
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
| 344 | [Reverse String][344] |
3535
| 7 | [Reverse Integer][007] |
3636
| 387 | [First Unique Character in a String][387] |
37-
37+
| 242 | [Valid Anagram][242] |
3838

3939
**其他**
4040

@@ -63,3 +63,4 @@
6363
[344]: https://github.com/andavid/leetcode-java/blob/master/note/344/README.md
6464
[007]: https://github.com/andavid/leetcode-java/blob/master/note/007/README.md
6565
[387]: https://github.com/andavid/leetcode-java/blob/master/note/387/README.md
66+
[242]: https://github.com/andavid/leetcode-java/blob/master/note/242/README.md

note/242/README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# [Valid Anagram][title]
2+
3+
## Description
4+
5+
Given two strings s and t, write a function to determine if t is an anagram of s.
6+
7+
For example,
8+
s = "anagram", t = "nagaram", return true.
9+
s = "rat", t = "car", return false.
10+
11+
**Note:**
12+
You may assume the string contains only lowercase alphabets.
13+
14+
**Follow up:**
15+
What if the inputs contain unicode characters? How would you adapt your solution to such case?
16+
17+
## 思路一
18+
题意是判断两个字符串是否是同字母异序词,即由相同字母变换顺序后组成的新词。
19+
首先判断长度是否一致,如果不一致的话肯定不是。
20+
然后将字符串转成字符数组,对字符数组进行排序,比较两个排序后的字符数组是否完全一致。
21+
22+
## [完整代码][src]
23+
24+
```java
25+
class Solution {
26+
public boolean isAnagram(String s, String t) {
27+
if (s == null || t == null || s.length() != t.length()) {
28+
return false;
29+
}
30+
char[] chs = s.toCharArray();
31+
char[] cht = t.toCharArray();
32+
Arrays.sort(chs);
33+
Arrays.sort(cht);
34+
return (new String(chs)).equals(new String(cht));
35+
}
36+
}
37+
```
38+
39+
## 思路二
40+
由于题目中字符串只包含字母,因此可以使用一个数组记录26个字母出现的次数,遍历第一个字符串时次数加1,遍历第二个字符串时出现次数减1,一旦发现某个字符出现次数小于等于0时,说明该字符要么是从未出现过的字符,要么是出现次数超过第一个字符串,肯定不是同字母异序词。
41+
42+
## [完整代码][src]
43+
44+
```java
45+
class Solution {
46+
public boolean isAnagram(String s, String t) {
47+
if (s == null || t == null || s.length() != t.length()) {
48+
return false;
49+
}
50+
51+
int[] count = new int[26];
52+
53+
for (char ch : s.toCharArray()) {
54+
count[ch - 'a']++;
55+
}
56+
57+
for (char ch : t.toCharArray()) {
58+
if (count[ch - 'a'] <= 0) {
59+
return false;
60+
}
61+
count[ch - 'a']--;
62+
}
63+
64+
return true;
65+
}
66+
}
67+
```
68+
69+
[title]: https://leetcode.com/problems/valid-anagram
70+
[src]: https://github.com/andavid/leetcode-java/blob/master/src/com/andavid/leetcode/_242/Solution.java
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public boolean isAnagram1(String s, String t) {
5+
if (s == null || t == null || s.length() != t.length()) {
6+
return false;
7+
}
8+
char[] chs = s.toCharArray();
9+
char[] cht = t.toCharArray();
10+
Arrays.sort(chs);
11+
Arrays.sort(cht);
12+
return (new String(chs)).equals(new String(cht));
13+
}
14+
15+
public boolean isAnagram(String s, String t) {
16+
if (s == null || t == null || s.length() != t.length()) {
17+
return false;
18+
}
19+
int[] count = new int[26];
20+
for (char ch : s.toCharArray()) {
21+
count[ch - 'a']++;
22+
}
23+
for (char ch : t.toCharArray()) {
24+
if (count[ch - 'a'] <= 0) {
25+
return false;
26+
}
27+
count[ch - 'a']--;
28+
}
29+
return true;
30+
}
31+
32+
public static void main(String[] args) {
33+
Solution solution = new Solution();
34+
System.out.println(solution.isAnagram("anagram", "nagaram"));
35+
System.out.println(solution.isAnagram("rat", "cat"));
36+
}
37+
}

0 commit comments

Comments
 (0)