Maximize characters to be removed from string with given condition


In this problem, we need to remove the maximum characters from the string if the adjacent character is the previous character.

We can find the occurrence of each character and check whether any of its adjacent characters is a previous character, we can remove the particular character.

Problem statement - We have given a string containing the N alphabetic characters. The given task is we need to remove the maximum number of characters, and we can remove any character of the string if any adjacent character is the previous character in the alphabet. At last, print the count of removed characters.

Sample examples

Input

str = 'acd'

Output

1

Explanation - We can remove 'd' as an adjacent character is 'c'.

Input

str = "efghijk"

Output

6

Explanation - We can remove the 'k' as the adjacent character is 'j'.

We can remove 'j' as its adjacent character is 'i'.

In this way, we can remove all characters except 'e'. So, we can remove 6 characters.

Input

str = "agnbpc";

Output

0

Explanation - Any character of the string doesn't have an adjacent character which is a previous character to it in the alphabet.

Approach 1

This approach will start from 'z' to 'a'. We will find all occurrences of 'z', 'y', 'x', and so on. We can remove the current character if we find a previous adjacent character for any alphabet.

Algorithm

Step 1 - Use the loop to traverse alphabetical characters in reverse order.

Step 2 - Use another nested loop to traverse the string. If a character in the string is equal to character p, follow the below steps.

Step 3 - Check whether any adjacent character is equal to the previous character; if yes, erase the character from the string, and decrease the value of q by 1.

Step 4 - Return the result after subtracting the updated string's length from the current string's length.

Example

#include <iostream>
using namespace std;

int numOfMaxCharacters(string alpha, int str_len) {
   // Traverse alphabets from 'z' to 'a'
   for (int p = 'z'; p > 'a'; p--) {
      // Traverse string
      for (int q = 0; q < alpha.size(); q++) {
         // If the character matches
         if (alpha[q] == p) {
            // Check if adjacent characters are less than the current character
            if (alpha[q - 1] == p - 1 || alpha[q + 1] == p - 1) {
               alpha.erase(q, 1);
               q = -1;
            }
         }
      }
   }
   // Return answer
   return str_len - alpha.size();
}
int main() {
   string str = "efghijk";
   // String size
   int str_len = str.size();
   cout << "The number of maximum characters that we can remove is " << numOfMaxCharacters(str, str_len);
}

Output

The number of maximum characters that we can remove is 6

Time complexity - O(N), as we traverse the string.

Space complexity - O(1)

Here, we remove each character in the reverse order to maximize the removal. If we remove a character from the string in random order, we might not be able to remove the maximum characters.

Updated on: 24-Aug-2023

38 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements