Skip to content

Latest commit

 

History

History
37 lines (27 loc) · 753 Bytes

101.md

File metadata and controls

37 lines (27 loc) · 753 Bytes
@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
[上一个例子](100.md) [下一个例子](102.md)