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

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 occurence 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find longest possible
// subsequence of s beginning with x and y
int 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 pairs
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 = max(count,
                        solve(s, i, j));
        }
    }
 
    // Return the answer
    return count;
}
 
// Driver Code
int 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to find longest possible
// subsequence of s beginning with x and y
static 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 pairs
static 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 Code
public 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach
 
# Function to find longest possible
# subsequence of s beginning with x and y
def 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 pairs
def 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 s
s = "100120013"
 
# Find the size of the string
n = len(s)
 
# Function call
answer = find_min(s)
 
# This value is the count of
# minimum element to be removed
print(n - answer)
 
# This code is contributed by Shivam Singh

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using System;
class GFG{
 
// Function to find longest possible
// subsequence of s beginning with x and y
static 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 pairs
static 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 Code
public 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

chevron_right


Output: 

5





 

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


Eat Sleep Code Repeat

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.