Https://leetcode.com/problems/wildcard-matching/ ...why this code is giving runtime error.... plz any1 help me..


  • 0
    E

    bool isMatch(char* s, char* pat) {

    int m,n,i,idx=0,j;
    m=strlen(s);
    n=strlen(pat);
    bool isfirst=true;
    char *p=(char*)malloc(n*sizeof(char));
    for(i=0;i<n;i++)
    {
        if(pat[i]=='*')
        {
            if(isfirst)
            {
                p[idx++]=pat[i];
                isfirst=false;
            }
        }
        else
        {
            p[idx++]=pat[i];
            isfirst=true;
        }
        
    }
    
    //int idx=strlen(p);
    
    bool dp[m+1][idx+1];
    
    dp[0][0]=true;
    
    if(idx>0 && p[0]=='*')
    dp[0][1]=true;
    else
    dp[0][1]=false;
    
    for(i=2;i<=idx;i++)
    dp[0][i]=false;
    
    for(i=1;i<=m;i++)
    dp[i][0]=false;
    
    for(i=1;i<=m;i++)
    {
        for(j=1;j<=idx;j++)
        {
            if(s[i-1]==p[j-1] || p[j-1]=='?')
            dp[i][j]=dp[i-1][j-1];
            
            else if(p[j-1]=='*')
            dp[i][j]=dp[i-1][j]||dp[i][j-1];
            else
            dp[i][j]=false;
            
        }
    }
    
    
    return dp[m][idx];
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.