Interview Question : Trim multiple consecutive spaces from a string

前端 未结 11 742
情书的邮戳
情书的邮戳 2020-12-08 17:34

This is an interview question Looking for best optimal solution to trim multiple spaces from a string. This operation should be in-place operation.

input  =          


        
相关标签:
11条回答
  • 2020-12-08 17:47

    I'd propose a little state machine (just a simple switch statement). Because if the interviewer is anything like me, the first enhancement they'll ask you to do is to fully trim any leading or trailing spaces, so that:

    "    leading and    trailing    "
    

    gets transformed to:

    "leading and trailing"
    

    instead of:

    " leading and trailing "
    

    This is a really simple modification to a state-machine design, and to me it seems easier to understand the state-machine logic in general over a 'straight-forward' coded loop, even if it takes a few more lines of code than a straight-forward loop.

    And if you argue that the modifications to the straight forward loop wouldn't be too bad (which can be reasonably argued), then I (as the interviewer) would throw in that I also want leading zeros from numbers to be be trimmed.

    On the other hand, a lot of interviewers might actually dislike a state-machine solution as being 'non-optimal'. I guess it depends on what you're trying to optimize.

    0 讨论(0)
  • 2020-12-08 17:53

    Keep two indices: The next available spot to put a letter in (say, i), and the current index you're examining (say, j).

    Just loop over all the characters with j, and whenever you see a letter, copy it to index i, then increment i. If you see a space that was not preceded by a space, also copy the space.

    I think this would work in-place...

    0 讨论(0)
  • 2020-12-08 17:57

    Here it is using only stdio:

    #include <stdio.h>
    
    int main(void){
        char str[] = "I    Like    StackOverflow a      lot";
        int i, j = 0, lastSpace = 0;
        for(i = 0;str[i]; i++){
            if(!lastSpace || str[i] != ' '){
                str[j] = str[i];
                j++;
            }
            lastSpace = (str[i] == ' ');
        }
        str[j] = 0;
        puts(str);
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-08 17:57
    void trimspaces(char * str){
        int i = 0;
        while(str[i]!='\0'){
            if(str[i]==' '){
                for(int j = i + 1; j<strlen(str);j++){
                    if(str[j]!=' '){
                        memmove(str + i + 1, str + j, strlen(str)-j+1);
                        break;
                    }
                }
            }
            i++;
        }
    }
    
    0 讨论(0)
  • 2020-12-08 17:57

    Functional variant in Haskell:

    import Data.List (intercalate)
    
    trimSpaces :: String -> String
    trimSpaces =  intercalate " " . words
    

    The algorithm the next:

    1. breaks a string up into a list of words, which were delimited by white space
    2. concatenate the list inserting one space between each element in list
    0 讨论(0)
提交回复
热议问题