1.8

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”).


这是算法导论上的一道习题:假设开始前字符为AB,旋转后字符为BA,AB之间为分割的位置,当我们将旋转后的字符连在一起为:BABA,所以我们只需要在连在一起的字符中查找是否含有AB子串,首先连接,然后判断。1、朴素的查找 2、kmp算法

你可能感兴趣的:(1.8)