Given a string S, the task is to find the minimum removal of characters required such that the string S consists only of two alternating characters.
Examples:
Input: S = “adebbeeaebd”
Output: 7
Explanation: Removing all occurrences of ‘b’ and ‘e’ modifies the string to “adad”, which consist of alternating occurrences of ‘a’ and ‘d’.
Input: S = “abccd”
Output: 3
Explanation: Removing all occurrences of ‘c’ and ‘d’ modifies the string to “ab”, which consist of alternating ‘a’ and ‘b’.
Approach: The problem can be solved by generating all the possible 262 pairs of English letters and find the pair with a maximum length of alternating occurrences, say len, in the string S. Then, print the number of characters required to be removed to achieve that by subtracting len from N.
Follow the steps below to solve the given problem:
- Initialize a variable, say len, to store the maximum length of alternating occurrences of a pair of characters.
- Iterate over every possible pair of English alphabets and for each pair, perform the following operations:
- Iterate over the characters of the string S and find the length, say newlen, of alternating occurrences of two characters from the string S.
- Check if len is smaller than newlen or not. If found to be true, then update len = newlen.
- Finally, print N – len as the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findLength(string s, char i, char j)
{
char required = i;
int length = 0;
for ( char curr : s) {
if (curr == required) {
length += 1;
if (required == i)
required = j;
else
required = i;
}
}
return length;
}
int minimumDeletions(string S)
{
int len = 0;
int n = S.length();
for ( char i = 'a' ; i <= 'z' ; i++) {
for ( char j = i + 1; j <= 'z' ; j++) {
int newLen = findLength(S, i, j);
len = max(len, newLen);
}
}
return n - len;
}
int main()
{
string S = "adebbeeaebd" ;
cout << minimumDeletions(S);
return 0;
}
|
Java
class GFG{
static int findLength(String s, char i, char j)
{
char required = i;
int length = 0 ;
for ( int k = 0 ; k < s.length(); k++)
{
char curr = s.charAt(k);
if (curr == required)
{
length += 1 ;
if (required == i)
required = j;
else
required = i;
}
}
return length;
}
static int minimumDeletions(String S)
{
int len = 0 ;
int n = S.length();
for ( int i = 0 ; i < 26 ; i++)
{
for ( int j = i + 1 ; j < 26 ; j++)
{
int newLen = findLength(S, ( char )(i + 97 ),
( char )(j + 97 ));
len = Math.max(len, newLen);
}
}
return n - len;
}
public static void main (String[] args)
{
String S = "adebbeeaebd" ;
System.out.print(minimumDeletions(S));
}
}
|
Python3
def findLength(s, i, j):
required = i
length = 0
for curr in s:
if (curr = = required):
length + = 1
if (required = = i):
required = j
else :
required = i
return length
def minimumDeletions(S):
len1 = 0
n = len (S)
for i in range ( 0 , 26 , 1 ):
for j in range (i + 1 , 26 , 1 ):
newLen = findLength(S, chr (i + 97 ),
chr (j + 97 ))
len1 = max (len1, newLen)
return n - len1
if __name__ = = '__main__' :
S = "adebbeeaebd"
print (minimumDeletions(S))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int findLength( string s, char i, char j)
{
char required = i;
int length = 0;
for ( int k = 0; k < s.Length; k++)
{
char curr = s[k];
if (curr == required)
{
length += 1;
if (required == i)
required = j;
else
required = i;
}
}
return length;
}
static int minimumDeletions( string S)
{
int len = 0;
int n = S.Length;
for ( int i = 0; i < 26; i++)
{
for ( int j = i + 1; j < 26; j++)
{
int newLen = findLength(S, ( char )(i + 97),
( char )(j + 97));
len = Math.Max(len, newLen);
}
}
return n - len;
}
public static void Main( string [] args)
{
string S = "adebbeeaebd" ;
Console.WriteLine(minimumDeletions(S));
}
}
|
Javascript
<script>
function findLength( s, i, j)
{
var required = i;
let length = 0;
for (let k = 0; k < s.length; k++)
{
var curr = s.charAt(k);
if (curr == required)
{
length += 1;
if (required == i)
required = j;
else
required = i;
}
}
return length;
}
function minimumDeletions( S)
{
let len = 0;
let n = S.length;
for (let i = 0; i < 26; i++)
{
for (let j = i + 1; j < 26; j++)
{
let newLen =
findLength(S, String.fromCharCode(i + 97),
String.fromCharCode(j + 97));
len = Math.max(len, newLen);
}
}
return n - len;
}
let S = "adebbeeaebd" ;
document.write(minimumDeletions(S));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)