Divide given numeric string into at most two increasing subsequences which form an increasing string upon concatenation | Function to check for valid subsequences ; Stores which element belongs to which subsequence ; Check for each pos if a possible subsequence exist or not ; Last member of 1 subsequence ; Last Member of 2 nd subsequence ; Check if current element can go to 2 nd subsequence ; Check if the current elements belongs to first subsequence ; If the current element does not belong to any subsequence ; Check if last digit of first subsequence is greater than pos ; If a subsequence is found , find the subsequences ; Stores the resulting subsequences ; Print the subsequence ; If no subsequence found , print - 1 ; Driver Code
function findSubsequence ( str ) { let n = str . length ; let res = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) res [ i ] = 0 ; for ( let pos = 0 ; pos <= 9 ; pos ++ ) { let lst1 = ' ' ; let flag = 1 ; let lst2 = String . fromCharCode ( pos + ' ' . charCodeAt ( 0 ) ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( lst2 . charCodeAt ( 0 ) <= str [ i ] . charCodeAt ( 0 ) ) { res [ i ] = ' ' ; lst2 = str [ i ] ; } else if ( lst1 . charCodeAt ( 0 ) <= str [ i ] . charCodeAt ( 0 ) ) { res [ i ] = ' ' ; lst1 = str [ i ] ; } else flag = 0 ; } if ( lst1 . charCodeAt ( 0 ) > pos + ' ' . charCodeAt ( 0 ) ) { flag = 0 ; } if ( flag ) { let S1 = " " ; let S2 = " " ; for ( let i = 0 ; i < n ; i ++ ) { if ( res [ i ] == ' ' ) { S1 += str [ i ] ; } else { S2 += str [ i ] ; } } document . write ( S1 + ' ' + S2 + ' ' ) ; return ; } } document . write ( " " ) ; } let S = " " ; findSubsequence ( S ) ; S = " " ; findSubsequence ( S ) ;