-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagrams.java
More file actions
80 lines (72 loc) · 1.92 KB
/
Anagrams.java
File metadata and controls
80 lines (72 loc) · 1.92 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.util.Scanner;
public class Solution {
static boolean isAnagram(String a, String b)
{
// Complete the function
a = a.toLowerCase();
b = b.toLowerCase();
char A[] = a.toCharArray();
char B[] = b.toCharArray();
//sorting the first array..
for(int i=0;i<A.length-1;i++)
{
for(int j=0;j<A.length-i-1;j++)
{
if(A[j]>A[j+1])
{
char t = A[j];
A[j]=A[j+1];
A[j+1]=t;
}
}
}
//sorting the second array..
for(int i=0;i<B.length-1;i++)
{
for(int j=0;j<B.length-i-1;j++)
{
if(B[j]>B[j+1])
{
char t = B[j];
B[j]=B[j+1];
B[j+1]=t;
}
}
}
//checking if they both are anagrams or not..
boolean r=false;
if(A.length==B.length) //checking if the length of both the arrays are same or not..
{
int i;
for(i=0;i<A.length;i++)
{
if(A[i]==B[i])
{
continue;
}
else
{
r = false;
break;
}
}
if(i==A.length)//checking if the i has reached at the end or not...
{
r = true;
}
}
else
{
r = false;
}
return r;
}
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 Anagram" );
}
}