while (getline(ss, t, ' ')) { reverse(t.begin(), t.end()); ans += t + ' '; } // We will take substring of one less size as there will be space after last word which is not required. return ans.substr(0, ans.size() - 1); } };
Java Code
Press enter or click to view image in full size
Python Code
class Solution: def reverseWords(self, s: str) -> str: return ' '.join(word[::-1] for word in s.split())
Output
Press enter or click to view image in full size
Press enter or click to view image in full size
That’s it for today’s question. Let me know if you need anything else to be there in this blog. Thank you💕