Table of Contents
Method 1: Use Hashmap
Example
Output
Time and space complexity
in conclusion
Home Backend Development C++ Checks whether the given string can only be split into subsequences of ABC

Checks whether the given string can only be split into subsequences of ABC

Sep 06, 2023 pm 05:01 PM
examine String splitting child order

Checks whether the given string can only be split into subsequences of ABC

A subsequence of a string is a portion of a string where characters can be taken from any position (zero or more elements) of the string without changing the order of the characters and Form a new string. In this problem, we are given a string of length N where every character of the string is either an 'A', 'B' or 'C' character. Our task is to find that the string can only be split into subsequences "ABC" or "Not". Returns "yes" if the string is only split into the subsequence "ABC", otherwise returns "no".

Input 1: str = “AABCBC” 
Output 1: yes
Copy after login

Instructions - The method of splitting is to split the string into 2 subsequences of "ABC", as follows -

  • One possible method is to form the subsequence "ABC" by taking the characters with indexes 0, 2, and 3, and then form the subsequence "ABC" by taking the characters with indexes 1, 4, and 5 .

  • Another possible way is to form the subsequence "ABC" by getting the characters at indexes 0, 4, 5 and 1, 2, 3.

Therefore, the string can be split into 2 subsequences of "ABC".

Input 2: str = “AABBBACCC”
Output 2: no
Copy after login

Explanation - For 'A' occurring at index number 5, there is no 'B' after it. Therefore, the entire string cannot be split into unique subsequences "ABC". Therefore, the answer is "no".

Method 1: Use Hashmap

We have two observations as follows -

  • The size of the string should be divisible by 3 because we need to split the string into "ABC" and the number of characters 'A', 'B' and 'C' should be equal. Otherwise, we cannot meet the conditions.

  • When we count the frequency of characters "A", "B" and "C", the count of "A" must be greater than or equal to the count of "B" and the count of "B" must be greater than or equal to 'C ' count. Because A's count >= B's count >= C's cout

Based on the above observations, we have three conditions to check.

  • Expected string size % 3 == 0.

  • should be the count of A >= the count of B >= the count of C.

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

We can use a hash map to solve this problem since we need to store the frequency of each character in the given string "str".

Let us discuss the following method step by step -

  • First we will create a function called "checkSubsequences" which will take the given string "str" ​​as parameter and return the desired string "yes" if possible , otherwise "no" is returned as the return value.

  • In the function, all the steps are given below-

  • Create variable "len" to store the length of the string.

  • Check the first condition and return 'no' if the length is not divisible by 3.

  • Create a hash map to store the frequencies of characters 'A', 'B' and 'C'. Therefore, the space complexity is constant.

  • Use a for loop to traverse the string from 0 to less than len.

    • Increase the current character count of the string

    • Check the second condition and return "No" if "A"'s count is less than "B"'s count or "B"'s count is less than "C"'s count.

  • After the for loop, we have to check the last third condition and return "No" if A's count is not equal to B's count or B's count is not equal to C's count.

  • Finally, when all conditions are met, 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;
}
Copy after login

Output

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

Time and space complexity

The time complexity of the above code is O(N) because we traverse the string. where N is the size of the string.

The space complexity of the above code is O(1) because we are storing the frequency of numbers, whose size is constant 3.

in conclusion

In this tutorial, we implemented a program to check if a given string can only be split into subsequences ABC. We implemented a hashing method because we had to store the frequencies. In this method, we mainly check three conditions, if all conditions are met, it means that we can only split the string into subsequences of "ABC".

The above is the detailed content of Checks whether the given string can only be split into subsequences of ABC. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Spellcheck not working in Teams [Fixed] Spellcheck not working in Teams [Fixed] Mar 06, 2024 am 09:10 AM

We've started noticing that sometimes spellcheck stops working for Teams. Spell check is an essential tool for effective communication, and any attack on it can cause considerable disruption to workflow. In this article, we'll explore common reasons why spell check might not be working as expected, and how to restore it to its previous state. So, if spell check is not working in Teams, follow the solutions mentioned in this article. Why doesn't Microsoft spell check work? There may be several reasons why Microsoft spell check is not working properly. These reasons include incompatible language settings, disabled spell check function, damaged MSTeam or MSOffice installation, etc. Also, outdated MSTeams and MSOf

How to check if application is open in Python? How to check if application is open in Python? Aug 26, 2023 pm 06:49 PM

The program being executed is called a process. A process can be an application running on the current operating system or an application related to the operating system. If an application is tied to the operating system, it first creates a process to execute itself. Other applications rely on operating system services for execution. Most applications are operating system services and background applications that maintain the operating system, software, and hardware. In python we have different methods to check if application is open or not. Let’s learn about them in detail one by one. Using the psutil.process_iter() function psutil is a module in Python that provides users with an interface to retrieve information about running processes and system utilization.

How to check if an object is iterable in Python? How to check if an object is iterable in Python? Aug 25, 2023 pm 10:05 PM

An iterable object is an object whose all elements can be iterated over using a loop or iterable function. Lists, strings, dictionaries, tuples, etc. are all called iterable objects. In Python language, there are various ways to check whether an object is iterable. Let’s take a look one by one. Using Loops In Python, we have two looping techniques, one is using "for" loop and the other is using "while" loop. Using either of these two loops, we can check if a given object is iterable. Example In this example, we will try to iterate an object using "for" loop and check if it is iterated or not. Below is the code. l=["apple",22,"orang

How to check SSD health status in Windows 11? How to check SSD health status on Win11 How to check SSD health status in Windows 11? How to check SSD health status on Win11 Feb 14, 2024 pm 08:21 PM

How to check SSD health status in Windows 11? For their fast read, write, and access speeds, SSDs are quickly replacing HDDs, but even though they are more reliable, you still need to check the health of your SSDs in Windows 11. How to operate it? In this tutorial, the editor will share with you the method. Method 1: Use WMIC1, use the key combination Win+R, type wmic, and then press or click OK. Enter2. Now, type or paste the following command to check the SSD health status: diskdrivegetstatus If you receive the "Status: OK" message, your SSD drive is operating normally.

Java program used to check if TPP students are eligible for interviews Java program used to check if TPP students are eligible for interviews Sep 06, 2023 pm 10:33 PM

Please consider the table below to know the eligibility criteria of different companies - The Chinese translation of CGPA is: GPA greater than or equal to 8 Eligible companies Google, Microsoft, Amazon, Dell, Intel, Wipro greater than or equal to 7 Tutorial points, accenture, Infosys , Emicon, Rellins greater than or equal to 6rtCamp, Cybertech, Skybags, Killer, Raymond greater than or equal to 5Patronics, Shoes, NoBrokers Let us enter the java program to check the eligibility of tpp students for interview. Method 1: Using ifelseif condition Normally when we have to check multiple conditions we use

How to check if a string starts with a specific character in Golang? How to check if a string starts with a specific character in Golang? Mar 12, 2024 pm 09:42 PM

How to check if a string starts with a specific character in Golang? When programming in Golang, you often encounter situations where you need to check whether a string begins with a specific character. To meet this requirement, we can use the functions provided by the strings package in Golang to achieve this. Next, we will introduce in detail how to use Golang to check whether a string starts with a specific character, with specific code examples. In Golang, we can use HasPrefix from the strings package

How to check if ArrayList contains a certain element in Java? How to check if ArrayList contains a certain element in Java? Sep 03, 2023 pm 04:09 PM

You can use the contains() method of the List interface to check whether an object exists in the list. contains() method booleancontains(Objecto) Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null?e==null:o.equals(e)). Parameter c - the element whose presence in this list is to be tested. Return Value Returns true if this list contains the specified element. Throws ClassCastException - if the specified element's type is incompatible with this list (optional). NullP

Use the strings.Split function to split a string into multiple substrings according to the specified delimiter. Use the strings.Split function to split a string into multiple substrings according to the specified delimiter. Jul 24, 2023 pm 03:21 PM

Use the strings.Split function to split a string into multiple substrings according to the specified delimiter. In the Go language, we can use the Split function in the strings package to split a string into multiple substrings according to the specified delimiter. This is very useful when working with strings, especially when we need to split, parse, or extract specific content from the string. The prototype of the Split function is as follows: funcSplit(s,sepstring)[]string where, s

See all articles