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.
Input: s = “95831”
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”
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:
- Since all the characters in the resultant string are alternating and same then the smallest substring of distinct digits will be of length 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.
- 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.
- 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:
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.
- Minimum number of digits to be removed so that no two consecutive digits are same
- Minimum value of X to make all array elements equal by either decreasing or increasing by X
- Color a grid such that all same color cells are connected either horizontally or vertically
- Minimum letters to be removed to make all occurrences of a given letter continuous
- Minimum swaps required to make a binary string alternating
- Minimum number of replacements to make the binary string alternating | Set 2
- Minimum number of flips with rotation to make binary string alternating
- Minimum number of elements to be removed so that pairwise consecutive elements are same
- Minimum number of characters to be removed to make a binary string alternate
- Minimum number of elements to be removed to make XOR maximum
- Minimum number of elements that should be removed to make the array good
- Find the minimum number of elements that should be removed to make an array good
- Minimum size substring to be removed to make a given string palindromic
- Number of digits to be removed to make a number divisible by 3
- Shortest subarray to be removed to make all Array elements unique
- Minimum steps for increasing and decreasing Array to reach either 0 or N
- Minimum Circles needed to be removed so that all remaining circles are non intersecting
- Print digit's position to be removed to make a number divisible by 6
- Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal
- Generate all possible strings such that char at index i is either str1[i] or str2[i]
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.