-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidAnagram.java
52 lines (49 loc) · 1.67 KB
/
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
package string;
/**
* Created by Yang on 2017/9/29.
************************************************************************************************
* 242. Valid Anagram
* https://leetcode.com/problems/valid-anagram/
* 变位词(异序词):438. Find All Anagrams in a String
************************************************************************************************
* Given two strings s and t, write a function to determine if t is an anagram of s.
*
* Example 1:
* Input: s = "anagram", t = "nagaram"
* Output: true
*
* Example 2:
* 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?
* --- Use a HashMap
************************************************************************************************
*/
public class ValidAnagram {
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']++;
counts[t.charAt(i) - 'a']--;
}
for(int count : counts) {
if(count != 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
ValidAnagram validAnagram = new ValidAnagram();
System.out.println(validAnagram.isAnagram("anagram", "nagaram") + " <---> true");
System.out.println(validAnagram.isAnagram("rat", "car") + " <---> false");
}
}