# Minimum digits to be removed to make either all digits or alternating digits same

• Difficulty Level : Hard
• Last Updated : 22 Jul, 2022

Given a numeric string str, the task is to find the minimum number of digits to be removed from the string such that it satisfies either of the below conditions:

• All the elements of the string are the same.
• All the elements at even position are same and all the elements at the odd position are same, which means the string is alternating with the equal occurrence of each digit.

Examples:

Input: s = “95831”
Output:
Explanation:
In this examples, remove any three elements form the string to make it alternating, i.e. “95” has 9 at even index and 5 at odd index and hence it satisfies second condition.
Input: s = “100120013”
Output:
Explanation:
In this case, either make the string 0000 or make the string 1010. In both the cases the minimum element must be removed from the string will be 5

Approach: The idea is to use the Greedy Approach. Below are the steps:

1. Since all the characters in the resultant string are alternating and same then the smallest substring of distinct digits will be of length 2.
2. As, there are only 10 different types of digits that are from 0 to 9. The idea is to iterate every possible string of length 2 and find the occurrence of subsequence formed by them.
3. Hence find all possible combinations of the first and the second character of the string of the above two digit string and greedily construct the longest possible sub-sequence of s beginning with those characters.
4. The difference between string length and the maximum length of the subsequence with alternating digit in the above step is the required result.

Below is the implementation of the above approach:

## C++

 // C++ program for the above approach #include using namespace std; // Function to find longest possible// subsequence of s beginning with x and yint solve(string s, int x, int y){    int res = 0;     // Iterate over the string    for (auto c : s) {        if (c - '0' == x) {             // Increment count            res++;             // Swap the positions            swap(x, y);        }    }     if (x != y && res % 2 == 1)        --res;     // Return the result    return res;} // Function that finds all the// possible pairsint find_min(string s){    int count = 0;    for (int i = 0; i < 10; i++) {         for (int j = 0; j < 10; j++) {             // Update count            count = max(count,                        solve(s, i, j));        }    }     // Return the answer    return count;} // Driver Codeint main(){    // Given string s    string s = "100120013";     // Find the size of the string    int n = s.size();     // Function Call    int answer = find_min(s);     // This value is the count of    // minimum element to be removed    cout << (n - answer);    return 0;}

## Java

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

## Python3

 # Python3 program for the above approach # Function to find longest possible# subsequence of s beginning with x and ydef solve(s, x, y):     res = 0     # Iterate over the string    for c in s:        if(ord(c) - ord('0') == x):             # Increment count            res += 1             # Swap the positions            x, y = y, x     if(x != y and res % 2 == 1):        res -= 1     # Return the result    return res # Function that finds all the# possible pairsdef find_min(s):     count = 0    for i in range(10):        for j in range(10):             # Update count            count = max(count, solve(s, i, j))     # Return the answer    return count # Driver Code # Given string ss = "100120013" # Find the size of the stringn = len(s) # Function callanswer = find_min(s) # This value is the count of# minimum element to be removedprint(n - answer) # This code is contributed by Shivam Singh

## C#

 // C# program for the above approachusing System;class GFG{ // Function to find longest possible// subsequence of s beginning with x and ystatic int solve(String s, int x, int y){    int res = 0;     // Iterate over the String    foreach (char c in s.ToCharArray())    {        if (c - '0' == x)        {             // Increment count            res++;             // Swap the positions            x = x + y;            y = x - y;            x = x - y;        }    }     if (x != y && res % 2 == 1)        --res;     // Return the result    return res;} // Function that finds all the// possible pairsstatic int find_min(String s){    int count = 0;    for (int i = 0; i < 10; i++)    {        for (int j = 0; j < 10; j++)        {             // Update count            count = Math.Max(count,                             solve(s, i, j));        }    }     // Return the answer    return count;} // Driver Codepublic static void Main(String[] args){    // Given String s    String s = "100120013";     // Find the size of the String    int n = s.Length;     // Function Call    int answer = find_min(s);     // This value is the count of    // minimum element to be removed    Console.Write((n - answer));}} // This code is contributed by gauravrajput1

## Javascript



Output:

5

Time Complexity: O(N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up