i don't know why i'm getting wrong answer for my this code, please help me anyone🙂.
Link for this question is reverse-words-in-a-string
class Solution {
public:
string reverseWords(string s) {
int i = s.size()-1;
string ans = "";
while(i>=0){
while(i>=0 && s[i] == ' ') i--;
if(i<0){
break;
}
int j = i;
while(i>=0 && s[i] != ' ') i--;
// cout << s.substr(i+1, j+1) << " i = " << i << ", j = " << j << endl;
if(ans.empty()){
ans.append(s.substr(i+1, j+1));
}else{
ans.append(" " + s.substr(i+1, j+1));
}
}
return ans;
}
};
Wrong Answer
Runtime: 0 ms
Your input
"the sky is blue"
Output
"blue is blue sky is the"
Expected
"blue is sky the"