Given a String S or length N, reverse the string without reversing its individual words. Words are separated by single whitespace.
Input: "this is an amazing program", Output: "program amazing an is this"
Input: "The purpose of our lives is to be happy", Output: "happy be to is lives our of purpose The"
Solutions
Method 1: In O(n) time
We can easily solve this problem in O(n) time and O(n) extra space.
The idea is to first reverse the entire string and then reverse only the individual words.
Complexity
The time complexity of this solution is O(N) and space complexity is O(N).