forked from GDSC-IGDTUW-Autumn-of-Code-2022/dsa-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Strings_Ana.java
39 lines (31 loc) · 978 Bytes
/
Strings_Ana.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
import java.lang.reflect.Array;
import java.util.Arrays;
public class Strings_Ana {
public static void main(String[] args) {
System.out.println(checkAnagram("CAT", "ACT"));
}
public static boolean checkAnagram(String a, String b) {
// convert both the string to lower case
String b1 = b.toLowerCase();
String a1 = a.toLowerCase();
// to compare both string, tore it into char array
char[] ch = a1.toCharArray();
char[] ch1 = b1.toCharArray();
// sort both the array to compare directly
Arrays.sort(ch);
Arrays.sort(ch1);
int i = 0;
while (ch.length > 0 && ch.length > i) {
if (a.length() == b.length()) {
if (ch[i] == ch1[i]) {
i++;
} else {
return false;
}
} else {
return false;
}
}
return true;
}
}