Skip to content

Commit 7e0adc8

Browse files
committed
Check if Strings are Anagrams
1 parent d06abd6 commit 7e0adc8

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

Java/Strings/Anagrams.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Two strings AA and BB are called anagrams if they consist same characters, but may be in different orders. So the list of anagrams of CAT are "CAT", "ACT" , "TAC", "TCA" ,"ATC" and "CTA".
3+
4+
Given two strings, print "Anagrams" if they are anagrams, print "Not Anagrams" if they are not. The strings may consist at most 50 english characters, the comparison should NOT be case sensitive.
5+
6+
This exercise will verify that you are able to sort the characters of a string, or compare frequencies of characters.
7+
8+
Sample Input 1
9+
anagram
10+
margana
11+
12+
Sample Output 1:
13+
Anagrams
14+
*/
15+
16+
import java.io.*;
17+
import java.util.*;
18+
19+
public class Anagrams {
20+
21+
static boolean isAnagram(String A, String B) {
22+
A=A.toLowerCase();
23+
B=B.toLowerCase();
24+
boolean f = false;
25+
char[] c = A.toCharArray();
26+
Arrays.sort(c);
27+
char[] d = B.toCharArray();
28+
Arrays.sort(d);
29+
String a = new String (c);
30+
String b = new String (d);
31+
if (a.equals(b)) {
32+
f=true;
33+
}
34+
return f;
35+
36+
}
37+
public static void main(String[] args) {
38+
39+
Scanner sc=new Scanner(System.in);
40+
String A=sc.next();
41+
String B=sc.next();
42+
boolean ret=isAnagram(A,B);
43+
if(ret)System.out.println("Anagrams");
44+
else System.out.println("Not Anagrams");
45+
46+
}
47+
}

0 commit comments

Comments
 (0)