Minimum Removals are Required such that a given String Consists only of a Pair of Alternating Characters


Minimum removals required such that a given string consists only of a pair of alternating characters is a common problem in computer science and is encountered frequently in applications involving string manipulation.

In this tutorial, we will solve this problem using the C++ programming language. We will begin by explaining the problem statement in detail and discussing its importance in various real-world applications.

Then, we will provide a step-by-step algorithm to solve this problem and demonstrate its implementation in C++. Finally, we will conclude with some insights into the time and space complexity of our solution and discuss potential areas of improvement. So let’s get started!

Problem Statement

The objective of this program is to find the minimum number of characters that need to be removed from a given string, 'S', in order to transform it into a new string where only two alternating characters are present.

Sample Example 1

Input

String S = "abccd"

Output

3

Explanation: After eliminating all instances of 'c' and 'd' from the original string, we are left with the resulting string "ab," which is characterized by the pattern of alternating occurrences of 'a' and 'b’.

Sample Example 2

Input

String S = "adebbeeaebd"

Output

7

Explanation: After eliminating every instance of 'b' and 'e', we acquire the string "adad," which exhibits a pattern of alternating occurrences between 'a' and 'd.'

Algorithm

STEP 1. Define a function ‘findLength’ that takes three parameters: a string ‘s’, and two characters ‘i’ and ‘j’.

STEP 2. Inside the ‘findLength’ function:

  • Initialize a variable required as i.

  • Initialize a variable length as 0 to keep track of the number of characters.

  • Iterate through each character ‘curr’ in the string ‘s’.

    • If the current character ‘curr’ is equal to the required character required:

      • Increment length by 1.

      • Update required to the other character (j), if required, is currently i, or to i if it is currently j. This ensures alternating characters.

  • Return the final length.

STEP 3. Define a function ‘minimumDeletions’ that takes a string ‘S’ as a parameter.

STEP 4. Inside the ‘minimumDeletions’ function:

  • Initialize a variable len as 0 to store the maximum length of alternating characters found.

  • Get the length of the string ‘S’ and store it in a variable ‘n’.

  • Iterate through all lowercase alphabets from 'a' to 'z' using a variable i.

    • For each i, iterate through all subsequent lowercase alphabets from i + 1 to 'z' using a variable j.

    • Call the ‘findLength’ function with parameters ‘S’, ‘i’, and ‘j’ to get the length of alternating characters when considering i and j.

    • Update len to the maximum value between len and the new length obtained.

  • Calculate the minimum number of deletions required by subtracting len from the length of the input string n.

  • Return the minimum number of deletions.

STEP 5. Inside the main function:

  • Create a string S and initialize it with the value "abccd".

  • Call the ‘minimumDeletions’ function with ‘S’ as the parameter and store the result in a variable.

  • Output the result using ‘std::cout’.

Note: The above algorithm assumes that the input string S consists only of lowercase English alphabets. If the input string contains other characters or capital letters, the program may not work as intended.

Example

Implementation of the above algorithm using C++

The below C++ program determines the minimum number of characters that need to be deleted from a string in order to obtain a string consisting of only two alternating characters.

The ‘findLength’ function counts the length of alternating substrings within the given string, using two characters as alternating markers.

The ‘minimumDeletions’ function iterates through all possible pairs of characters ('a' to 'z') and finds the maximum alternating substring length using the ‘findLength’ function.

The program's ‘main’ function initializes a string ‘S’ as "abccd" and calls ‘minimumDeletions’ to calculate the result. The result is then printed using ‘cout’.

The overall program efficiently solves the problem of finding the minimum deletions required to obtain a string with only two alternating characters.

#include <iostream>
#include <string>
#include <algorithm>

int findLength(std::string s, char i, char j) {
   char required = i;
   int length = 0;
   for (char curr : s) {
      if (curr == required) {
         length += 1;
         required = (required == i) ? j : i;
      }
   }
   return length;
}
int minimumDeletions(std::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 = std::max(len, newLen);
      }
   }
   return n - len;
}
int main() {
   std::string S = "abccd";
   std::cout << minimumDeletions(S) << std::endl;
   return 0;
}

Output

3

Conclusion

To sum up, the provided C++ program efficiently addresses the problem of determining the minimum number of character removals needed to transform a given string into a new string consisting of only a pair of alternating characters. By utilizing the findLength function to identify the length of alternating substrings and the minimumDeletions function to find the maximum alternating substring length, the program successfully computes the desired result. This solution enables the efficient handling of scenarios where minimal deletions are required to achieve a string with the desired alternating character pattern.

Updated on: 08-Sep-2023

103 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements