-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValid_Anagram.java
More file actions
35 lines (33 loc) · 917 Bytes
/
Copy pathValid_Anagram.java
File metadata and controls
35 lines (33 loc) · 917 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
import java.util.*;
import java.util.Arrays;
import java.util.Collections;
public class Valid_Anagram {
public boolean isAnagram(String s, String t) {
if(s.length()!=t.length()){
return false;
}
int map[]=new int[26];
for(char x : s.toCharArray()){
map[x-'a']++;
}
for(char x : t.toCharArray()){
map[x-'a']--;
}
for(int i:map){
if (i!=0){
return false;
}
}
return true;
}
}
public static void main(String args[]) {
String s = "anagram";
String t = "nagaram";
if(isAnagram(s , t))
System.out.println("The two strings are"
+ " anagram of each other");
else
System.out.println("The two strings are not"
+ " anagram of each other");
}