forked from RasPat1/practice-codewars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDictionary2.java
211 lines (167 loc) · 5.34 KB
/
Dictionary2.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import java.util.*;
/*
* https://www.codewars.com/kata/5259510fc76e59579e0009d4/train/java
*/
public class Dictionary2 {
private final String[] words;
public Dictionary(String[] words) {
this.words = words;
}
public String findMostSimilar(String to) {
// TODO: this is your task ;)
String closestWord = "";
int minDistance = Integer.MAX_VALUE;
for (int i = 0; i < words.length; i++) {
String from = words[i];
// int distance = naiveDistance(from, to);
int distance = deletionDistance(from, to);
// System.out.println("Distance from " + from + " to " + to + " is: " + distance);
// System.out.println("minDistance: " + minDistance);
if (distance < minDistance) {
closestWord = from;
minDistance = distance;
// System.out.println("updated");
}
}
return closestWord;
}
public static int deletionDistance(String s1, String s2) {
HashMap<String, Integer> memo = new HashMap<String, Integer>();
int bestKnownSolution = s1.length() + s2.length();
return deletionDistance(s1, s2, memo, 0, bestKnownSolution);
}
public static int deletionDistance(String s1, String s2, HashMap<String, Integer> memo, int count, int bestKnownSolution) {
if (count > bestKnownSolution) {
return count;
}
if (s1.equals(s2)) {
return count;
}
if (s2.length() > s1.length() || (s1.length() == s2.length() && s1.charAt(0) < s2.charAt(0))) {
String temp = s1;
s1 = s2;
s2 = temp;
}
String key = s1 + "," + s2;
if (memo.containsKey(key)) {
// System.out.println("used memo for: " + key);
return memo.get(key);
}
int minDistance = s1.length() + s2.length() + count;
for (int i = 0; i < s1.length(); i++) { // O(m)
String afterDeletion = removeChar(s1, i); // O(1)
int d = deletionDistance(afterDeletion, s2, memo, count + 1, bestKnownSolution);
// memo.put(afterDeletion + "," + s2, d - 1);
minDistance = d < minDistance ? d : minDistance; // O(1)
if (d < bestKnownSolution) {
bestKnownSolution = d;
}
}
memo.put(key, minDistance); // O(1)
return minDistance;
}
// Get LCS (multiple LCs of same length?)
// try to extend LCS
// add char to line up
// replace char to line up
// remove char to line up
// want LCS to be size of string
// Use DP!
// take naive version
// instead of trying
// XXXCCCCXCCC
// CCCCXCCCXXX
// 12345667890
// 61234567890
// 6
// 12345667890
// 61234567890
// 6
// abcd
// bczz
// Edit distance
public int editDistance(String s1, String s2) {
// scoring -- 1 point for add, remove, or replace
int score = 0;
// I dont konw this alg so let's start form scratch
// Do we ahve to use longest common subsequence?
// s1 does not change, try to match s2 to s1
// alg
// 1) Find LCS
// 2) ?Line them up?
// 3) Make each possible change
// 4) Repeat with each changed word
// goign to be recursive
// BASECASE
// if they are the same return 0
// SUBPROBLEM
// LCS?
return 0;
}
/**
* Return longest common subsequence
* return index on s1 where s2 starts
* Should this return the actual LCS, the index on s1 or s2 where it starts?
* Let's return teh actual LCS
* We can get the index on each later by doing a find on the string
*/
public int LCS(String s1, String s2) {
return 0;
}
public static int naiveDistance(String s1, String s2) {
HashMap<String, Integer> memo = new HashMap<String, Integer>();
return naiveDistance(s1, s2, memo);
}
/**
* Naive brute force edit Distance?
* Just try every operation on every char
* and see if we can get to 0
*/
public static int naiveDistance(String s1, String s2, HashMap<String, Integer> memo) {
int maxScore = Math.max(s1.length(), s2.length());
int score = maxScore;
if (s2.equals(s1)) {
return 0;
}
if (memo.containsKey(s2)) {
return memo.get(s2);
}
if (s2.length() < s1.length()) {
int localAddMin = maxScore;
for (int i = 0; i < s1.length(); i++) {
char charToAdd = s1.charAt(i);
for (int j = 0; j <= s2.length(); j++) {
String addTemp = addChar(s2, j, charToAdd);
int addScore = naiveDistance(s1, addTemp, memo) + 1;
localAddMin = addScore < localAddMin ? addScore : localAddMin;
}
}
score = localAddMin;
} else if (s2.length() == s1.length()) {
int distance = 0;
for (int i = 0; i < s1.length(); i++) {
if (s1.charAt(i) != s2.charAt(i)) {
distance++;
}
}
score = distance;
} else if (s2.length() > s1.length()) {
for (int i = 0; i < s2.length(); i++) {
String removeTemp = removeChar(s2, i);
int removeScore = naiveDistance(s1, removeTemp, memo) + 1;
score = removeScore < score ? removeScore : score;
}
}
memo.put(s2, score);
return score;
}
public static String replaceChar(String s, int i, char c) {
return s.substring(0, i) + c + s.substring(i+1);
}
public static String addChar(String s, int i, char c) {
return s.substring(0, i) + c + s.substring(i);
}
public static String removeChar(String s, int i) {
return s.substring(0, i) + s.substring(i+1);
}
}