-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathValidAnagram242.java
64 lines (59 loc) · 1.67 KB
/
ValidAnagram242.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* Given two strings s and t, write a function to determine if t is an
* anagram of s.
*
* For example,
* s = "anagram", t = "nagaram", return true.
* s = "rat", t = "car", return false.
*
* Note:
* You may assume the string contains only lowercase alphabets.
*
* Follow up:
* What if the inputs contain unicode characters? How would you adapt your
* solution to such case?
*/
public class ValidAnagram242 {
public boolean isAnagram(String s, String t) {
int[] cs = new int[26];
for (int i=0; i<s.length(); i++) cs[s.charAt(i)-'a']++;
int[] ct = new int[26];
for (int i=0; i<t.length(); i++) ct[t.charAt(i)-'a']++;
for (int i=0; i<26; i++) {
if (cs[i] != ct[i]) return false;
}
return true;
}
/**
* https://leetcode.com/problems/valid-anagram/solution/
*/
public boolean isAnagram2(String s, String t) {
if (s.length() != t.length()) {
return false;
}
char[] str1 = s.toCharArray();
char[] str2 = t.toCharArray();
Arrays.sort(str1);
Arrays.sort(str2);
return Arrays.equals(str1, str2);
}
/**
* https://leetcode.com/problems/valid-anagram/solution/
*/
public boolean isAnagram3(String s, String t) {
if (s.length() != t.length()) {
return false;
}
int[] counter = new int[26];
for (int i = 0; i < s.length(); i++) {
counter[s.charAt(i) - 'a']++;
counter[t.charAt(i) - 'a']--;
}
for (int count : counter) {
if (count != 0) {
return false;
}
}
return true;
}
}