LET'S TALK TECHNICAL

This blog is intended to help people prepare for the job interviews and improve their analytical skills. We have posted difficult datastructures and algorithm questions and puzzles. Interview experiences section is for the people to post their interview experiences.Views expressed here are of their personal and the blog author doesn't take any responsibility for the same.

-

Followers

Jobs

Tuesday, February 24, 2009

Given a string s1 and a string s2, write a snippet to say whether s2 is a rotation of s1 using only one call to strstr routine?

(eg given s1 = ABCD and s2 = CDAB, return true)
(given s1 = ABCD, and s2 = ACBD , return false)

Sol:
This is most frequently asked question. It can be solved with simple logic as given below.

1 . Check if both s1, s2 are of same length.
2. if( strstr(strcat(s1,s1), s2) !=NULL) return TRUE
else
return FALSE

Example:
if s1 = ABCD, the concatination will make it ABCDABCD and hence any rotation will fall within the concatenated string. s2 is italicized in conctenized in above concatenated string.

Popular Posts