两串旋转
如果對于一個字符串A,將A的前面任意一部分挪到后邊去形成的字符串稱為A的旋轉詞。比如A=”12345”,A的旋轉詞有”12345”,”23451”,”34512”,”45123”和”51234”。對于兩個字符串A和B,請判斷A和B是否互為旋轉詞。
給定兩個字符串A和B及他們的長度lena,lenb,請返回一個bool值,代表他們是否互為旋轉詞。
測試樣例:
"cdab",4,"abcd",4
返回:true
參考文章:從頭到尾徹底理解KMP https://www.nowcoder.com/study/vod/1/1/4
參考答案
python版
(只是不是到用 python in 來判斷是否有匹配也可以,不是說好要KMP的嗎?囧~)
-- coding:utf-8 --
class Rotation:
def chkRotation(self, A, lena, B, lenb):
write code here
if lena != lenb:return Falseelse:combineStr = A + Aprint combineStrif B in combineStr:return Trueelse:return FalseJava版
import java.util.*;
public class Rotation {
public boolean chkRotation(String a, int lena, String b, int lenb) {
if (a == null || b == null || lena != lenb) {
return false;
}
String b2 = b + b;
return getIndexOf(b2, a) != -1;
}
}
轉載于:https://blog.51cto.com/13545923/2054473
總結
- 上一篇: Objective-C 和 Swift
- 下一篇: java文件传输