-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Anagram.java
43 lines (37 loc) · 1.54 KB
/
Anagram.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
40
41
42
43
import java.util.*;
public class Anagram {
public static void main(String[] args) {
String firstString = args[0];
String secondSring = args[1];
if (isAnagram(firstString, secondSring)) {
System.out.println(secondSring + " is an anagram of " + firstString);
} else {
System.out.println(secondSring + " is not an anagram of " + firstString);
}
}
public static Boolean isAnagram(String firstString, String secondString) {
if (firstString.length() != secondString.length()) {
return false;
} else {
char[] firstStringCharArray = new char[firstString.length()];
char[] secondStringCharArray = new char[secondString.length()];
// Copy character by character into arrays
for (int i = 0; i < firstString.length(); i++) {
firstStringCharArray[i] = firstString.toLowerCase().charAt(i);
}
for (int i = 0; i < secondString.length(); i++) {
secondStringCharArray[i] = secondString.toLowerCase().charAt(i);
}
// Sort the arrays
java.util.Arrays.sort(firstStringCharArray);
java.util.Arrays.sort(secondStringCharArray);
// Check if the two char arrays are equal
for (int i = 0; i < firstString.length(); i++) {
if (firstStringCharArray[i] != secondStringCharArray[i]) {
return false;
}
}
return true;
}
}
}