Check if a string is a subsequence of another string ( using Stacks ) | 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
function checkforSubsequence ( S , target ) { var s = [ ] ; for ( var i = 0 ; i < target . length ; i ++ ) { s . push ( target [ i ] ) ; } for ( var i = S . length - 1 ; i >= 0 ; i -- ) { if ( s . length == 0 ) { document . write ( " " ) ; return ; } if ( S [ i ] == s [ s . length - 1 ] ) { s . pop ( ) ; } } if ( s . length == 0 ) document . write ( " " ) ; else document . write ( " " ) ; } var S = " " ; var target = " " ; checkforSubsequence ( S , target ) ;