Bloomberg LP Amazon Interview Question
Financial Software Developers Software Engineer / DevelopersReplace and? Scoot over the other chars? This requires careful coding and it would be easy to go wrong. I would not be surprised if the code I attempt below is wrong.
Idea is, basically go through the string, and when we hit a space, go in a loop to ignore the next space chars. We maintain two pointers, one which goes through given string, and the other which corresponds to what the final string would be.
void trimspaces(char *s) {
char *curRead;
char *curCopy;
int foundSpace = 0;
if (s == NULL || *s == NULL) { return;}
curRead = s;
curCopy = s;
while (*curRead != NULL){
// The previous character was a space.
// Go into loop ignoring the spaces.
if (foundSpace) {
while(*curRead == ' ') {
curRead++;
}
// Reached the end of the string.
if (*curRead == NULL) {
break;
}
foundSpace = 0;
}
if (*curRead == ' ' && !foundSpace) {
foundSpace = 1;
}
*curCopy = *curRead;
curCopy++;
curRead++;
}
// Copy over the NULL byte.
*curCopy = NULL;
}
A generic solution to replace anything
strReplaceAllOccurance(std::string strBaseString,std::string strToFind,std::string strToReplace)
{
int intDotLocation;
//Search the complete string for strToFind Recursively and replace it with required string
while((intDotLocation = strBaseString.find_first_of(strToFind)) != -1)
strBaseString.replace(intDotLocation,1,strToReplace);
return strBaseString;
}
protected String trimSpaces(String value){
StringTokenizer tokenize = new StringTokenizer (value);
StringBuilder returnValue = null;
while( tokenize.hasNext()){
returnValue.append(tokenize.next()+" ");
}
return returnValue.toString().trim();
}
void PruneSpaces(char* szStr)
{
size_t len = strlen(szStr);
size_t writePos = 0;
for (size_t i=1;i<=len;i++)
{
if ((szStr[i] != ' ') || (szStr[writePos] != ' '))
{
szStr[++writePos]=szStr[i];
}
}
}
string PruneSpace(string s)
{
if(s== null || s.Length == 0)
return s;
StringBuilder sb = new StringBuilder(s);
int pos = 0;
int spaceIndex;
while (pos < sb.Length)
{
spaceIndex = sb.ToString().IndexOf(' ', pos);
pos = spaceIndex + 1;
while (sb[pos] == ' ')
pos++;
sb.Replace(' ', '', spaceIndex, pos - spaceIndex);
pos = spaceIndex + 1;
}
return sb.ToString();
}
#include <stdio.h>
#include <string.h>
#include <malloc.h>
void RemoveSpace( char *str)
{
char *tmp = (char*)malloc(sizeof(str));
strcpy(tmp, str);
char *s1;
s1 = strtok(tmp," ");
while(s1 != NULL)
{
printf("%s", s1);
s1 = strtok(NULL, " ");
}
}
public class ReplaceMultipleSpaces {
public static void main(String args[]) {
String str = "Hi This is Code for replacing multiple Spaces ";
String str2 = "";
int cnt = 0;
System.out.println(str);
char[] strArr = str.toCharArray();
for (int i = 0; i < strArr.length; i++) {
if (strArr[i] == ' ')
cnt++;
else
cnt = 0;
if (cnt > 1)
continue;
str2 = str2 + strArr[i];
}
System.out.println(str2);
}
}
package string;
public class ReplaceMultipleStrings {
public static void main(String[] args) {
System.out.println(replaceMultSpace("Suha a s abc wedd"));
}
static String replaceMultSpace(String inp) {
if(inp == null || inp.isEmpty())
return null;
int len = inp.length();
for(int i=0;i<len;i++) {
if(inp.charAt(i)==' ') {
int spCount = 1;
for(int j=i+1;j<len;j++) {
if(inp.charAt(j)==' '){
spCount++;
}
else
break;
}
if(spCount>1) {
inp = inp.substring(0,i)+" "+inp.substring(i+spCount,len);
len = inp.length();
}
}
}
return inp;
}
}
Space Complexity O(1) and time complexity O(n)
void ReplaceMultipleSpaces(char* str)
{
int nCount=0;
int nPrev=0;
while(str[nCount]!=NULL && str[nCount+1]!=NULL)
{
if(str[nCount]==' '&& str[nCount+1]==' ')
nPrev++;
else
str[nCount+1-nPrev]=str[nCount+1];
nCount++;
}
str[nCount+1-nPrev]='\0';
}
public static void main(String[] args) {
// TODO Auto-generated method stub
removespaces("This is an example string");
}
public static void removespaces(String str)
{
char t[]=str.toCharArray();
int spaceindex=0;
int count=0;
for(int i=0;i<t.length;i++)
{
if(t[i]==' ')
{
count++;
continue;
}
else
{
if(count>0)
{
t[spaceindex]=' ';
spaceindex++;
count=0;
}
t[spaceindex]=t[i];
spaceindex++;
}
}
String temp=new String(t);
System.out.println(temp.substring(0,spaceindex));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
removespaces("This is an example string");
}
public static void removespaces(String str)
{
char t[]=str.toCharArray();
int spaceindex=0;
int count=0;
for(int i=0;i<t.length;i++)
{
if(t[i]==' ')
{
count++;
continue;
}
else
{
if(count>0)
{
t[spaceindex]=' ';
spaceindex++;
count=0;
}
t[spaceindex]=t[i];
spaceindex++;
}
}
String temp=new String(t);
System.out.println(temp.substring(0,spaceindex));
}
- m3th0d.itbhu April 09, 2013