Cracking The Coding Interview/Q 1.8

From Software Engineers Wiki
Jump to: navigation, search

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

Answer

The simple trick is to create another string with two s1 string concatenated. Then call isSubstring(concatenated_str, s2) which will find that s2 is a rotation of s1.

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox