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: 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++
#include <bits/stdc++.h>
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;
}
|
Java
import java.util.*;
class GFG{
static int solve(String s, int x, int y)
{
int res = 0 ;
for ( char c : s.toCharArray())
{
if (c - '0' == x)
{
res++;
x = x+y;
y = x-y;
x = x-y;
}
}
if (x != y && res % 2 == 1 )
--res;
return res;
}
static int find_min(String s)
{
int count = 0 ;
for ( int i = 0 ; i < 10 ; i++)
{
for ( int j = 0 ; j < 10 ; j++)
{
count = Math.max(count,
solve(s, i, j));
}
}
return count;
}
public static void main(String[] args)
{
String s = "100120013" ;
int n = s.length();
int answer = find_min(s);
System.out.print((n - answer));
}
}
|
Python3
def solve(s, x, y):
res = 0
for c in s:
if ( ord (c) - ord ( '0' ) = = x):
res + = 1
x, y = y, x
if (x ! = y and res % 2 = = 1 ):
res - = 1
return res
def find_min(s):
count = 0
for i in range ( 10 ):
for j in range ( 10 ):
count = max (count, solve(s, i, j))
return count
s = "100120013"
n = len (s)
answer = find_min(s)
print (n - answer)
|
C#
using System;
class GFG{
static int solve(String s, int x, int y)
{
int res = 0;
foreach ( char c in s.ToCharArray())
{
if (c - '0' == x)
{
res++;
x = x + y;
y = x - y;
x = x - y;
}
}
if (x != y && res % 2 == 1)
--res;
return res;
}
static int find_min(String s)
{
int count = 0;
for ( int i = 0; i < 10; i++)
{
for ( int j = 0; j < 10; j++)
{
count = Math.Max(count,
solve(s, i, j));
}
}
return count;
}
public static void Main(String[] args)
{
String s = "100120013" ;
int n = s.Length;
int answer = find_min(s);
Console.Write((n - answer));
}
}
|
Javascript
<script>
function solve(s, x, y)
{
let res = 0;
for (let c of s) {
if (c - '0' == x) {
res++;
x = x+y;
y = x-y;
x = x-y;
}
}
if (x != y && res % 2 == 1)
--res;
return res;
}
function find_min(s)
{
let count = 0;
for (let i = 0; i < 10; i++) {
for (let j = 0; j < 10; j++) {
count = Math.max(count,
solve(s, i, j));
}
}
return count;
}
let s = "100120013" ;
let n = s.length;
let answer = find_min(s);
document.write(n - answer);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
08 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...