-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCheckForAnagram.java
More file actions
42 lines (37 loc) · 902 Bytes
/
CheckForAnagram.java
File metadata and controls
42 lines (37 loc) · 902 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
42
import java.util.Arrays;
/**
*
* Ashish Patel
* e: ashishsushilPatel@gmail.com
* w: https://ashish.me
*
*/
class CheckForAnagram {
static final int CHAR=256;
static boolean areAnagram(String s1, String s2){
if(s1.length() != s2.length()){
return false;
}
int[] count = new int[CHAR];
for(int i = 0 ;i < s1.length(); i++){
count[s1.charAt(i)]++;
count[s2.charAt(i)]--;
}
for(int i = 0;i < s1.length();i++){
if(count[i] != 0){
return false;
}
}
return true;
}
public static void main(String[] args){
String str1 = "abaac";
String str2 = "aacba";
if (areAnagram(str1, str2))
System.out.println("The two strings are"
+ " anagram of each other");
else
System.out.println("The two strings are not"
+ " anagram of each other");
}
}