I have this string s1 = "My name is X Y Z"
and I want to reverse the order of the words so that s1 = "Z Y X is name My"
.
I can do it using an additional array. I thought hard but is it possible to do it inplace (without using additional data structures) and with the time complexity being O(n)?
Here is the Java Implementation:
This is assuming all words are separated by spaces:
In Smalltalk:
I know noone cares about Smalltalk, but it's so beautiful to me.
In Python, if you can't use [::-1] or reversed(), here is the simple way:
}