-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagramChecker.java
More file actions
41 lines (33 loc) · 881 Bytes
/
AnagramChecker.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
**/
class AnagramChecker
{
public boolean isAnagram(String s, String t)
{
int nS = s.length();
int tS = t.length();
int[] counts = new int[26];
if(nS != tS)
{
return false;
}
for(int i = 0; i < nS; i++)
{
counts[s.charAt(i) - 'a']++;
counts[t.charAt(i) - 'a']--;
}
for(int j = 0; j < 26; j++)
{
if(counts[j] != 0)
{
return false;
}
}
return true;
}
}