Check if given String can be split only into subsequences ABC | C # program for the above approach ; Function to check if the given string can be splitted into subsequences " ABC " ; Stores the length of the string ; Stores the indices of ' A ' ; Stores the indices of ' B ' ; Stores the indices of ' C ' ; Traverse the string S ; If S [ i ] is equal to ' A ' ; Push the index i in A ; Else if S [ i ] is equal to ' B ' ; Push the index i in B ; Else if S [ i ] is equal to ' C ' ; Push the index i in C ; If N is not multiple of 3 or count of characters ' A ' , ' B ' and ' C ' are not equal ; Return " No " ; Iterate over the deque B ; If A is not empty and curr B is greater than A [ 0 ] ; Removes the front element of A ; Else return " No " ; Iterate over the deque B in reverse ; If C is not empty and last element of C is greater thab B [ i ] ; Removes the last element of C ; Else return " No " ; If none of the above cases satisfy return "Yes' ; Driver code ; Input ; Function call
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 ) ) ; } }