-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagram.java
More file actions
37 lines (35 loc) · 994 Bytes
/
Anagram.java
File metadata and controls
37 lines (35 loc) · 994 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
import java.util.Arrays;
public class Anagram
{
static void isAnagram(String a, String b)
{
String x = a.replaceAll("\\s", "");
String y = b.replaceAll("\\s", "");
boolean status = true;
if (x.length() != y.length())
{
status = false;
}
else
{
char[] ArrayX = x.toLowerCase().toCharArray();
char[] ArrayY = y.toLowerCase().toCharArray();
Arrays.sort(ArrayX);
Arrays.sort(ArrayY);
status = Arrays.equals(ArrayX, ArrayY);
}
if (status)
{
System.out.println(x + " and " + y + " are anagrams");
}
else
{
System.out.println(x + " and " + y + " are not anagrams");
}
}
public static void main(String[] args)
{
isAnagram("abc", "cab");
isAnagram("game of thrones", "friends");
}
}