Minimum digits to be removed to make either all digits or alternating digits same | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( string s , int x , int y ) { int res = 0 ; for ( auto c : s ) { if ( c - '0' == x ) { res ++ ; swap ( x , y ) ; } } if ( x != y && res % 2 == 1 ) -- res ; return res ; } int find_min ( string s ) { int count = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { count = max ( count , solve ( s , i , j ) ) ; } } return count ; } int main ( ) { string s = "100120013" ; int n = s . size ( ) ; int answer = find_min ( s ) ; cout << ( n - answer ) ; return 0 ; }