Minimum deletions from string to reduce it to string with at most 2 unique characters
Last Updated :
21 Sep, 2022
Given a string containing lowercase English alphabets. The task is to find the minimum number of characters needed to be removed so that the remaining string contains at most 2 unique characters.
Note: The final string can have duplicate characters. The task is only to reduce the string with minimum deletions such that there can be a maximum of 2 unique characters in the resultant string.
Examples:
Input: S = “geeksforgeeks”
Output: 7
After removing 7 characters, the final string will be “geegee”
Input: S = “helloworld”
Output: 5
Approach:
First count the occurrences of each character within the given string, then just select two characters with the maximum occurrence, i.e. the two most frequently occurring characters in the string. And the result will be:
String length – (Occurrence of 1st most frequent character + Occurrence of 2nd most frequent character)
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int check(string s)
{
int i, j;
int fr[26] = {0} ;
int n = s.size() ;
for (i = 0; i < n; i++)
{
char x = s[i] ;
fr[x- 'a' ] += 1 ;
}
int minimum = INT_MAX;
for (i = 0 ; i < 26; i++)
{
for ( j = i + 1;j < 26; j++)
{
int z = fr[i] + fr[j] ;
minimum = min(minimum, n - z) ;
}
}
return minimum ;
}
int main()
{
string s = "geeksforgeeks" ;
cout << check(s) ;
}
|
Java
public class GFG{
static int check(String s)
{
int i,j;
int fr[] = new int [ 26 ] ;
int n = s.length() ;
for (i = 0 ; i < n; i++)
{
char x = s.charAt(i) ;
fr[x- 'a' ] += 1 ;
}
int minimum = Integer.MAX_VALUE;
for (i = 0 ; i < 26 ; i++)
{
for ( j = i + 1 ;j < 26 ; j++)
{
int z = fr[i] + fr[j] ;
minimum = Math.min(minimum, n-z) ;
}
}
return minimum ;
}
public static void main(String []args){
String s = "geeksforgeeks" ;
System.out.println(check(s)) ;
}
}
|
Python3
def check(s):
fr = [ 0 ] * 26
n = len (s)
for i in range (n):
x = ord (s[i])
fr[x - 97 ] + = 1
minimum = 99999999999
for i in range ( 26 ):
for j in range (i + 1 , 26 ):
z = fr[i] + fr[j]
minimum = min (minimum, n - z)
return minimum
s = "geeksforgeeks"
print (check(s))
|
C#
using System;
public class GFG{
static int check( string s)
{
int i,j;
int [] fr = new int [26] ;
int n = s.Length ;
for (i = 0; i < n; i++)
{
char x = s[i] ;
fr[x- 'a' ] += 1 ;
}
int minimum = int .MaxValue;
for (i = 0 ; i < 26; i++)
{
for ( j = i + 1;j < 26; j++)
{
int z = fr[i] + fr[j] ;
minimum = Math.Min(minimum, n-z) ;
}
}
return minimum ;
}
public static void Main(){
string s = "geeksforgeeks" ;
Console.Write(check(s)) ;
}
}
|
Javascript
<script>
function check(s)
{
var i, j;
var fr = Array(26).fill(0);
var n = s.length ;
for (i = 0; i < n; i++)
{
var x = s[i] ;
fr[x.charCodeAt(0) - 'a' .charCodeAt(0)] += 1 ;
}
var minimum = 10000000000;
for (i = 0 ; i < 26; i++)
{
for ( j = i + 1;j < 26; j++)
{
var z = fr[i] + fr[j] ;
minimum = Math.min(minimum, n - z) ;
}
}
return minimum ;
}
var s = "geeksforgeeks" ;
document.write( check(s));
</script>
|
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(1) because it is using constant space.
Share your thoughts in the comments
Please Login to comment...