forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWithoutString.java
45 lines (40 loc) · 1.59 KB
/
WithoutString.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
package com.rampatra.strings;
import com.sun.tools.javac.util.Assert;
/**
* Given two strings, base and remove, return a version of the base string where all instances
* of the remove string have been removed (not case sensitive). You may assume that the remove
* string is length 1 or more. Remove only non-overlapping instances, so with "xxx" removing
* "xx" leaves "x".
*
* @author rampatra
* @since 2019-01-23
*/
public class WithoutString {
private static String withoutString(String base, String remove) {
String original = base;
base = base.toLowerCase();
remove = remove.toLowerCase();
int baseLen = base.length();
int removeLen = remove.length();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < baseLen; ) {
int j = 0;
// when we see a match, advance the pointer
while (j < removeLen && i + j < baseLen && base.charAt(i + j) == remove.charAt(j)) {
j++;
}
if (j == removeLen) { // an entire match was found, move ahead and skip these chars
i += removeLen;
} else {
sb.append(original.charAt(i)); // entire match was not found so append the char to StringBuilder
i++;
}
}
return sb.toString();
}
public static void main(String[] args) {
Assert.check(withoutString("Hello there", "llo").equals("He there"));
Assert.check(withoutString("THIS is a FISH", "is").equals("TH a FH"));
Assert.check(withoutString("xxx", "x").equals(""));
}
}