-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathAnagrams.java
More file actions
43 lines (35 loc) · 1.09 KB
/
Anagrams.java
File metadata and controls
43 lines (35 loc) · 1.09 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
public class Anagrams {
static boolean isAnagram(String a, String b) {
// Complete the function
String tempA = a.toLowerCase();
String tempB = b.toLowerCase();
// if string length not same return false
if (tempA.length() != tempB.length()){
return false;
}
char[] array1 = tempA.toCharArray();
char[] array2 = tempB.toCharArray();
// sort array
java.util.Arrays.sort(array1);
java.util.Arrays.sort(array2);
// compare sorted arrays
for (int i=0;i<array1.length;i++){
if (array1[i] != array2[i]){
return false;
}
}
return true;
}
public static void main(String[] args) {
String a = "Hello";
String b = "hello";
// if string same print yes else no
if (isAnagram(a, b)){
System.out.println("Yes");
}else{
System.out.println("No");
}
System.out.println("\nString before reverse: " + a);
System.out.println("String after reverse: " + b);
}
}