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: 3
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: 5
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:
- 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 occurrence 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:
C++
// 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; } |
Java
// 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 |
Python3
# 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 |
C#
// 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 |
Javascript
<script> // JavaScript program for the above approach // Function to find longest possible // subsequence of s beginning with x and y function solve(s, x, y) { let res = 0; // Iterate over the string for (let c of s) { 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 function find_min(s) { let count = 0; for (let i = 0; i < 10; i++) { for (let j = 0; j < 10; j++) { // Update count count = Math.max(count, solve(s, i, j)); } } // Return the answer return count; } // Driver Code // Given string s let s = "100120013" ; // Find the size of the string let n = s.length; // Function Call let answer = find_min(s); // This value is the count of // minimum element to be removed document.write(n - answer); // This code is contributed by Surbhi Tyagi. </script> |
5
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!