Wildcard Pattern Matching | C # program to implement wildcard pattern matching algorithm ; Function that matches input str with given wildcard pattern ; empty pattern can only match with empty string ; lookup table for storing results of subproblems ; initialize lookup table to false ; empty pattern can match with empty string ; Only ' * ' can match with empty string ; fill the table in bottom - up fashion ; Two cases if we see a ' * ' a ) We ignore ' * ' ' ▁ character ▁ and ▁ move ▁ to ▁ next ▁ character ▁ in ▁ the ▁ pattern , ▁ i . e . , ▁ ' * ' ▁ indicates ▁ an ▁ empty ▁ sequence . ▁ b ) ▁ ' * ' character matches with ith  character in input ; Current characters are considered as matching in two cases ( a ) current character of pattern is ' ? ' ( b ) characters actually match ; If characters don 't match ; Driver Code ; String pattern = " ba * * * * * ab " ; String pattern = " ba * ab " ; String pattern = " a * ab " ; String pattern = " a * * * * * ab " ; String pattern = " * a * * * * * ab " ; String pattern = " ba * ab * * * * " ; String pattern = " * * * * " ; String pattern = " * " ; String pattern = " aa ? ab " ; String pattern = " b * b " ; String pattern = " a * a " ; String pattern = " baaabab " ; String pattern = " ? baaabab " ; String pattern = " * baaaba * " ;
using System ; class GFG { static Boolean strmatch ( String str , String pattern , int n , int m ) { if ( m == 0 ) return ( n == 0 ) ; Boolean [ , ] lookup = new Boolean [ n + 1 , m + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) for ( int j = 0 ; j < m + 1 ; j ++ ) lookup [ i , j ] = false ; lookup [ 0 , 0 ] = true ; for ( int j = 1 ; j <= m ; j ++ ) if ( pattern [ j - 1 ] == ' * ' ) lookup [ 0 , j ] = lookup [ 0 , j - 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { if ( pattern [ j - 1 ] == ' * ' ) lookup [ i , j ] = lookup [ i , j - 1 ] || lookup [ i - 1 , j ] ; else if ( pattern [ j - 1 ] == ' ? ' str [ i - 1 ] == pattern [ j - 1 ] ) lookup [ i , j ] = lookup [ i - 1 , j - 1 ] ; else lookup [ i , j ] = false ; } } return lookup [ n , m ] ; } public static void Main ( String [ ] args ) { String str = " baaabab " ; String pattern = " * * * * * ba * * * * * ab " ; if ( strmatch ( str , pattern , str . Length , pattern . Length ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }