-
Notifications
You must be signed in to change notification settings - Fork 71
/
Anagram.java
30 lines (27 loc) · 954 Bytes
/
Anagram.java
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
// import Arrays to implement sort
import java.util.Arrays;
import java.io.*;
public class Anagram {
static boolean isAnagram(String str1, String str2) {
String s1 = str1.replaceAll("\\s", "");
String s2 = str2.replaceAll("\\s", "");
boolean status = true;
if (s1.length() != s2.length()) {
return false;
}
else {
char[] ArrayS1 = s1.toLowerCase().toCharArray();
char[] ArrayS2 = s2.toLowerCase().toCharArray();
Arrays.sort(ArrayS1);
Arrays.sort(ArrayS2);
return Arrays.equals(ArrayS1, ArrayS2);
}
}
public static void main(String[] args) {
if(isAnagram("Keep", "Peek"))
System.out.print("Case 1 " + "are anagrams");
else
System.out.println("Are not anagrams");
//isAnagram("Mother In Law", "Hitler Woman");
}
}