Dynamic Programming | Wildcard Pattern Matching | Linear Time and Constant Space | Function that matches input txt with given wildcard pattern ; empty pattern can only match with empty sting Base case ; step 1 initialize markers : ; For step - ( 2 , 5 ) ; For step - ( 3 ) ; For step - ( 4 ) ; For step - ( 5 ) ; For step - ( 6 ) ; For step - ( 7 ) ; Final Check ; Driver code
def stringmatch ( txt , pat , n , m ) : NEW_LINE INDENT if ( m == 0 ) : NEW_LINE INDENT return ( n == 0 ) NEW_LINE DEDENT i = 0 NEW_LINE j = 0 NEW_LINE index_txt = - 1 NEW_LINE index_pat = - 1 NEW_LINE while ( i < n - 2 ) : NEW_LINE INDENT if ( j < m and txt [ i ] == pat [ j ] ) : NEW_LINE INDENT i += 1 NEW_LINE j += 1 NEW_LINE DEDENT elif ( j < m and pat [ j ] == ' ? ' ) : NEW_LINE INDENT i += 1 NEW_LINE j += 1 NEW_LINE DEDENT elif ( j < m and pat [ j ] == ' * ' ) : NEW_LINE INDENT index_txt = i NEW_LINE index_pat = j NEW_LINE j += 1 NEW_LINE DEDENT elif ( index_pat != - 1 ) : NEW_LINE INDENT j = index_pat + 1 NEW_LINE i = index_txt + 1 NEW_LINE index_txt += 1 NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT while ( j < m and pat [ j ] == ' * ' ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT if ( j == m ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT strr = " baaabab " NEW_LINE pattern = " * * * * * ba * * * * * ab " NEW_LINE char pattern [ ] = " ba * * * * * ab " NEW_LINE char pattern [ ] = " ba ▁ * ▁ ab " NEW_LINE char pattern [ ] =   " a * ab NEW_LINE if ( stringmatch ( strr , pattern , len ( strr ) , len ( pattern ) ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT pattern2 = " a * * * * * ab " ; NEW_LINE if ( stringmatch ( strr , pattern2 , len ( strr ) , len ( pattern2 ) ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT