If we are asked to remove specific or certain or unwanted characters from the string .How can we achieve this in the most efficient way ? This question has cemented its place in the technical interview of the reputed companies like Amazon or Goldman sachs .
First we need to understand the question , we need to write an algorithm to remove the certain or specific or unwanted characters from the string , for example
Read Also : Remove all whitespaces in the string
If the original string is "Alive is awesome" and the user inputs string to remove "alwsr" then it should print "ive i eome" as output .
If the original string is "Learning never stops" and the user inputs string to remove "estp" then the it should print "Larning nvr o" as output .
let us understand the logic and pseudo algorithm to achieve this task .
The prototype of the function to achieve result will be like this
String removeSpecificChars( String originalstring , String removecharacterstring)
Logic :
We need to remove the certain characters from the original string . To achieve this , we will create a tempBoolean array , in which every index element value will be false by default . As we know if we put char in integer field , it will always print the ascii value of the character . We will pass one by one the characters of the removecharacterstring to the index of the tempBoolean array , and set the corresponding index value to true . Now iterate through the originalstring and print the character only if its value in tempBoolean is false.
Read Also : Find first non repeated character in the string : Amazon interview question
Pseudo Algorithm :
* Change strings originalstring and removecharacterstring to character array .
* Create a temporary boolean array named tempBoolean of length 128 (ASCII characters) . This will set
all the indexes value to false by default .
* Iterate through each character in removecharacterstring , setting the corresponding values in the
tempBoolean array to true .
* Start iterating originalstring from 0 to the length of the originalstring string , copying each character only if its corresponding value in tempBoolean array is false .
Read Also : Reverse string without using built in java method
Demo :
Code :
First we need to understand the question , we need to write an algorithm to remove the certain or specific or unwanted characters from the string , for example
Read Also : Remove all whitespaces in the string
If the original string is "Alive is awesome" and the user inputs string to remove "alwsr" then it should print "ive i eome" as output .
If the original string is "Learning never stops" and the user inputs string to remove "estp" then the it should print "Larning nvr o" as output .
let us understand the logic and pseudo algorithm to achieve this task .
The prototype of the function to achieve result will be like this
String removeSpecificChars( String originalstring , String removecharacterstring)
Logic :
We need to remove the certain characters from the original string . To achieve this , we will create a tempBoolean array , in which every index element value will be false by default . As we know if we put char in integer field , it will always print the ascii value of the character . We will pass one by one the characters of the removecharacterstring to the index of the tempBoolean array , and set the corresponding index value to true . Now iterate through the originalstring and print the character only if its value in tempBoolean is false.
Read Also : Find first non repeated character in the string : Amazon interview question
Pseudo Algorithm :
* Change strings originalstring and removecharacterstring to character array .
* Create a temporary boolean array named tempBoolean of length 128 (ASCII characters) . This will set
all the indexes value to false by default .
* Iterate through each character in removecharacterstring , setting the corresponding values in the
tempBoolean array to true .
* Start iterating originalstring from 0 to the length of the originalstring string , copying each character only if its corresponding value in tempBoolean array is false .
Read Also : Reverse string without using built in java method
Demo :
Code :
import java.util.Scanner; public class RemoveSpecificCharacter { public static void main(String[] args) { String originalstring="Alive is Awesome"; System.out.println("Original string is >> "+ originalstring); System.out.println(""); System.out.println("Please enter unwanted characters as String"); System.out.println(""); System.out.println(""); Scanner in =new Scanner(System.in); String removecharacterstring=in.nextLine(); String output=removeSpecificChars(originalstring, removecharacterstring); System.out.println(""); System.out.println(""); System.out.print("Output is >> " ); System.out.println(output); } public static String removeSpecificChars(String originalstring ,String removecharacterstring) { char[] orgchararray=originalstring.toCharArray(); char[] removechararray=removecharacterstring.toCharArray(); int start,end=0; //tempBoolean automatically initialized to false ,size 128 assumes ASCII boolean[] tempBoolean = new boolean[128]; //Set flags for the character to be removed for(start=0;start < removechararray.length;++start) { tempBoolean[removechararray[start]]=true; } //loop through all characters ,copying only if they are flagged to false for(start=0;start < orgchararray.length;++start) { if(!tempBoolean[orgchararray[start]]) { orgchararray[end++]=orgchararray[start]; } } return new String(orgchararray,0,end); } }