while the question says there can be multiple spaces and your solution only takes care of double spaces, second when there are questions like these the person asking the question expects the implementation and not the function so using string.reverse() is bit of a shortcut.
I would try to implement a queue and a stack and read the string from right to left, emptying the stack when hitting space and making sure I handle the case when I've already emptied the stack.
So during multiple space, first space will empty the stack, second will check that stack is empty so will simply move on. This approach may also handle trailing and leading space.
One special case would be the end of string where we empty the stack once again and have the result.
There can be multiple ways to solve a given problem.
I have updated the code. And, now all edge cases are being taken care of. Leading spaces, trailing spaces, multiple spaces. Built-in method reverse() is replaced by implementation of reverse().
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
while the question says there can be multiple spaces and your solution only takes care of double spaces, second when there are questions like these the person asking the question expects the implementation and not the function so using string.reverse() is bit of a shortcut.
I would try to implement a queue and a stack and read the string from right to left, emptying the stack when hitting space and making sure I handle the case when I've already emptied the stack.
So during multiple space, first space will empty the stack, second will check that stack is empty so will simply move on. This approach may also handle trailing and leading space.
One special case would be the end of string where we empty the stack once again and have the result.
There can be multiple ways to solve a given problem.
I have updated the code. And, now all edge cases are being taken care of. Leading spaces, trailing spaces, multiple spaces. Built-in method reverse() is replaced by implementation of reverse().