-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagram
More file actions
39 lines (33 loc) · 1.13 KB
/
Copy pathAnagram
File metadata and controls
39 lines (33 loc) · 1.13 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
39
Two strings, and , are called anagrams if they contain all the same characters in the same frequencies. For this challenge, the test is not case-sensitive.
For example, the anagrams of CAT are CAT, ACT, tac, TCA, aTC, and CtA.
Solution:
import java.io.*;
import java.util.*;
public class Solution {
public static String IsAnagram(String s1,String s2){
s1=s1.replaceAll("\\s", "").toLowerCase();
s2=s2.replaceAll("\\s", "").toLowerCase();
if(s1.length() != s2.length()){
return "Not Anagrams";
}
int[] letter=new int[26];
for(int i=0;i<s1.length();i++){
int index1 = s1.charAt(i) - 'a';
int index2 = s2.charAt(i) - 'a';
letter[index1]++;
letter[index2]--;
}
for(int count:letter){
if(count!=0){
return "Not Anagrams";
}
}
return "Anagrams";
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s1=sc.nextLine();
String s2=sc.nextLine();
System.out.println(IsAnagram(s1,s2));
}
}