-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringCleaning.java
58 lines (47 loc) · 1.39 KB
/
StringCleaning.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
package Google;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
/**
* Algorithm: Push string by removing every possibility of word from chunk
* @author macretina
*
*/
public class StringCleaning {
public static void main(String[] args) {
String chunk = "aabb";
String word = "ab";
System.out.println("answer: " + answer(chunk, word));
}
private static String answer(String chunk, String word) {
Set<String> seen = new HashSet<String>();
Queue<String> queue = new LinkedList();
return answerUtil(chunk, word, seen, queue);
}
public static String answerUtil(String chunk, String word,
Set<String> seen, Queue<String> queue) {
String finalResult = chunk;
queue.add(chunk);
while (!queue.isEmpty()) {
String value = queue.remove();
int fromIndex = 0;
while (value.indexOf(word, fromIndex) != -1) {
int index = value.indexOf(word, fromIndex);
String remaining = value.substring(0, index)
+ value.substring(index + word.length());
fromIndex++;
if (seen.contains(remaining))
continue;
else if (remaining.length() == finalResult.length()) {
if (remaining.compareTo(finalResult) < 0)
finalResult = remaining;
} else if (remaining.length() < finalResult.length())
finalResult = remaining;
seen.add(remaining);
queue.add(remaining);
}
}
return finalResult;
}
}