Check if it is possible to transform one string to another | function to check if a string can be converted to another string by performing following operations ; calculates length ; mark 1 st position as true ; traverse for all DPi , j ; if possible for to convert i characters of s1 to j characters of s2 ; if upper_case ( s1 [ i ] ) == s2 [ j ] is same ; if not upper then deletion is possible ; driver code
function check ( s1 , s2 ) { let n = s1 . length ; let m = s2 . length ; let dp = new Array ( n + 1 ) ; for ( let i = 0 ; i <= n ; i ++ ) { dp [ i ] = new Array ( m + 1 ) ; for ( let j = 0 ; j <= m ; j ++ ) { dp [ i ] [ j ] = false ; } } dp [ 0 ] [ 0 ] = true ; for ( let i = 0 ; i < s1 . length ; i ++ ) { for ( let j = 0 ; j <= s2 . length ; j ++ ) { if ( dp [ i ] [ j ] ) { if ( j < s2 . length && ( s1 [ i ] . toUpperCase ( ) == s2 [ j ] ) ) dp [ i + 1 ] [ j + 1 ] = true ; if ( ! ( s1 [ i ] == s1 [ i ] . toUpperCase ( ) ) ) dp [ i + 1 ] [ j ] = true ; } } } return ( dp [ n ] [ m ] ) ; } let s1 = " " ; let s2 = " " ; if ( check ( s1 , s2 ) ) document . write ( " " ) ; else document . write ( " " ) ;