DEV Community

Discussion on: Day-15 Reverse words in a string

Collapse
 
ri5hirajp profile image
Rishiraj Purohit

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.

Collapse
 
mridubhatnagar profile image
Mridu Bhatnagar

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().