forked from phishman3579/java-algorithms-implementation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringFunctions.java
308 lines (266 loc) · 10.1 KB
/
StringFunctions.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package com.jwetherell.algorithms.strings;
import java.util.BitSet;
import java.util.StringTokenizer;
/**
* This class contains methods for modifying text.
* <p>
* @author Justin Wetherell <phishman3579@gmail.com>
*/
public class StringFunctions {
private static final char SPACE = ' ';
public static final String reverseWithStringConcat(String string) {
String output = new String();
for (int i = (string.length() - 1); i >= 0; i--) {
output += (string.charAt(i));
}
return output;
}
public static final String reverseWithStringBuilder(String string) {
final StringBuilder builder = new StringBuilder();
for (int i = (string.length() - 1); i >= 0; i--) {
builder.append(string.charAt(i));
}
return builder.toString();
}
public static final String reverseWithStringBuilderBuiltinMethod(String string) {
final StringBuilder builder = new StringBuilder(string);
return builder.reverse().toString();
}
public static final String reverseWithSwaps(String string) {
final char[] array = string.toCharArray();
final int length = array.length - 1;
final int half = (int) Math.floor(array.length / 2);
char c;
for (int i = length; i >= half; i--) {
c = array[length - i];
array[length - i] = array[i];
array[i] = c;
}
return String.valueOf(array);
}
public static final String reverseWithXOR(String string) {
final char[] array = string.toCharArray();
final int length = array.length;
final int half = (int) Math.floor(array.length / 2);
for (int i = 0; i < half; i++) {
array[i] ^= array[length - i - 1];
array[length - i - 1] ^= array[i];
array[i] ^= array[length - i - 1];
}
return String.valueOf(array);
}
public static final String reverseWordsByCharWithAdditionalStorage(String string) {
final StringBuilder builder = new StringBuilder();
final int length = string.length() - 1;
final StringBuilder temp = new StringBuilder();
char c = 0;
int index = 0;
int last = string.length();
for (int i = length; i >= 0; i--) {
c = string.charAt(i);
if (c == SPACE || i == 0) {
index = (i == 0) ? 0 : i + 1;
temp.append(string.substring(index, last));
if (index != 0)
temp.append(c);
builder.append(temp);
temp.delete(0, temp.length());
last = i;
}
}
return builder.toString();
}
public static final String reverseWordsUsingStringTokenizerWithAdditionalStorage(String string) {
final StringTokenizer st = new StringTokenizer(string);
String output = new String();
while (st.hasMoreTokens()) {
output = (st.nextToken()) + ' ' + output;
}
return output.trim();
}
public static final String reverseWordsUsingSplitWithAdditionalStorage(String string) {
final StringBuilder builder = new StringBuilder();
final String[] temp = string.split(" ");
for (int i = (temp.length - 1); i >= 0; i--) {
builder.append(temp[i]).append(' ');
}
return builder.toString().trim();
}
public static final String reverseWordsInPlace(String string) {
char[] chars = string.toCharArray();
int lengthI = 0;
int lastI = 0;
int lengthJ = 0;
int lastJ = chars.length - 1;
int i = 0;
char iChar = 0;
char jChar = 0;
while (i < chars.length && i <= lastJ) {
iChar = chars[i];
if (iChar == SPACE) {
lengthI = i - lastI;
for (int j = lastJ; j >= i; j--) {
jChar = chars[j];
if (jChar == SPACE) {
lengthJ = lastJ - j;
swapWords(lastI, i - 1, j + 1, lastJ, chars);
lastJ = lastJ - lengthI - 1;
break;
}
}
lastI = lastI + lengthJ + 1;
i = lastI;
} else {
i++;
}
}
return String.valueOf(chars);
}
private static final void swapWords(int startA, int endA, int startB, int endB, char[] array) {
int lengthA = endA - startA + 1;
int lengthB = endB - startB + 1;
int length = lengthA;
if (lengthA > lengthB)
length = lengthB;
int indexA = 0;
int indexB = 0;
char c = 0;
for (int i = 0; i < length; i++) {
indexA = startA + i;
indexB = startB + i;
c = array[indexB];
array[indexB] = array[indexA];
array[indexA] = c;
}
if (lengthB > lengthA) {
length = lengthB - lengthA;
int end = 0;
for (int i = 0; i < length; i++) {
end = endB - ((length - 1) - i);
c = array[end];
shiftRight(endA + i, end, array);
array[endA + 1 + i] = c;
}
} else if (lengthA > lengthB) {
length = lengthA - lengthB;
for (int i = 0; i < length; i++) {
c = array[endA];
shiftLeft(endA, endB, array);
array[endB + i] = c;
}
}
}
private static final void shiftRight(int start, int end, char[] array) {
for (int i = end; i > start; i--) {
array[i] = array[i - 1];
}
}
private static final void shiftLeft(int start, int end, char[] array) {
for (int i = start; i < end; i++) {
array[i] = array[i + 1];
}
}
public static final boolean isPalindromeWithAdditionalStorage(String string) {
final String reversed = new StringBuilder(string).reverse().toString();
return string.equals(reversed);
}
public static final boolean isPalindromeInPlace(String string) {
final char[] array = string.toCharArray();
final int length = array.length - 1;
final int half = Math.round(array.length / 2f);
char a, b;
for (int i = length; i >= half; i--) {
a = array[length - i];
b = array[i];
if (a != b)
return false;
}
return true;
}
public static final String[] generateSubsets(String inputString) {
final int length = inputString.length();
final int size = (int) Math.pow(2, length);
final BitSet[] sets = new BitSet[size];
final String[] output = new String[size];
for (int i = 0; i < size; i++) {
final BitSet set = new BitSet(size);
final StringBuilder builder = new StringBuilder();
if (i > 0) {
for (int j = length - 1; j >= 0; j--) {
if (j == length - 1) {
if (i % 2 != 0)
set.set(j, true);
} else {
boolean prev = sets[i - 1].get(j);
boolean next = true;
for (int k = j + 1; k < length; k++) {
next = next && sets[i - 1].get(k);
}
if (next)
prev = !prev;
set.set(j, prev);
}
if (set.get(j))
builder.append(inputString.charAt(j));
}
}
sets[i] = set;
output[i] = builder.toString();
}
return output;
}
/** recursive **/
public static final int levenshteinDistanceRecursive(String s, String t) {
final int sLength = s.length();
final int tLength = t.length();
final char[] sChars = s.toCharArray();
final char[] tChars = t.toCharArray();
int cost = 0;
if ((sLength > 0 && tLength > 0) && sChars[0] != tChars[0])
cost = 1;
if (sLength == 0)
return tLength;
else if (tLength == 0)
return sLength;
else {
final int min1 = levenshteinDistanceRecursive(s.substring(1), t) + 1;
final int min2 = levenshteinDistanceRecursive(s, t.substring(1)) + 1;
final int min3 = levenshteinDistanceRecursive(s.substring(1), t.substring(1)) + cost;
int minOfFirstSet = Math.min(min1, min2);
return (Math.min(minOfFirstSet, min3));
}
}
/** iterative - dynamic programming **/
public static final int levenshteinDistanceIterative(String string1, String string2) {
final char[] s = string1.toCharArray();
final char[] t = string2.toCharArray();
final int m = s.length;
final int n = t.length;
// for all i and j, d[i,j] will hold the Levenshtein distance between
// the first i characters of s and the first j characters of t
// note that d has (m+1)*(n+1) values
final int[][] d = new int[m+1][n+1];
// source prefixes can be transformed into empty string by
// dropping all characters
for (int i=1; i<=m; i++)
d[i][0] = i;
// target prefixes can be reached from empty source prefix
// by inserting every character
for (int j=1; j<=n; j++)
d[0][j] = j;
int substitutionCost;
for (int j=1; j<=n; j++) {
for (int i=1; i<=m; i++) {
if (s[i-1] == t[j-1])
substitutionCost = 0;
else
substitutionCost = 1;
int minOfInsAndDel = Math.min(d[i-1][j] + 1, // deletion
d[i][j-1] + 1); // insertion
d[i][j] = Math.min(minOfInsAndDel, // minimum of insert and delete
d[i-1][j-1] + substitutionCost); // substitution
}
}
return d[m][n];
}
}