📌  相关文章
📜  Python程序检查字符串是否相互旋转

📅  最后修改于: 2022-05-13 01:55:25.691000             🧑  作者: Mango

Python程序检查字符串是否相互旋转

给定一个字符串s1 和一个字符串s2,写一个片段来说明 s2 是否是 s1 的旋转?
(例如,给定 s1 = ABCD 和 s2 = CDAB,返回真,给定 s1 = ABCD,和 s2 = ACBD,返回假)


算法:
areRotations(str1, str2)

1. Create a temp string and store concatenation of str1 to
       str1 in temp.
                          temp = str1.str1
    2. If str2 is a substring of temp then str1 and str2 are 
       rotations of each other.

    Example:                 
                     str1 = "ABACD"
                     str2 = "CDABA"

     temp = str1.str1 = "ABACDABACD"
     Since str2 is a substring of temp, str1 and str2 are 
     rotations of each other.
Python
# Python program to check if strings are rotations of
# each other or not
  
# Function checks if passed strings (str1 and str2)
# are rotations of each other
def areRotations(string1, string2):
    size1 = len(string1)
    size2 = len(string2)
    temp = ''
  
    # Check if sizes of two strings are same
    if size1 != size2:
        return 0
  
    # Create a temp string with value str1.str1
    temp = string1 + string1
  
    # Now check if str2 is a substring of temp
    # string.count returns the number of occurrences of
    # the second string in temp
    if (temp.count(string2)> 0):
        return 1
    else:
        return 0
  
# Driver program to test the above function
string1 = "AACD"
string2 = "ACDA"
  
if areRotations(string1, string2):
    print "Strings are rotations of each other"
else:
    print "Strings are not rotations of each other"
  
# This code is contributed by Bhavya Jain



输出:
Strings are rotations of each other

使用的库函数:
字符串:
strstr 在 string 中查找子字符串。
原型:char * strstr(const char *s1, const char *s2);
有关详细信息,请参阅 http://www.lix.polytechnique.fr/Labo/Leo.Liberti/public/computing/prog/c/C/MAN/strstr.htm

字符串:
strncat 连接两个字符串
原型:char *strcat(char *dest, const char *src);
有关详细信息,请参阅 http://www.lix.polytechnique.fr/Labo/Leo.Liberti/public/computing/prog/c/C/MAN/strcat.htm

时间复杂度:这个问题的时间复杂度取决于 strstr函数的实现。
如果 strstr 的实现是使用 KMP 匹配器完成的,则上述程序的复杂度为 (-)(n1 + n2) 其中 n1 和 n2 是字符串的长度。 KMP 匹配器花费 (-)(n) 时间在长度为 n 的字符串中查找子字符串,其中假设子字符串的长度小于字符串。
请参阅 A Program 上的完整文章以检查字符串是否相互旋转以获取更多详细信息!