Thursday Code Puzzler: Some String Manipulation
Another Thursday, another puzzle. The idea is simple: solve the coding problem as efficiently as you can, in any language or framework that you think is suitable. This week's one is a bit more practical than previous puzzles.
Note: Even though there really is nothing stopping you from finding a solution to this on the internet, try to keep honest, and come up with your own answer. It's all about the participation!
Remove Characters From A String
Write a method that takes two String parameters string1 and string2. The method must remove all the characters from string2 that appeared in string1. Here's an example to make the problem clearer:
string1: code
string2: puzzle
result: puzzl
If you wish, you can express the efficiency of the algorithm you come up with using Big O notation.






Comments
Sathish Kumar replied on Thu, 2012/05/10 - 4:18am
string1Char = string1.toCharArray(); string2Char = string2.toCharArray(); for (int i=0;i<string1Char.length;i++ ) { int asciicode = (int)string1Char[i]; arrChar[asciicode-97] += 1; } for (int j=0;j<string2Char.length;j++ ) { int asciicode = (int)string2Char[j]; if ( arrChar[asciicode - 97] == 0 ) { System.out.print(string2Char[j]); } } }
Philipp Neumann replied on Thu, 2012/05/10 - 4:59am
James Sugrue replied on Thu, 2012/05/10 - 5:28am
in response to:
Philipp Neumann
I see where you're going with that one :) But it's more fun to do the lower level algorithm instead of using the utility functions available
James
Motaz Mohamed replied on Thu, 2012/05/10 - 6:09am
Reinhard Nägele replied on Thu, 2012/05/10 - 6:42am
import com.google.common.primitives.Chars; public static String removeCharsFromString(final String s, final String charsToRemove) { char[] chars = s.toCharArray(); CharBuffer buf = CharBuffer.allocate(charsToRemove.length()); for (char c : charsToRemove.toCharArray()) { if (Chars.indexOf(chars, c) == -1) { buf.append(c); } } return buf.flip().toString(); }Chirag Visavadia replied on Thu, 2012/05/10 - 6:54am
private static String getUniqueString(String str1, String str2) { /** * Approach : Check all the char of str2 for existence in str1 and if * NOT exists then print. */ long startTime = Calendar.getInstance().getTimeInMillis(); HashSet<Character> alreadyProcessed = new HashSet<Character>(); for (int i = 0; i < str2.length(); i++) { /** * Removing extra cycle for repetitive Char */ if (alreadyProcessed.contains(str2.charAt(i))) break; boolean charExists = false; for (int j = 0; j < str1.length(); j++) { if (str2.charAt(i) == str1.charAt(j)) { charExists = true; /** * Require to reduce extra cycle for repetitive Char. */ alreadyProcessed.add(str2.charAt(i)); /** * We are using break hear because it will reduce * unnecessary time for repetitive char. */ break; } } if (!charExists) System.out.print(str2.charAt(i)); } long endTime = Calendar.getInstance().getTimeInMillis(); System.out.println("\nStart Time: " + startTime + " - End Time: " + endTime); System.out.println("Approach Total Time(mills) : " + (endTime - startTime)); /** * Approach Ends */ return null; }Bowen Ma replied on Thu, 2012/05/10 - 10:32am
Bit Twiddler replied on Thu, 2012/05/10 - 5:00pm
// Well, it's a solution!! :)
String removeChars(final String input, final String removed) {
byte[] charTable = new byte[8192];
for(int i = 0 ; i < removed.length() ; i++) {
char ch = removed.charAt(i);
charTable[ch / 8] |= (1 << (ch & 7));
}
StringBuilder sb = new StringBuilder(input.length());
for(int i = 0 ; i < input.length() ; i++ ) {
char ch = input.charAt(i);
if((charTable[ch / 8] & (1 << (ch & 7))) == 0) {
sb.append(ch);
}
}
return sb.toString();
}
Jason Holbrook replied on Thu, 2012/05/10 - 5:04pm
public static String removeCharacters(String str, String removeChars) { StringBuilder sb = new StringBuilder(); StringTokenizer st = new StringTokenizer(str, removeChars); while(st.hasMoreTokens()) { sb.append(st.nextToken()); } return sb.toString(); }Justin Julicher replied on Thu, 2012/05/10 - 9:41pm
public static String removeChars(String string, String removeString) { char[] removeChars = removeString.toCharArray(); Arrays.sort(removeChars); char[] array = string.toCharArray(); CharBuffer buffer = CharBuffer.allocate(array.length); for (char c : array) { if (Arrays.binarySearch(removeChars, c) < 0) { buffer.append(c); } } return buffer.flip().toString(); }Ian Mcintosh replied on Fri, 2012/05/11 - 10:03am
Jason Holbrook replied on Fri, 2012/05/11 - 12:49pm
in response to:
Jason Holbrook
ebenezer raj replied on Fri, 2012/05/11 - 1:48pm