字符串轮转。给定两个字符串s1和s2,请编写代码检查s2是否为s1旋转而成(比如,waterbottle是erbottlewat旋转后的字符串)。
示例1:
输入:s1 = "waterbottle", s2 = "erbottlewat"
输出:True
示例2:
输入:s1 = "aa", s2 = "aba"
输出:False
提示:
字符串长度在[0, 100000]范围内。
说明:
你能只调用一次检查子串的方法吗?
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/string-rotation-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
方法一:
不用库函数,自己实现,也是双百0ms
func isFlipedString(s1 string, s2 string) bool {
if len(s1) == len(s2) && len(s1) == 0 {
return true
}
if len(s1) != len(s2) {
return false
}
sb1 := []byte(s1)
sb2 := []byte(s2)
for i:=0;i<len(sb1);i++ {
if sb2[0] == sb1[i] {
t1 := sb1[i:]
t2 := sb2[:len(t1)]
if string(t1) == string(t2) {
if string(sb1[:i]) == string(sb2[len(t1):]) {
return true
}
}
}
}
return false
}
方法二:
库函数实现:
func isFlipedString(s1 string, s2 string) bool {
if len(s1) != len(s2) {
return false
}
if s1 == s2 {
return true
}
s1 += s1
return strings.Contains(s1, s2)
}