Minimum digits to be removed to make either all digits or alternating digits same | Java program for the above approach ; Function to find longest possible subsequence of s beginning with x and y ; Iterate over the String ; Increment count ; Swap the positions ; Return the result ; Function that finds all the possible pairs ; Update count ; Return the answer ; Driver Code ; Given String s ; Find the size of the String ; Function Call ; This value is the count of minimum element to be removed
import java . util . * ; class GFG { static int solve ( String s , int x , int y ) { int res = 0 ; for ( char c : s . toCharArray ( ) ) { if ( c - '0' == x ) { res ++ ; x = x + y ; y = x - y ; x = x - y ; } } if ( x != y && res % 2 == 1 ) -- res ; return res ; } static int find_min ( String s ) { int count = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { count = Math . max ( count , solve ( s , i , j ) ) ; } } return count ; } public static void main ( String [ ] args ) { String s = "100120013" ; int n = s . length ( ) ; int answer = find_min ( s ) ; System . out . print ( ( n - answer ) ) ; } }