Remove Chars from String in place
private Set<Character> makeSet (String t) {
Set<Character> set = new HashSet<Character>();
for (int i = 0; i < t.length(); i++) {
set.add(t.charAt(i));
}
return set;
}
public String remove(String input, String t) {
//get input into array
char[] array = input.toChararray();
Set<Character> set = makeSet(t);
//slow: 0 -> slow not inclusive is result array
//fast: swap with slow if kept
int slow = 0;
for (int fast = 0; fast < array.length; fast++) {
if (!set.contains(array[fast])) {
array[slow++] = array[fast];
}
}
return new String(array, 0, slow);
}Last updated