-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidAnagram.java
35 lines (28 loc) · 944 Bytes
/
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
/* Example 1 : Your input : s = "rat" , t = "cat"
* Output : false
*
*
* Given two strings s and t, return true if t is an anagram of s, and false otherwise.
*
*
* An Anagram is a word or phrase formed by rearranging the letters
* of a different word or phrase, typically using all the original letters exactly once. */
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 i : counts){
if( i != 0 ){
return false; /* We increment each letter we see in the s string once.
We decrease it in the t string once. That's why, 'i' must be 0. */
}
}
return true;
}
}