Can anyone please tell me what does this error mean?


  • 0
    Z

    Input: " "
    Output: ""
    Expected: ""

    I really can't see the difference between my output and the expected value;

    Here is the code. I just push the first char of each word into a stack and pull them out.

            char* stack[5000];
            char** stackTop=&stack[0];
            int index=0;
            int counter=0;
            char* toPush=NULL;
            for(;index<=s.size();index++){
                if(((s[index]!=' ')&&(s[index]!='\0'))&&(toPush==NULL)){
                    toPush=&s[index];
                }
                if(((s[index]==' ')||s[index]=='\0')&&(toPush!=NULL)){
                    *stackTop=toPush;
                    stackTop++;
                    toPush=NULL;
                    counter++;
                }
            }
            
            string newS(s.size(),'\0');
            int newSIndex=0;
            for(index=0;index<counter;index++){
                stackTop--;
                char* valueInS=*stackTop;
                while((*valueInS!=' ')&&(*valueInS!='\0')){
                    newS[newSIndex]=*valueInS;
                    newSIndex++;
                    valueInS++;
                }
                if(index!=counter-1)
                {
                    newS[newSIndex]=' ';
                    newSIndex++;
                }
            }
            s=newS;

  • 0
    M

    Assuming you have shown us exactly what you submitted, the reason you are failing is that you inserted unprintable characters into the string. They do not appear when printed, so the strings look the same, but the additional values cause comparison methods to declare them unequal. Try using cout<<(int)c <<endl for each character as you pop them off the stack. You will almost certainly get some unexpected values from your empty string.


  • 0
    Z

    Thank you very much!!!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.