-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode242.java
More file actions
31 lines (28 loc) · 873 Bytes
/
LeetCode242.java
File metadata and controls
31 lines (28 loc) · 873 Bytes
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
public class LeetCode242 {
public static void main(String[] args) {
// 输入: s = "anagram", t = "nagaram"
// 输出: true
System.out.println(new Solution242().isAnagram("anagram", "nagaram"));
// 输入: s = "rat", t = "car"
// 输出: false
System.out.println(new Solution242().isAnagram("rat", "car"));
}
}
class Solution242 {
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) {
return false;
}
int[] counts = new int[26];
for (int i = 0; i < s.length(); i++) {
counts[s.charAt(i) - 'a']++;
}
for (int i = 0; i < t.length(); i++) {
counts[t.charAt(i) - 'a']--;
if (counts[t.charAt(i) - 'a'] == -1) {
return false;
}
}
return true;
}
}