File tree 1 file changed +41
-0
lines changed
1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .Scanner ;
2
+
3
+ public class Anagrams {
4
+
5
+ static boolean isAnagram (String a , String b ) {
6
+
7
+
8
+ if (a .length ()!=b .length ()) return false ;
9
+
10
+ a =a .toLowerCase ();
11
+ b =b .toLowerCase ();
12
+ int []frequency =new int [26 ];
13
+ for (int i =0 ;i <a .length ();i ++)
14
+ {
15
+ char current =a .charAt (i );
16
+ int index =current -'a' ;
17
+ frequency [index ]++;
18
+ }
19
+ for (int i =0 ;i <b .length ();i ++)
20
+ {
21
+ char current =b .charAt (i );
22
+ int index =current -'a' ;
23
+ frequency [index ]--;
24
+ }
25
+ for (int i =0 ;i <26 ;i ++)
26
+ {
27
+ if (frequency [i ]!=0 ) return false ;
28
+ }
29
+ return true ;
30
+ }
31
+
32
+ public static void main (String [] args ) {
33
+
34
+ Scanner scan = new Scanner (System .in );
35
+ String a = scan .next ();
36
+ String b = scan .next ();
37
+ scan .close ();
38
+ boolean ret = isAnagram (a , b );
39
+ System .out .println ( (ret ) ? "Anagrams" : "Not Anagrams" );
40
+ }
41
+ }
You can’t perform that action at this time.
0 commit comments