Length of largest subsequence consisting of a pair of alternating digits | Function to find the length of the largest subsequence consisting of a pair of alternating digits ; Variable initialization ; Nested loops for iteration ; Check if i is not equal to j ; Initialize length as 0 ; Iterate from 0 till the size of the string ; Increment length ; Increment length ; Update maxi ; Check if maxi is not equal to 1 the print otherwise pr0 ; Driver Code ; Given string ; Function call
def largestSubsequence ( s ) : NEW_LINE INDENT maxi = 0 NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT for j in range ( 10 ) : NEW_LINE INDENT if ( i != j ) : NEW_LINE INDENT lenn = 0 NEW_LINE prev1 = chr ( j + ord ( '0' ) ) NEW_LINE for k in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ k ] == chr ( i + ord ( '0' ) ) and prev1 == chr ( j + ord ( '0' ) ) ) : NEW_LINE INDENT prev1 = s [ k ] NEW_LINE lenn += 1 NEW_LINE DEDENT elif ( s [ k ] == chr ( j + ord ( '0' ) ) and prev1 == chr ( i + ord ( '0' ) ) ) : NEW_LINE INDENT prev1 = s [ k ] NEW_LINE lenn += 1 NEW_LINE DEDENT DEDENT maxi = max ( lenn , maxi ) NEW_LINE DEDENT DEDENT DEDENT if ( maxi != 1 ) : NEW_LINE INDENT print ( maxi ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( 0 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "1542745249842" NEW_LINE largestSubsequence ( s ) NEW_LINE DEDENT