forked from shivamkaushik12007/Encryption-Decryption
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagram.java
More file actions
46 lines (38 loc) · 1.28 KB
/
Copy pathAnagram.java
File metadata and controls
46 lines (38 loc) · 1.28 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
40
41
42
43
44
45
46
import java.util.Arrays;
import java.util.Scanner;
public class Anagram {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
String stringOne=scanner.nextLine();
String stringTwo=scanner.nextLine();
String[] arrayOne=stringOne.split("[^a-zA-Z]");
String[] arrayTwo=stringTwo.split("[^a-zA-Z]");
stringOne="";
stringTwo="";
for(int i=0;i<arrayOne.length;i++){
stringOne+=arrayOne[i];
}
for(int i=0;i<arrayTwo.length;i++){
stringTwo+=arrayTwo[i];
}
boolean isPalindrome=true;
char[] charArrayOne=stringOne.toCharArray();
char[] charArrayTwo=stringTwo.toCharArray();
Arrays.sort(charArrayOne);
Arrays.sort(charArrayTwo);
if(stringOne.length()!=stringTwo.length()){
System.out.println("Unequal Length");
}else {
for(int i=0;i<charArrayOne.length;i++){
if(charArrayOne[i]!=charArrayTwo[i]){
isPalindrome=false;
System.out.println("Not a palindrome");
break;
}
}
if(isPalindrome){
System.out.println("Is a palindrome");
}
}
}
}