-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagram 2
More file actions
55 lines (47 loc) · 1.54 KB
/
Anagram 2
File metadata and controls
55 lines (47 loc) · 1.54 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
47
48
49
50
51
52
53
54
55
//To chech if the given strings are anagrams (contains same counts of every letter)
//WIthout using Array
import java.util.Scanner;
public class Solution {
static boolean isAnagram(String a, String b) {
a=a.toLowerCase();
b=b.toLowerCase();
if(a.length()!=b.length())
return false;
String as[]=new String[a.length()];
String bs[]=new String[b.length()];
for(int i=0; i<a.length(); i++){
as[i]=a.substring(i,i+1);
bs[i]=b.substring(i,i+1);
}
for(int i=0; i<as.length-1; i++){
for(int j=i+1; j<as.length; j++){
if(as[i].compareTo(as[j])>=0){
String temp=as[i];
as[i]=as[j];
as[j]=temp;
}
if(bs[i].compareTo(bs[j])>=0){
String temp=bs[i];
bs[i]=bs[j];
bs[j]=temp;
}
}
}
boolean c=true;
for(int i=0; i<as.length; i++){
//System.out.println(as[i]+bs[i]);
if(!(as[i].equals(bs[i]))){
return false;
}
}
return c;
}
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" );
}
}