LC: Reverse Words in a String II - spiralgo/algorithms GitHub Wiki
The Essence:
When the entire data is reversed, all the words in the string will be reversed too. Therefore, since each word can be recognized by space between them, each individual word can also be reversed in its own range. With this, all words will be reversed.
Like in this case, solutions are sometimes achieved through creating temporary problems, and then finding a way to solve and even make use of these temporary problems.
Details:
The solution can be implemented with two methods reverse(i,j) and reverseEachWord(). Initially, the entire data is reversed. The second method finds the indices of the words and calls the reverse(start,end) function to reverse these.
Built-in methods of the programming language of choice can also be preferred.
The code implementations can be found here: https://github.com/spiralgo/algorithms/tree/master/src/main/java/algorithms/curated170/medium/reversewordsinastring2