Check if given String can be split only into subsequences ABC
Given a string S of length N where each character of the string is either ‘A‘, ‘B‘ or ‘C‘. The task is to find if is it possible to split the string into subsequences “ABC“. If it is possible to split then print “Yes“. Otherwise, print “No“.
Examples:
Input: S = “ABABCC”
Output: Yes
Explanation:
One of the possible way of splitting is to split the string in 2 subsequences of “ABC” is following:
- First form a subsequence “ABC” by taking the character at indices 0, 1, and 4.
- Again form a subsequence “ABC” by taking the character at indices 2, 3, and 5.
Therefore, the string can be split in 2 subsequences of “ABC”.
Input: S = “AABBCC”
Output: Yes
Explanation:
One of the possible way of splitting is to split the string in 2 subsequences of “ABC” is following:
- First form a subsequence “ABC” by taking the character at indices 0, 2, and 4
- Again form a subsequence “ABC” by taking the character at indices 1, 3, and 5.
Therefore, the string can be split in 2 subsequences of “ABC”.
Input: S = “BAC”
Output: No
Approach: The given problem can be solved based on the following observations:
- It can be observed that if N is not multiple of 3 or count of ‘A‘, ‘B‘, and ‘C‘ are not equal then it will be impossible to split the string satisfying the conditions.
- Also, for every ‘B‘ there must be at least one ‘A‘ to its left and one ‘C’ to its right.
Follow the steps below to solve the problem:
- Initialize 3 deques of integers say A, B, and C to store the indices of characters ‘A‘, ‘B‘ and ‘C‘ respectively.
- Iterate over characters of the string S and in each iteration, if the current character is ‘A‘ then push the index i into A. Else if the current character is ‘B‘ then push the index i into B. Otherwise, push the index i into C.
- If N is not multiple of 3 and count of characters ‘A‘, ‘B‘ and ‘C‘ are not equal then print “No”.
- Otherwise, Iterate over the deque B using the variable i, and in each iteration, if B[i] is greater than the front element of deque A then Pop the front element of deque A. Otherwise print “No” and return.
- Now again iterate over the deque B using the variable i in reverse and in each iteration if B[i] is lesser than the last element of deque C then Pop the last element of the deque C. Otherwise print “No” and return.
- Finally, if none of the above cases satisfy then print “Yes” as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string check(string S)
{
int N = S.length();
deque< int > A;
deque< int > B;
deque< int > C;
for ( int i = 0; i < N; i++) {
if (S[i] == 'A' ) {
A.push_back(i);
}
else if (S[i] == 'B' ) {
B.push_back(i);
}
else {
C.push_back(i);
}
}
if (N % 3 || A.size() != B.size()
|| A.size() != C.size()) {
return "No" ;
}
for ( int i = 0; i < B.size(); i++) {
if (!A.empty() && B[i] > A[0]) {
A.pop_front();
}
else {
return "No" ;
}
}
for ( int i = B.size() - 1; i >= 0; i--) {
if (!C.empty() && B[i] < C.back()) {
C.pop_back();
}
else {
return "No" ;
}
}
return "Yes" ;
}
int main()
{
string S = "ABABCC" ;
cout << check(S) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
public static String check(String S)
{
int N = S.length();
Deque<Integer> A = new LinkedList<Integer>();
Deque<Integer> B = new LinkedList<Integer>();
Deque<Integer> C = new LinkedList<Integer>();
for ( int i = 0 ; i < N; i++)
{
if (S.charAt(i) == 'A' )
{
A.addLast(i);
}
else if (S.charAt(i) == 'B' )
{
B.addLast(i);
}
else
{
C.addLast(i);
}
}
if (N % 3 > 0 || A.size() != B.size() ||
A.size() != C.size())
{
return "No" ;
}
for (Iterator itr = B.iterator(); itr.hasNext();)
{
Integer b = (Integer)itr.next();
if (A.size() > 0 && b > A.getFirst())
{
A.pop();
}
else
{
return "No" ;
}
}
for (Iterator itr = B.descendingIterator();
itr.hasNext();)
{
Integer b = (Integer)itr.next();
if (C.size() > 0 && b < C.getLast())
{
C.pollLast();
}
else
{
return "No" ;
}
}
return "Yes" ;
}
public static void main(String[] args)
{
String S = "ABABCC" ;
System.out.println(check(S));
}
}
|
Python3
from collections import deque
def check(S):
N = len (S)
A = deque()
B = deque()
C = deque()
for i in range (N):
if (S[i] = = 'A' ):
A.append(i)
elif (S[i] = = 'B' ):
B.append(i)
else :
C.append(i)
if (N % 3 or len (A) ! = len (B) or
len (A) ! = len (C)):
return "No"
for i in range ( len (B)):
if ( len (A) > 0 and B[i] > A[ 0 ]):
A.popleft()
else :
return "No"
for i in range ( len (B) - 1 , - 1 , - 1 ):
if ( len (C) > 0 and B[i] < C[ - 1 ]):
C.popleft()
else :
return "No"
return "Yes"
if __name__ = = '__main__' :
S = "ABABCC"
print (check(S))
|
C#
using System;
using System.Collections.Generic;
public static class GFG {
public static IEnumerable<T> Reverse<T>( this LinkedList<T> list) {
var el = list.Last;
while (el != null ) {
yield return el.Value;
el = el.Previous;
}
}
public static string check( string S)
{
int N = S.Length;
LinkedList< int > A = new LinkedList< int >();
LinkedList< int > B = new LinkedList< int >();
LinkedList< int > C = new LinkedList< int >();
for ( int i = 0; i < N; i++)
{
if (S[i] == 'A' )
{
A.AddLast(i);
}
else if (S[i] == 'B' )
{
B.AddLast(i);
}
else
{
C.AddLast(i);
}
}
if (N % 3 > 0 || A.Count != B.Count ||
A.Count != C.Count)
{
return "No" ;
}
foreach ( var itr in B)
{
int b = itr;
if (A.Count > 0 && b > A.First.Value)
{
A.RemoveFirst();
}
else
{
return "No" ;
}
}
foreach ( var itr in B.Reverse())
{
int b = itr;
if (C.Count > 0 && b < C.Last.Value)
{
C.RemoveLast();
}
else
{
return "No" ;
}
}
return "Yes" ;
}
static public void Main (){
string S = "ABABCC" ;
Console.Write(check(S));
}
}
|
Javascript
function check(S) {
const N = S.length;
const A = [];
const B = [];
const C = [];
for (let i = 0; i < N; i++) {
if (S[i] === 'A' ) {
A.push(i);
} else if (S[i] === 'B' ) {
B.push(i);
} else {
C.push(i);
}
}
if (N % 3 || A.length !== B.length || A.length !== C.length) {
return "No" ;
}
for (let i = 0; i < B.length; i++) {
if (A.length > 0 && B[i] > A[0]) {
A.shift();
} else {
return "No" ;
}
}
for (let i = B.length - 1; i >= 0; i--) {
if (C.length > 0 && B[i] < C[C.length - 1]) {
C.pop();
} else {
return "No" ;
}
}
return "Yes ";
}
// Driver Code
const S = " ABABCC";
console.log(check(S));
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
08 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...