Why this code is getting run time error? It is showing-> Last executed input: ""


  • 0
    D

    class Solution {
    public:
    void reverseWords(string &str) {
    string temp;
    stack <string> st;
    for(int i=0;i<str.length();i++)
    {
    if(str[i]== ' ')
    {
    st.push(temp);
    temp.clear();
    continue;
    }
    temp.push_back(str[i]);
    }
    st.push(temp);
    str= "";

    while(!st.empty())
    {
        str+=" ";
        str+= st.top();
        st.pop();
    }
     while (str.size()>0 && str[0]==' ')
    {
        str.erase(0,1); //leading space
    }
    for(int i=0; i<str.size(); i++)
    {
        while( str[i]==' ' &&i<str.size()-1 && str[i+1]==' ')
        {
            str.erase(i,1); //multiple space
        }
    
    }
    str.erase(str.size()-1,1); //remove last space
    
    }
    

    };


Log in to reply
 

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