-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidAnagram.java
More file actions
31 lines (25 loc) · 888 Bytes
/
ValidAnagram.java
File metadata and controls
31 lines (25 loc) · 888 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
/**
* 242. Valid Anagram
*
* 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 theS, String theT) {
if (theS.length() != theT.length())
return false;
int[] charCount = new int[26];
// Count characters in theS
for (char c : theS.toCharArray())
charCount[c - 'a']++;
// Compare character counts in theT
for (char c : theT.toCharArray())
charCount[c - 'a']--;
// Check if any character count is non-zero
for (int count : charCount) {
if (count != 0)
return false;
}
return true;
}
}