-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnagramChecker242.java
89 lines (83 loc) · 2.68 KB
/
AnagramChecker242.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
89
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
/*
* test test test waaaaa
*/
class Solution {
/**
* extremely shitty implementation i cooked up because i forgot hashtables existsed
* @param s
* @param t
* @return
*/
public boolean isAnagramWithLists(String s, String t) {
if (s.length() != t.length()) {
return false;
} int maxLength = 5 * (int)Math.pow(10, 4);
if (s.length() < 1 || t.length() > maxLength) {
return false; // given constraint, filter for length
}
String regex = "[A-Z]";
if (s.matches(regex) || t.matches(regex)) {
return false; //filter for lower cases;
}
List<Character> inputLetters = convertToList(s.toCharArray());
List<Character> checkedLetters = convertToList(t.toCharArray());
int listSize = inputLetters.size();
Boolean[] letterList = new Boolean[listSize]; // true if used, false otherwise
for (int i = 0; i < letterList.length; i++) {
letterList[i] = false;
}
boolean matched = false;
for (Character i : inputLetters) {
matched = false;
int tempJIndex = 0;
for (Character j : checkedLetters) {
if ((i.compareTo(j)) == 0 && letterList[tempJIndex] == false) {
matched = true;
letterList[tempJIndex] = true;
break;
}
tempJIndex++;
}
if (!matched) {
return false;
}
}
return true;
}
private static List<Character> convertToList(char[] array) {
List<Character> list = new ArrayList<>();
for (char c : array) {
list.add(c);
}
return list;
}
/**
* new superior solution
* based on the stuff i read about hashtables, sadly
*/
public Boolean isAnagram(String s, String t) {
Hashtable<Character, Integer> valueMap = new Hashtable<>();
for (char x : s.toCharArray()){
valueMap.put(x,valueMap.getOrDefault(x, 0) + 1);
}
for (char x : t.toCharArray()){
valueMap.put(x,valueMap.getOrDefault(x, 0) - 1);
}
for (int val: valueMap.values()){
if (val != 0) {
return false;
}
}
return true;
}
}
public class AnagramChecker242 {
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.isAnagram("anagram", "nagaram"));
System.out.println(solution.isAnagram("aacc", "ccac"));
}
}