Maximize characters to be removed from string with given condition
Last Updated :
04 Apr, 2022
Given a string s. The task is to maximize the removal of characters from s. Any character can be removed if at least one of its adjacent characters is the previous letter in the Latin English alphabet.
Examples:
Input: s = “bacabcab”
Output: 4
Explanation: Following are the removals that maximize the answer.
During the first move, the first character can be removed as s1 = b because s2= a.
The string updates to s= acabcab.
During the second move, fifth character can be removed as s5= c because s4= b.
The string updates to s= acabab.
During the third move, sixth character can be removed s6=’b’ because s5= a.
The string updates to s= acaba.
During the fourth move, the only character that can be removed is s4= b, because s3= a (or s5= a).
The string updates to s= acaa. Now it is not possible to remove any character.
Therefore, 4 is required answer which is maximum possible.
Input: s = “bcda”
Output: 3
Approach: This problem can be solved by using the Greedy Approach. Follow the steps below to solve the given problem.
- Choose the maximum possible (alphabetically) letter that can be removed and just remove it.
- If maximum letter p is removed that can be used to remove some other letter q in string s.
- It is obvious that s[p]+1=s[q] in such a case.
- If there are no other letters between s[p] and s[q] then s[p] is not the maximum letter.
- Now suppose that if all letters between s[p] and s[q] can be removed. Then first choose s[q] and only after that s[p].
- Consider the last case there is at least one letter s[k] between s[p] and s[q]. Because we cannot remove s[k] then there are only two cases: s[k] – 1 > s[q] or s[k] < s[p] – 1. Then we cannot use s[p] to remove s[q] at all.
Below is the implementation of the above approach.
C++
#include <iostream>
using namespace std;
int removeMaxCharacters(string s, int n)
{
for ( int i = 'z' ; i > 'a' ; i--) {
for ( int k = 0; k < s.size(); k++) {
if (s[k] == i) {
if (s[k - 1] == i - 1
|| s[k + 1] == i - 1) {
s.erase(k, 1);
k = -1;
}
}
}
}
return n - s.size();
}
int main()
{
string s = "abcde" ;
int N = s.size();
cout << removeMaxCharacters(s, N);
}
|
Java
import java.util.*;
public class GFG
{
static int removeMaxCharacters(String s, int n)
{
for ( int i = 'z' ; i > 'a' ; i--) {
for ( int k = 0 ; k < s.length(); k++) {
if (s.charAt(k) == i) {
if (s.charAt(k - 1 ) == i - 1
|| s.charAt(k + 1 ) == i - 1 ) {
s = s.substring(k);
}
}
}
}
return n - s.length();
}
public static void main(String args[])
{
String s = "abcde" ;
int N = s.length();
System.out.println(removeMaxCharacters(s, N));
}
}
|
Python3
def removeMaxCharacters(s,n):
for i in range ( ord ( 'z' ), ord ( 'a' ), - 1 ):
for k in range ( len (s)):
if (s[k] = = chr (i)):
if (s[k - 1 ] = = chr (i - 1 ) or s[k + 1 ] = = chr (i - 1 )):
s = s[:k] + s[ 1 + k:]
k = - 1
return n - len (s)
s = "abcde"
N = len (s)
print (removeMaxCharacters(s, N))
|
C#
using System;
class GFG
{
static int removeMaxCharacters( string s, int n)
{
for ( int i = 'z' ; i > 'a' ; i--) {
for ( int k = 0; k < s.Length; k++) {
if (s[k] == i) {
if (s[k - 1] == i - 1
|| s[k + 1] == i - 1) {
s = s.Substring(k, 1);
}
}
}
}
return n - s.Length;
}
public static void Main()
{
string s = "abcde" ;
int N = s.Length;
Console.WriteLine(removeMaxCharacters(s, N));
}
}
|
Javascript
<script>
function removeMaxCharacters(s,n)
{
for (let i = 'z' .charCodeAt(0); i > 'a' .charCodeAt(0); i--)
{
for (let k = 0; k < s.length; k++)
{
if (s[k] == String.fromCharCode(i))
{
if (s[k - 1] == String.fromCharCode(i-1) || s[k + 1] == String.fromCharCode(i-1)) {
s = s.split( '' ).splice(k, 1).join( '' );
k = -1;
}
}
}
}
return n - s.length;
}
let s = "abcde" ;
let N = s.length;
console.log(removeMaxCharacters(s, N));
</script>
|
Time complexity: O(N2), where N is the length of the string.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...