Check if the given String can be split only into subsequences ABC


A subsequence of a string means part of a string in which characters can be taken from anywhere of the string (zero or more elements) without changing the order of the characters and forming a new string. In this problem, we have given a string of length N where every character of the string belongs to either ‘A’, ‘B’, or ‘C’ character. Our task is to find that the string can be split only into subsequences “ABC” or Not. If the string is split only into subsequences “ABC” then return “yes” otherwise return “no”.

Input 1: str = “AABCBC” 
Output 1: yes

Explanation − Ways of splitting is to split the string into 2 subsequences of “ABC” as follows −

  • One of the possible ways is, First form a subsequence “ABC” by taking the character at indexes 0, 2, 3 and second form a subsequence “ABC” by taking the character at indexes 1, 4, 5.

  • Another possible way is, Form a subsequence “ABC” by taking the character at indices 0, 4, 5, and 1, 2, 3.

Therefore, the string can be split in 2 subsequences of “ABC”.

Input 2: str = “AABBBACCC”
Output 2: no

Explanation − For ‘A’ which is present at index number 5 there is no ‘B’ present after that. Therefore, the whole string can’t be split into the only subsequence of “ABC. Therefore, the answer is “no”.

Approach1: Using Hashmap

We have two observations as follows −

  • The size of the string should be divisible by 3 as we have to split the string as “ABC” and also the count of characters ‘A’, ‘B’, and ‘C’ should be equal. Otherwise, we are not able to full fill condition.

  • When we count the frequency of characters ‘A’, ‘B’, and ‘C’ then the count of ‘A’ must be greater than equal to the count of ‘B’ and the count of ‘B’ must be greater than equal to the count of ‘C’.As A’s count >= B’s count >= C’s cout

On the basis of the above observation, we have three conditions to check.

  • Should be String size % 3 == 0.

  • Should be A’s count >= B’s count >= C’s cout.

  • Last condition should be freq[ ‘A’ ] == freq[ ‘B’ ] == freq[ ‘C’ ] .

We can solve this problem using a hashmap as we need to store the frequency of each character of a given string “str”.

Let’s discuss this approach step by step below-

  • First, we will create a function ‘checkSubsequences’ that will take the given string ‘str’ as the parameter and will return the required string if possible “yes” else “no” as the return value.

  • In the function, below given all the step-

  • Create variable ‘len’ to store the length of the string.

  • Check first condition, if len is not divisible by 3 then return ‘no’.

  • Create a hashmap to store the frequency of only characters ‘A’, ‘B’, and ‘C’. Therefore, the space complexity is constant.

  • Traverse string using for loop from 0 to less than len.

    • Increase the count of current character of string

    • Check the second condition, if the count of ‘A’ less than count of ‘B’ or the count of ‘B’ less than count of ‘C’ return “no”.

  • After the for loop, we have to check final third condition, If count of A’s not equal to count of B’s or count B’s not equal to count of C’s return “no”.

  • In the end as all conditions are satisfied return “yes”.

Example

#include <bits/stdc++.h>
using namespace std;
// function to check subsequences of "ABC"
string checkSubsequences( string str ){
   int len = str.size(); //getting length of the string str
   // check first condition 
   if( len%3 != 0 ) {
      return "no";
   }
   map< char, int >freq; //store the count of character 'A', 'B' and 'C'
   for( int i=0; i<len; i++){
      freq[ str[i] ]++; // increase the count of the character
      //chech second condition 
      if(freq[ 'A' ] < freq[ 'B' ] || freq[ 'B' ] < freq[ 'C' ]){
         return "no";
      }
   }
   //check third condition 
   if(freq[ 'A' ] != freq[ 'B' ] || freq[ 'B' ] != freq[ 'C' ]){
      return "no";
   }
   // it is possible to split string only into subsequences of "ABC"
   return "yes";
}
// main function 
int main(){
   string str = "ABAAABCBC";// given string 
   // calling the function 'checkSubsequences' to check is it possible to split
   // string into subsequences of "ABC"
   string result = checkSubsequences( str );
   if( result == "yes" ){
      cout<< result << ", the string is splited only into the subsequences of ABC";
   }
   else {
      cout<< result << ", the string is not splited only into the subsequences of ABC.";
   }
   return 0;
}

Output

no, the string is not splited only into the subsequences of ABC.

Time and Space Complexity

The time complexity of the above code is O(N), as we traverse the spring. Where N is the size of the string.

The space complexity of the above code is O(1), as we are storing the frequency of the digit which is a constant size of three.

Conclusion

In this tutorial, we have implemented a program to Check if the given String can be split only into subsequences ABC or not. We have implemented an approach of hashing as we have to store the frequency. In this approach, we have to check mainly three conditions if all the co dition satisfied means we are able to split string only into the subsequences of “ABC”.

Updated on: 16-May-2023

112 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements