Length of largest subsequence consisting of a pair of alternating digits | Java program for the above approach ; 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 it otherwise print 0 ; Driver Code ; Given String ; Function call
import java . util . * ; class GFG { static void largestSubsequence ( char [ ] s ) { int maxi = 0 ; char prev1 ; for ( int i = 0 ; i < 10 ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { if ( i != j ) { int len = 0 ; prev1 = ( char ) ( j + '0' ) ; for ( int k = 0 ; k < s . length ; k ++ ) { if ( s [ k ] == i + '0' && prev1 == j + '0' ) { prev1 = s [ k ] ; len ++ ; } else if ( s [ k ] == j + '0' && prev1 == i + '0' ) { prev1 = s [ k ] ; len ++ ; } } maxi = Math . max ( len , maxi ) ; } } } if ( maxi != 1 ) System . out . print ( maxi + "NEW_LINE"); else System . out . print ( 0 + "NEW_LINE"); } public static void main ( String [ ] args ) { String s = "1542745249842" ; largestSubsequence ( s . toCharArray ( ) ) ; } }