And sorry for the late response but my original solution in my previous post doesn't actually required 2 traversals of the complete string. A more clearer algo:- 1.Read original string character by character appending to another string.The string implementation can be that of the std. C++ string. 2.if space or null char encountered,push the *string(and not individual chars) *in the stack. 3.Now if null occured.pop each string from the stack till it is empty.
This way we do o(n+m) iterations where m is the number of words in the string.In real world scenario m<n. Still its not the best solution but I am not traversing the string twice as some earlier members have commented. -- Saurabh Singh B.Tech (Computer Science) MNNIT ALLAHABAD -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
