Efficient way to verify if a string is a rotated palindrome?

后端 未结 5 650
萌比男神i
萌比男神i 2021-01-12 03:49

A rotated palindrome is like \"1234321\", \"3432112\". The naive method will be cut the string into different pieces and concate them back and see if the string is a palindr

5条回答
  •  鱼传尺愫
    2021-01-12 04:35

    #Given a string, check if it is a rotation of a palindrome. 
    #For example your function should return true for “aab” as it is a rotation of “aba”.
    string1 = input("Enter the first string")
    def check_palindrome(string1):
        #string1_list = [word1 for word1 in string1]
        #print(string1_list)
        string1_rotated = string1[1::1] + string1[0]
        print(string1_rotated)
        string1_rotated_palindrome = string1_rotated[-1::-1]
        print(string1_rotated_palindrome)
        if string1_rotated == string1_rotated_palindrome:
            return True
        else:
            return False
    isPalindrome = check_palindrome(string1)
    if(isPalindrome):
        print("Rotated string is palindrome as well")
    else:
        print("Rotated string is not palindrome")
    

提交回复
热议问题