Number of Counterclockwise shifts to make a string palindrome | Function to check if given string is palindrome or not . ; Function to find counter clockwise shifts to make string palindrome . ; Pointer to starting of current shifted string . ; Pointer to ending of current shifted string . ; Concatenate string with itself ; To store counterclockwise shifts ; Move left and right pointers one step at a time . ; Check if current shifted string is palindrome or not ; If string is not palindrome then increase count of number of shifts by 1. ; Driver code .
def isPalindrome ( str , l , r ) : NEW_LINE INDENT while ( l < r ) : NEW_LINE INDENT if ( str [ l ] != str [ r ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT l += 1 NEW_LINE r -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT def CyclicShifts ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE left = 0 NEW_LINE right = n - 1 NEW_LINE str = str + str NEW_LINE cnt = 0 NEW_LINE while ( right < 2 * n - 1 ) : NEW_LINE INDENT if ( isPalindrome ( str , left , right ) ) : NEW_LINE INDENT break NEW_LINE DEDENT cnt += 1 NEW_LINE left += 1 NEW_LINE right += 1 NEW_LINE DEDENT return cnt NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " bccbbaab " ; NEW_LINE print ( CyclicShifts ( str ) ) NEW_LINE DEDENT