-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathIsAnagram.java
More file actions
37 lines (31 loc) · 1001 Bytes
/
IsAnagram.java
File metadata and controls
37 lines (31 loc) · 1001 Bytes
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
public class IsAnagram {
public static void main(String[] args) {
System.out.println(isAnagram("keep", "peek"));
}
public static String sort(String word) {
StringBuilder builder = new StringBuilder(word);
for (int i = 1; i < builder.length(); i++) {
char temp = builder.charAt(i);
int j = i;
while (j > 0 && builder.charAt(j - 1) > temp) {
builder.setCharAt(j, builder.charAt(j - 1));
j--;
}
builder.setCharAt(j, temp);
}
return builder.toString();
}
public static boolean isAnagram(String word1, String word2) {
if (word1.length() != word2.length()) {
return false;
}
word1 = sort(word1);
word2 = sort(word2);
for (int i = 0; i < word1.length(); i++) {
if (word1.charAt(i) != word2.charAt(i)) {
return false;
}
}
return true;
}
}