-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaAnagrams.java
More file actions
35 lines (31 loc) · 1.13 KB
/
JavaAnagrams.java
File metadata and controls
35 lines (31 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
import java.io.*;
import java.util.*;
public class JavaAnagrams {
static boolean isAnagram(String a, String b) {
//Removing all white spaces from a and b
String s1 = a.replaceAll("\\s", "");
String s2 = b.replaceAll("\\s", "");
//If the length of the two strings is not equal now then they are not anagrams.
if (a.length()!=b.length()){
return false;
}
//Changing the case of characters of both copyOfs1 and copyOfs2 and converting them to char array
char[] s1Array = s1.toLowerCase().toCharArray();
char[] s2Array = s2.toLowerCase().toCharArray();
//Sorting both s1Array and s2Array
Arrays.sort(s1Array);
Arrays.sort(s2Array);
if (Arrays.equals(s1Array, s2Array)){
return true;
}
return false;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String a = scan.next();
String b = scan.next();
scan.close();
boolean ret = isAnagram(a, b);
System.out.println( (ret) ? "Anagrams" : "Not Anagrams" );
}
}