Thursday, September 1, 2011

Reverse the order of words in a string


For example: Input string is " This is my first post"
                       Output string will be "post first my is This";

first reverse each word.(complexity o(n))
finally reverse the whole string.(complexity O(n))
total O(n)+O(n) = O(n)

How about pushing tokens using stack and then popping all.

No comments:

Post a Comment