Segmentation fault in regular expression 2


#1

Hi,
This code which I have written for the question is giving segmentation fault:
int Solution::isMatch(const string A, const string B) {

long long int len1=A.size();
long long int len2=B.size();
int dp[len1+1][len2+1];
for(long long int i=0;i<=len1;i++)
{
for(long long int j=0;j<=len2;j++)
dp[i][j]=0;
}
dp[0][0]=1;
for(long long int j=0;j<=len2;j++)
{
if(B[j-1]==’*’)

dp[0][j]=dp[0][j-2];

}
for(long long int i=1;i<=len1;i++)
{
for(long long int j=1;j<=len2;j++)
{
if(A[i-1]==B[j-1]||B[j-1]==’.’)
dp[i][j]=dp[i-1][j-1];
else if(B[j-1]==’*’)
{
dp[i][j]=dp[i][j-2];
if(A[i-1]==B[j-2]||B[j-2]==’.’)
dp[i][j]=dp[i][j] || dp[i-1][j];
}
}
}
return dp[len1][len2];
}

Can anyone please help me?


#2

for(long long int j=0;j<=len2;j++)
{
if(B[j-1]==’*’)

dp[0][j]=dp[0][j-2];

}

because of j-2 access -> loop should start with j = 2