-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExample16.java
51 lines (37 loc) · 1.21 KB
/
Example16.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
44
45
46
47
48
49
50
51
package applications.algorithms;
import java.util.Arrays;
/** Category: Algorithms
* ID: Example 16
* Description: Test if a word is an anagram of another
* Taken From:
* Details:
* TODO
*/
public class Example16 {
public static boolean isAnagram(String w, String a){
if(w == null || a == null){
throw new NullPointerException("Both strings should be not null");
}
if(w.length() != a.length()){
return false;
}
String lowerW = w.toLowerCase();
char[] lowerWArray = lowerW.toCharArray();
Arrays.sort(lowerWArray);
String lowerA = a.toLowerCase();
char[] lowerSortedAArray = lowerA.toCharArray();
Arrays.sort(lowerSortedAArray);
if((new String(lowerWArray)).equals(new String(lowerSortedAArray))){
return true;
}
return false;
}
public static void main(String[] args){
String w = "eve";
String a = "eve";
System.out.println("String "+a+(isAnagram(w, a) ? " is ": " is not ")+ " an anagram of "+w);
w = "Alex";
a = "cat";
System.out.println("String "+a+(isAnagram(w, a) ? " is ": " is not ")+ " an anagram of "+w);
}
}