- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagramChecker.java
More file actions
Latest commit
41 lines (33 loc) · 881 Bytes
/
Copy pathAnagramChecker.java
File metadata and controls
41 lines (33 loc) · 881 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
32
33
34
35
36
37
38
39
40
41
/**
* Given two strings s and t, return true if the two strings are anagrams of each other, otherwise return false.
* An anagram is a string that contains the exact same characters as another string, but the order of the characters can be different.
* Example 1:
* Input: s = "racecar", t = "carrace"
* Output: true
**/
classAnagramChecker
{
publicbooleanisAnagram(Strings, Stringt)
{
intnS = s.length();
inttS = t.length();
int[] counts = newint[26];
if(nS != tS)
{
returnfalse;
}
for(inti = 0; i < nS; i++)
{
counts[s.charAt(i) - 'a']++;
counts[t.charAt(i) - 'a']--;
}
for(intj = 0; j < 26; j++)
{
if(counts[j] != 0)
{
returnfalse;
}
}
returntrue;
}
}