Check if a string is a subsequence of another string ( using Stacks ) | C # approach for the above approach ; Function to check if target is a subsequence of string S ; Declare a stack ; Push the characters of target into the stack ; Traverse the string S in reverse ; If the stack is empty ; If S [ i ] is same as the top of the stack ; Pop the top of stack ; Stack s is empty ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void checkforSubsequence ( String S , String target ) { Stack < char > s = new Stack < char > ( ) ; for ( int i = 0 ; i < target . Length ; i ++ ) { s . Push ( target [ i ] ) ; } for ( int i = ( int ) S . Length - 1 ; i >= 0 ; i -- ) { if ( s . Count == 0 ) { Console . WriteLine ( " Yes " ) ; return ; } if ( S [ i ] == s . Peek ( ) ) { s . Pop ( ) ; } } if ( s . Count == 0 ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } public static void Main ( String [ ] args ) { String S = " KOTTAYAM " ; String target = " KOTA " ; checkforSubsequence ( S , target ) ; } }