Sunday, November 6, 2011

Interview Problem: if string s2 is a rotation of string s1

Problem:

Assume you have a method isSubstring which checks if one word is a substring of another. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring (i.e., “waterbottle” is a rotation of “erbottlewat”)

Solution:

1st , check it they share the same length.
2nd, concatenate s1 with itself, check if s2 is the substring of s1.

No comments :