Check if a given pattern exists in a given string or not | Function to check if the pattern consisting of ' * ' , ' . ' and lowercase characters matches the text or not ; Base Case ; Stores length of text ; Stores length of pattern ; dp [ i ] [ j ] : Check if { text [ 0 ] , . . text [ i ] } matches { pattern [ 0 ] , ... pattern [ j ] } or not ; Base Case ; Iterate over the characters of the string pattern ; Update dp [ 0 ] [ i + 1 ] ; Iterate over the characters of both the strings ; If current character in the pattern is ' . ' ; Update dp [ i + 1 ] [ j + 1 ] ; If current character in both the strings are equal ; Update dp [ i + 1 ] [ j + 1 ] ; If current character in the pattern is ' * ' ; Update dp [ i + 1 ] [ j + 1 ] ; Update dp [ i + 1 ] [ j + 1 ] ; Return dp [ M ] [ N ] ; Driver Code
function isMatch ( text , pattern ) { if ( text == null pattern == null ) { return false ; } let N = text . length ; let M = pattern . 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 < M ; i ++ ) { if ( pattern [ i ] == ' ' && dp [ 0 ] [ i - 1 ] ) { dp [ 0 ] [ i + 1 ] = true ; } } for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { if ( pattern [ j ] == ' ' ) { dp [ i + 1 ] [ j + 1 ] = dp [ i ] [ j ] ; } if ( pattern [ j ] == text [ i ] ) { dp [ i + 1 ] [ j + 1 ] = dp [ i ] [ j ] ; } if ( pattern [ j ] == ' ' ) { if ( pattern [ j - 1 ] != text [ i ] && pattern [ j - 1 ] != ' ' ) { dp [ i + 1 ] [ j + 1 ] = dp [ i + 1 ] [ j - 1 ] ; } else { dp [ i + 1 ] [ j + 1 ] = ( dp [ i + 1 ] [ j ] dp [ i ] [ j + 1 ] dp [ i + 1 ] [ j - 1 ] ) ; } } } } return dp [ N ] [ M ] ; } let text = " " ; let pattern = " " ; if ( isMatch ( text , pattern ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }