Microsoft Interview Question
Tech LeadsTeam: Bing
Country: India
Interview Type: In-Person
Are you sure this is O(n)? I am asking because I know that you can't access the char from position j in a string using : str[j]. And you can't do str[j] = str[i], you will have to do : str = str.substring(0,j) +str.charAt(i)+str.substring(j+1). This will take O(n) . Including this in an iteration throw each character gives an O(n^2) algorithm.
the algo is not o(n) , it must be O (n^2)as is trying to shift all the data left , after encountering multiple spaces
Try it out this solution, it will not work, since you need to do it in the same buffer, when you copy characters to whitespace, then you need to convert the index of the characters being copied to whitespace, else it would lead to doubling of characters.
Also, the access part pointed earlier is also an issue
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STRING 100
#define NEW_LINE1 printf("\n");
#define NEW_LINE2 printf("\n\n");
#define STR_LEN(x) printf("\n\nstrlen == %d", strlen(x));
void remove_whitespace(char *str1);
int main(int argc, char *argv[])
{
char *str1 = calloc(1, MAX_STRING);
char *str2 = calloc(1, MAX_STRING);
str1 = " abcd efgh ijkl mnop ";
remove_whitespace(str1);
system("PAUSE");
return 0;
}
void remove_whitespace(char *str1)
{
char *str1_local = calloc(1, MAX_STRING);
char *str2_local = calloc(1, MAX_STRING);
char *str2_final = calloc(1, MAX_STRING);
int i = 0;
strcpy(str1_local, str1);
int length = 0;
printf("\nstr1_local = %d, %s", str1_local, str1_local);
printf("\nstr2_local = %d, %s", str2_local, str2_local);
printf("\nstr2_final = %d, %s", str2_final, str2_final);
NEW_LINE2;
NEW_LINE2;
do {
str2_local = strchr(str1_local, 32);
if(str2_local) {
NEW_LINE1;
strncat(str2_final , str1_local, str2_local - str1_local);
printf("\nstr1_local = %d, %s", str1_local, str1_local);
printf("\nstr2_local = %d, %s", str2_local, str2_local);
printf("\nstr2_final = %d, %s", str2_final, str2_final);
NEW_LINE1;
str1_local = str2_local + 1;
}
else {
strncat(str2_final , str1_local, strlen(str1_local));
}
length++;
} while (str2_local);
NEW_LINE2;
puts(str2_final);
STR_LEN(str2_final);
NEW_LINE2;
free (str1_local);
free (str2_local);
free (str2_final);
}
to trim all whitespaces, similarly can be modified to remove any unwanted chars, modify remove_whitespace and apss another arg with the char and modify , printf s left FYI
Is it necessary to do it in place? Why not read the whole string char by char and output to new variable if its non extra whitespace or valid character. O(n)
One way of solving is given below :
- for every character in the string, check if it is a redundant space
- If it is, then keep a count of the redundant spaces encountered so far and shift the position of the current character left by "count" times
pseudo code :
function adjust_spaces(char s[])
{
redundant_spaces_count = 0;
for(int i=0;i<s.length();i++)
{
if(redundant_space(s[i]))
redundant_spaces_deleted++;
else
s[i-redundant_spaces_deleted] = s[i];
}
}
How about using a linked list with O(n)
Node result = new Node(); Node before = null;
for (Node character = start; character != null; character = character.next)
{
if (character.data == ' ' && before == null)
{
before = character.next; result = character.next;
}
else if (character.data == ' ')
{
before.next = character.next;
}
else
before = character;
}
void Replaces(char* str)
{
int it =0;
int in=0;
bool iswhite = false;
while( str[it] != '\0')
{
if( str[it] == ' ' )
it++;
else
break;
}
while( str[it] != '\0')
{
if(( iswhite) && (str[it] == ' '))
{
it++;
}
else
{
str[in] = str[it];
if( str[it] == ' ')
iswhite = true;
else
iswhite = false;
it++;
in++;
}
}
if( iswhite)
str[--in] = '\0';
else
str[in] = '\0';
}
What's the time complexity time of my code?
public class RemoveWhiteSpace {
public static void removeSpace(char[] string) {
int spacePtr = 0;
while(spacePtr < string.length && string[spacePtr] != ' '){
spacePtr++;
}
int index = 0;
for(index = spacePtr;index < string.length; index++){
if(string[index] == ' '){
continue;
}else{
swap(string,spacePtr,index);
while(spacePtr < string.length && string[spacePtr] != ' '){
spacePtr++;
}
}
}
while(spacePtr < string.length){
string[spacePtr] = 0;
spacePtr++;
}
for(char c:string){
System.out.print(c);
}
System.out.println();
}
private static void swap(char[] string, int spacePtr, int index) {
char tmp = string[spacePtr];
string[spacePtr] = string[index];
string[index] = tmp;
}
public static void main(String[] args) {
removeSpace(" ab bc d".toCharArray());
removeSpace("ab ed d d".toCharArray());
}
}
I think you can also use str.replaceAll(" "," ") // replace all double spaces with one space. Check if it starts or ends with a space , and delete it, if so.
whitespacecount=0;
- dhineshkumar007 December 19, 2012string= given string;
Step1: Iterate thro the string by each and every character.
If( string[i]==' ')
{
If (IsNotSplCharacter(string[i-1]))
whitespacecount++;
}
else
string[i-whitespacecount]= string[i];
Complexity is O(n).