-
Notifications
You must be signed in to change notification settings - Fork 3
/
_0242_ValidAnagram.java
80 lines (75 loc) · 2.17 KB
/
_0242_ValidAnagram.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.diguage.algorithm.leetcode;
import java.util.Arrays;
import java.util.Objects;
/**
* = 242. Valid Anagram
*
* Given two strings `s` and `t` , write a function to determine if `t` is an anagram of `s`.
*
* .Example 1:
* [source]
* ----
* Input: s = "anagram", t = "nagaram"
* Output: true
* ----
*
* .Example 2:
* [source]
* ----
* Input: s = "rat", t = "car"
* Output: 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?
*
* @author D瓜哥, https://www.diguage.com/
* @since 2020-01-10 23:48
*/
public class _0242_ValidAnagram {
/**
* Runtime: 3 ms, faster than 93.97% of Java online submissions for Valid Anagram.
*
* Memory Usage: 36.2 MB, less than 98.06% of Java online submissions for Valid Anagram.
*/
public boolean isAnagram(String s, String t) {
if (Objects.isNull(s) && Objects.isNull(t)) {
return true;
}
if (Objects.isNull(s) || Objects.isNull(t)) {
return false;
}
int[] sChartCount = new int[26];
for (char c : s.toCharArray()) {
sChartCount[c - 'a']++;
}
int[] tChartCount = new int[26];
for (char c : t.toCharArray()) {
tChartCount[c - 'a']++;
}
return Arrays.equals(sChartCount, tChartCount);
}
/**
* Runtime: 3 ms, faster than 93.97% of Java online submissions for Valid Anagram.
*
* Memory Usage: 37.9 MB, less than 64.51% of Java online submissions for Valid Anagram.
*/
public boolean isAnagramSort(String s, String t) {
char[] sChars = s.toCharArray();
Arrays.sort(sChars);
char[] tChars = t.toCharArray();
Arrays.sort(tChars);
return Arrays.equals(sChars, tChars);
}
public static void main(String[] args) {
_0242_ValidAnagram solution = new _0242_ValidAnagram();
boolean r1 = solution.isAnagram("anagram", "nagaram");
System.out.println(r1);
boolean r2 = solution.isAnagram("rat", "car");
System.out.println(r2);
}
}