19 April 2012

Reverse word's of a string in O(1) space.

Reverse the words of a string
Eg:: if string is GNU programs are the best
Output = best the are programs GNU



Solution : 
If the string is : "How are you."
1. Reverse the entire string :  "uoy era woh "
Time Complexity of 1. is O(n)

2. Reverse the individual words again.
you are how.

No comments:

Post a Comment