跳转至

🔥AI副业赚钱星球

点击下面图片查看

郭震AI
@author jackzhenguo
@desc 
@date 2019/9/4

101 str1是否由str2旋转而来

stringbook旋转后得到bookstring,写一段代码验证str1是否为str2旋转得到。

思路

转化为判断:str1是否为str2+str2的子串

def is_rotation(s1: str, s2: str) -> bool:
    if s1 is None or s2 is None:
        return False
    if len(s1) != len(s2):
        return False

    def is_substring(s1: str, s2: str) -> bool:
        return s1 in s2
    return is_substring(s1, s2 + s2)

测试

r = is_rotation('stringbook', 'bookstring')
print(r)  # True

r = is_rotation('greatman', 'maneatgr')
print(r)  # False

上一个例子 下一个例子

大家在看

京ICP备20031037号-1 | AI之家 | AI资讯 | Python200 | 数据分析