


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
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
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.
li>
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; }
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) 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!

XML is used in C because it provides a convenient way to structure data, especially in configuration files, data storage and network communications. 1) Select the appropriate library, such as TinyXML, pugixml, RapidXML, and decide according to project needs. 2) Understand two ways of XML parsing and generation: DOM is suitable for frequent access and modification, and SAX is suitable for large files or streaming data. 3) When optimizing performance, TinyXML is suitable for small files, pugixml performs well in memory and speed, and RapidXML is excellent in processing large files.

The main differences between C# and C are memory management, polymorphism implementation and performance optimization. 1) C# uses a garbage collector to automatically manage memory, while C needs to be managed manually. 2) C# realizes polymorphism through interfaces and virtual methods, and C uses virtual functions and pure virtual functions. 3) The performance optimization of C# depends on structure and parallel programming, while C is implemented through inline functions and multithreading.

The DOM and SAX methods can be used to parse XML data in C. 1) DOM parsing loads XML into memory, suitable for small files, but may take up a lot of memory. 2) SAX parsing is event-driven and is suitable for large files, but cannot be accessed randomly. Choosing the right method and optimizing the code can improve efficiency.

C is widely used in the fields of game development, embedded systems, financial transactions and scientific computing, due to its high performance and flexibility. 1) In game development, C is used for efficient graphics rendering and real-time computing. 2) In embedded systems, C's memory management and hardware control capabilities make it the first choice. 3) In the field of financial transactions, C's high performance meets the needs of real-time computing. 4) In scientific computing, C's efficient algorithm implementation and data processing capabilities are fully reflected.

C is not dead, but has flourished in many key areas: 1) game development, 2) system programming, 3) high-performance computing, 4) browsers and network applications, C is still the mainstream choice, showing its strong vitality and application scenarios.

The main differences between C# and C are syntax, memory management and performance: 1) C# syntax is modern, supports lambda and LINQ, and C retains C features and supports templates. 2) C# automatically manages memory, C needs to be managed manually. 3) C performance is better than C#, but C# performance is also being optimized.

You can use the TinyXML, Pugixml, or libxml2 libraries to process XML data in C. 1) Parse XML files: Use DOM or SAX methods, DOM is suitable for small files, and SAX is suitable for large files. 2) Generate XML file: convert the data structure into XML format and write to the file. Through these steps, XML data can be effectively managed and manipulated.

Working with XML data structures in C can use the TinyXML or pugixml library. 1) Use the pugixml library to parse and generate XML files. 2) Handle complex nested XML elements, such as book information. 3) Optimize XML processing code, and it is recommended to use efficient libraries and streaming parsing. Through these steps, XML data can be processed efficiently.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
