-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnagram.java
48 lines (34 loc) · 1.16 KB
/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package test;
import java.util.Arrays;
import java.util.Scanner;
public class Anagram {
static boolean isAnagram(String s1, String s2)
{
char[] str1 = s1.toCharArray();
char[] str2 = s2.toCharArray();
int n1 = str1.length;
int n2 = str2.length;
if (n1 != n2)
return false;
Arrays.sort(str1);
Arrays.sort(str2);
for (int i = 0; i < n1; i++)
if (str1[i] != str2[i])
return false;
return true;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first string:");
String s1 = sc.nextLine();
System.out.print("Enter the second string:");
String s2= sc.nextLine();
if (isAnagram(s1, s2))
System.out.println("The two strings are"
+ " anagram of each other");
else
System.out.println("The two strings are not"
+ " anagram of each other");
}
}