-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathAnagrammanipulatorr.java
More file actions
36 lines (28 loc) · 1.07 KB
/
Anagrammanipulatorr.java
File metadata and controls
36 lines (28 loc) · 1.07 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
import java.util.Arrays;
public class Anagrammanipulatorr {
public static boolean areAnagrams(String str1, String str2) {
// Remove spaces,convert both strings to lowercase comparison
str1 = str1.replaceAll("\\s", "").toLowerCase();
str2 = str2.replaceAll("\\s", "").toLowerCase();
// Check if lengths of two strings are different
if (str1.length() != str2.length()) {
return false;
}
// Convertstrings to char arrays and sort
char[] charArray1 = str1.toCharArray();
char[] charArray2 = str2.toCharArray();
Arrays.sort(charArray1);
Arrays.sort(charArray2);
// Compare sorted arrays
return Arrays.equals(charArray1, charArray2);
}
public static void main(String[] args) {
String str1 = "abcd";
String str2 = "dabc";
if (areAnagrams(str1, str2)) {
System.out.println(str1 + " and " + str2 + " are anagrams.");
} else {
System.out.println(str1 + " and " + str2 + " are not anagrams.");
}
}
}