Microsoft Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
import java.util.HashSet;
import java.util.Set;
public class RemoveChar {
public static void remove(char[] string, Set<Character> set) {
int currPtr = 0;
int ptr = 0;
for(currPtr = 0;currPtr < string.length;currPtr++){
if(set.contains(string[currPtr])){
ptr++;
}else{
string[currPtr - ptr] = string[currPtr];
}
}
while(ptr > 1){
string[currPtr - ptr] = 0;
ptr--;
}
string[currPtr - ptr] = 0;
}
public static void main(String[] args) {
Set<Character> set = new HashSet<Character>();
set.add('a');
set.add('i');
// char[] string = { 'a', 'b', 'c', 'i', 'a', 'b', 'c' };
char[] string = { 'a', 'i', 'a'};
remove(string, set);
for (char c : string) {
System.out.print(c + " ");
}
}
}
public String removeOccurence(String word, char[] letters){
int pivot1 = 0;
int pivot2 = 0;
char[] words = word.toCharArray();
int length = words.length-1;
while (pivot1 <= length){
while (pivot1 <= length && (!isContainsLetter(words[pivot1], letters))){
words[pivot2] = words[pivot1];
pivot2++;
pivot1++;
}
while (pivot1 <= length && (isContainsLetter(words[pivot1],letters))){
pivot1++;
}
}
String result = String.copyValueOf(words);
return result.substring(0,pivot2);
}
public boolean isContainsLetter(char letter, char[] letters){
int i = 0;
while (i <= letters.length - 1){
if (letters[i] == letter){
return true;
}
i++;
}
return false;
}
Algorithm: Keep two pointers for this work
While(string != '\0') {
1. "GOING" is the string.Initially both will point to 'G'.
2. Move first pointer if it 'a' or 'i'.If it is not then don't move first pointer.
3. Copy first pointer data in second pointer and increment second pointer.
return second pointer.
}
char * string_removal(char *string, char a, char i)
{
char *first = string;
char *second = string;
char *temp = string;
while(*string != '\0') {
if(*first == a || *first == i)
first++;
else {
*second = *first++;
second++;
}
string++;
}
*second = '\0';
return temp;
}
char* remove_ai_occurence(char* str) {
unsigned int len = strlen(str);
char* nextHolePtr = NULL;
for (unsigned int i = 0; i < len; i++) {
if (str[i] == 'i' || str[i] == 'a') {
if (!nextHolePtr)
nextHolePtr = str + i;
} else if (nextHolePtr) {
//if the nextHolePtr is non-null, fill the whole
nextHolePtr[0] = str[i];
nextHolePtr++;
}
}
if (nextHolePtr)
*nextHolePtr = '\0';
return str;
}
public class RemoveLetters
{
public static void main(String[] args)
{
String s = null ;
System.out.println("Enter the first string\n");
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
s = br.readLine();
}
catch(Exception e){}
char arr[] = s.toCharArray();
int len = s.length();
int index1 = 0;
for (int i=0; i<len; i++)
{
if(arr[i] != 'i' && arr[i] != 'l')
{
arr[index1] = arr[i];
index1++;
}
}
arr[index1] = 0;
for (int i=0; arr[i] != 0 ; i++)
System.out.print(arr[i]);
}
}
pls suggest some method to reduce its complexity.......
#include<stdio.h>
int main()
{
char str[100];
int i=0,j;
printf("enter the string:\n");
scanf("%s",str);
while(str[i++])
{
if(str[i]=='A'||str[i]=='I'||str[i]=='a'||str[i]=='i')
{
j=i;
while(str[j]){
str[j]=str[j+1];j++;}
i--;
}
}
printf("thus the resultant string is:%s",str);
return 0;
}
static void RemoveAandI(char [] s)
{
int index = 0;
for (int i = 0; i < s.Length; i++)
{
if (s[i] != 'a' && s[i] != 'i')
{
if (i > index)
{
s[index++] = s[i];
}
}
}
if (index == 0 && (s[index] == 'a' || s[index] == 'i'))
{
throw new Exception("Unable to find char other then 'a' and 'i'");
}
Console.WriteLine(s);
}
public static int removeCharInPlace(char[] str){
int lo = 0, hi = str.length - 1, len = str.length;
while(lo < hi){
while(lo < len && (str[lo] != 'A' && str[lo] != 'I')){
lo ++;
}
if(lo >= len || lo > hi){
break;
}
while(hi >= 0 && (str[hi] == 'A' || str[hi] == 'I')){
len --;
hi --;
}
if(lo >= len || lo > hi){
break;
}
swapTwoChar(str, lo, hi);
}
return len;
}
public static void swapTwoChar(char[] str, int from, int to){
char tmpChar = str[from];
str[from] = str[to];
str[to] = tmpChar;
}
public string CharRemovalForString(string inputString, Char removalChar)
{
int p = 0;
Char[] inputCharArray = inputString.ToCharArray();
for (int i = 0; i < inputCharArray.Length; i++)
{
if (inputCharArray[i] == removalChar)
{
p++;
}
else
{
inputCharArray[i - p] = inputCharArray[i];
}
}
for (int j = inputCharArray.Length - 1; j > inputCharArray.Length - p - 1; j--)
{
inputCharArray[j] = ' ';
}
return new string(inputCharArray);
}
- isandesh7 January 31, 2013