-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnagrams.java
63 lines (51 loc) · 1.24 KB
/
Anagrams.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.util.Scanner;
public class Solution {
public static boolean isAnagram(String a,String b)
{
String c1 = a.replace(" ","").toLowerCase();
String c2 = b.replace(" ","").toLowerCase();
c1 = c1.toLowerCase();
c2 = c2.toLowerCase();
boolean status = true;
if(a.length()!=b.length())
{
status = false;
}
else
{
char[] ch1 = a.toCharArray();
for (char c : ch1)
{
int index = c2.indexOf(c);
if(index != -1)
{
c2 = c2.substring(0, index)+c2.substring(index+1, c2.length());
}
else
{
status = false;
break;
}
}
}
return status;
}
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" );
}
}
/*
output:
Input (stdin)
anagramm
marganaa
Your Output (stdout)
Not Anagrams
Expected Output
Not Anagrams
*/