Wrong Answer: Even when the expected output and program output are same.


  • 2
    S

    Can someone explain, what this implies?
    Thanks

    EDIT: Pasting the code as requested

    void reverseWords(string &str) {
    	int i = 0;
    	int len = str.length();
    	int nextraspaces = 0;
    	int ncurspace = 0;
    
    	// Find number of leading spaces
    	while (str[i] != NULL && str[i] == ' '){
    		nextraspaces++;
    		i++;
    	}
    
    	// Find no. of extra spaces between words and eliminate them
    	while (str[i] != NULL){
    		if (str[i] != ' '){
    			str[i - nextraspaces] = str[i];
    			ncurspace = 0;
    		}
    		else{
    			ncurspace++;
    			if (ncurspace == 1)
    				str[i - nextraspaces] = str[i];
    			else
    				nextraspaces++;
    		}
    		i++;
    	}
    	str[i - nextraspaces] = NULL;
    
    	// First reverse each word
    	i = 0;
    	int start = 0, end = 0;
    	while (str[i] != NULL){
    		if (str[i + 1] == ' ' || str[i + 1] == NULL){
    			end = i;
    			while (start < end){
    				char temp = str[start];
    				str[start] = str[end];
    				str[end] = temp;
    				start++, end--;
    			}
    			start = i + 2;
    		}
    		i++;
    	}
    
    	// Reverse the complete string
    	start = 0, end = i - 1;
    	while (start < end){
    		char temp = str[start];
    		str[start] = str[end];
    		str[end] = temp;
    		start++, end--;
    	}
    }
    

  • 0
    T

    where is code?
    It is impossible to answer your question without code.


  • 0
    S

    @turbocv code is not really required here!

    If you would click on the hyperlink labeled "this", you will see that the expected and actual output are exactly the same (atleast to me) and still the test case fails. So the moderators around, can you please comment on what's wrong here?


  • 0
    G

    I also got the same error.


  • 0
    S

    @shashank, providing code and clarify your algorithm would be better. This problem may causes due to your code, like stack memory overflow then returns some un-printable characters, or your implementation is against LeetCode's principle leads to unexpected result. Since so many people have passed this problem, it is probably not judger's fault. However, sorry to make this misunderstanding situation.


  • 0
    S

    @Shangrila Alright, makes sense. I have copied the function above.


  • 0
    S

    There is room for improvement in your code.

    I believe that there is some misunderstanding between char[] and string. Setting str[i] = NULL will do make the string print out like it erase the last characters, but as a type of string, the size of it won't change. I tried in your code, it will pass this test case you failed, but get wrong answer on another case.

    class Solution {
    public:
        void reverseWords(string &str) {
        int i = 0;
        int len = str.length();
        int nextraspaces = 0;
        int ncurspace = 0;
    
        // Find number of leading spaces
        while (str[i] != NULL && str[i] == ' '){
            nextraspaces++;
            i++;
        }
    
        // Find no. of extra spaces between words and eliminate them
        while (str[i] != NULL){
            if (str[i] != ' '){
                str[i - nextraspaces] = str[i];
                ncurspace = 0;
            }
            else{
                ncurspace++;
                if (ncurspace == 1)
                    str[i - nextraspaces] = str[i];
                else
                    nextraspaces++;
            }
            i++;
        }
        str[i - nextraspaces] = NULL;
        int newLen = i - nextraspaces;   // Here get the new length.
    
        // First reverse each word
        i = 0;
        int start = 0, end = 0;
        while (str[i] != NULL){
            if (str[i + 1] == ' ' || str[i + 1] == NULL){
                end = i;
                while (start < end){
                    char temp = str[start];
                    str[start] = str[end];
                    str[end] = temp;
                    start++, end--;
                }
                start = i + 2;
            }
            i++;
        }
    
        // Reverse the complete string
        start = 0, end = i - 1;
        while (start < end){
            char temp = str[start];
            str[start] = str[end];
            str[end] = temp;
            start++, end--;
        }
        str.erase(newLen, len - newLen);   // Finally erase the rest characters beyond the new length
    }
    };

Log in to reply
 

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