-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnagrams.java
88 lines (58 loc) · 1.54 KB
/
Anagrams.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// 13707293
// robertjohnhill1@gmail.com
import java.io.*;
import java.util.*;
public class Anagrams {
public static char anagrams( String a, String b )
{
String lowerA = a.toLowerCase(Locale.ROOT);
//removes non alphanumeric characters from string
lowerA.replace("/[^A-Za-z0-9]/", "");
//splits string into an array and sorts it in alphabetical order
String[] arrA = lowerA.split("");
Arrays.sort(arrA);
String newA = Arrays.toString(arrA);
String lowerB = b.toLowerCase(Locale.ROOT);
lowerB.replace("/[^A-Za-z0-9]/", "");
String[] arrB = lowerB.split("");
Arrays.sort(arrB);
String newB = Arrays.toString(arrB);
if ( Objects.equals(newA, newB)){
return '!';
}
else {
List<String> listA = new ArrayList<String>();
for (String k : arrA){
listA.add(k);
}
List<String> listB = new ArrayList<String>();
for (String k : arrB){
listB.add(k);
}
for( String j : arrB){
for (int i = 0 ; i <= listA.size() -1; i++){
if (Objects.equals(listA.get(i), j)) {
listA.remove(i);
}
}
}
for (int i = 0 ; i <= listB.size() - 1; i++){
for( String j : arrA){
if (Objects.equals(listB.get(i), j)) {
listB.remove(i);
}
}
}
ArrayList<String> lastArr = new ArrayList<String>();
lastArr.addAll(listA);
lastArr.addAll(listB);
Collections.sort(lastArr);
String ret_string = lastArr.toString();
Character ret_char = ret_string.charAt(ret_string.length()-2);
return ret_char;
}
}
public static void main( String[] args )
{
}
}