-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidAnagram.java
More file actions
52 lines (47 loc) · 1.31 KB
/
ValidAnagram.java
File metadata and controls
52 lines (47 loc) · 1.31 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
//给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
//
// 示例 1:
//
// 输入: s = "anagram", t = "nagaram"
//输出: true
//
//
// 示例 2:
//
// 输入: s = "rat", t = "car"
//输出: false
//
// 说明:
//你可以假设字符串只包含小写字母。
//
// 进阶:
//如果输入字符串包含 unicode 字符怎么办?你能否调整你的解法来应对这种情况?
// Related Topics 排序 哈希表
// 👍 257 👎 0
package leetcode.editor.cn;
/**
* [242]有效的字母异位词
*/
public class ValidAnagram {
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public boolean isAnagram(String s, String t) {
if(s.length() != t.length())
return false;
int[] map = new int[26];
for(int i = 0; i < s.length(); i++) {
map[s.charAt(i) - 'a'] = map[s.charAt(i) - 'a'] + 1;
map[t.charAt(i) - 'a'] = map[t.charAt(i) - 'a'] - 1;
}
for(int i = 0; i < map.length; i++) {
if(map[i] != 0)
return false;
}
return true;
}
}
//leetcode submit region end(Prohibit modification and deletion)
public static void main(String[] args) {
Solution solution = new ValidAnagram().new Solution();
}
}