Longest subsequence of a number having same left and right rotation | Python3 program to implement the above approach ; Function to find the longest subsequence having equal left and right rotation ; Length of the string ; Iterate for all possible combinations of a two - digit numbers ; Check for alternate occurrence of current combination ; Increment the current value ; Increment the current value ; If alternating sequence is obtained of odd length ; Reduce to even length ; Update answer to store the maximum ; Return the answer ; Driver code
import sys NEW_LINE def findAltSubSeq ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE ans = - sys . maxsize - 1 NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT for j in range ( 10 ) : NEW_LINE INDENT cur , f = 0 , 0 NEW_LINE for k in range ( n ) : NEW_LINE INDENT if ( f == 0 and ord ( s [ k ] ) - ord ( '0' ) == i ) : NEW_LINE INDENT f = 1 NEW_LINE cur += 1 NEW_LINE DEDENT elif ( f == 1 and ord ( s [ k ] ) - ord ( '0' ) == j ) : NEW_LINE INDENT f = 0 NEW_LINE cur += 1 NEW_LINE DEDENT DEDENT if i != j and cur % 2 == 1 : NEW_LINE INDENT cur -= 1 NEW_LINE DEDENT ans = max ( cur , ans ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT s = "100210601" NEW_LINE print ( findAltSubSeq ( s ) ) NEW_LINE