-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagram.java
More file actions
38 lines (33 loc) · 1.22 KB
/
Copy pathAnagram.java
File metadata and controls
38 lines (33 loc) · 1.22 KB
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
import java.util.*;
/**
* Anagram
* To examine if t is a rearrangement of s, we can count occurrences of each letter in the two strings and compare them.
* Since both s and t contain only letters from a−z, a simple counter table of size 26 is suffice.
*
* Do we need two counter tables for comparison? Actually no, because we could increment the counter for each letter in s and decrement
* the counter for each letter in t, then check if the counter reaches back to zero.
*/
public class Anagram {
public static void main(String[] args) {
isAnagram("abcd", "dcab");
}
static boolean isAnagram(String s, String t){
if(s.length() != t.length()){
return false;
}
int[] counter = new int[26];
for(int i=0; i<s.length();i++){
//System.out.println(counter[s.charAt(i)-'a']++);
//System.out.println(counter[t.charAt(i)-'a']++);
counter[s.charAt(i)-'a']++;
counter[t.charAt(i)-'a']--;
}
// System.out.println(Arrays.toString(counter));
for(int count: counter){
// System.out.print(count+" ");
if(count != 0)
return false;
}
return true;
}
}