Skip to content

Instantly share code, notes, and snippets.

@guolinaileen
Last active December 11, 2015 18:19
Show Gist options
  • Save guolinaileen/4640399 to your computer and use it in GitHub Desktop.
Save guolinaileen/4640399 to your computer and use it in GitHub Desktop.
bugs still exist. need to figure out a better method.
public class Solution {
public boolean isMatch(String s, String p) {
if(s!="" && p=="") return false;
char [] sArray=s.toCharArray();
int sLength=sArray.length;
char [] pArray=p.toCharArray();
int pLength=pArray.length;
int i=0, j=0;
while(i<sLength && j<pLength){
if(sArray[i]==pArray[j]||pArray[j]=='.')
{
i++; j++;
continue;
}else if(pArray[j]=='*')
{
if((j-1>=0) && (pArray[j-1]==sArray[i] ))
{
i++; j++;
continue;
}else if ((j-1>=0) && (pArray[j-1]=='.' ))
{
i++;
}else
{
return false;
}
}else
{
if((j-2>=0) && pArray[j-1]=='*' && pArray[j-2]==sArray[i])
{
i++;
continue;
}else if((j+2<pLength)&& pArray[j+1]=='*' && pArray[j+2]==sArray[i])
{
j+=3;
if((j+3<pLength)&&pArray[j+3]=='*') {j++;}
i++;
continue;
}else
{
return false;
}
}
}
if((i!=sLength)) return false;
if((j!=pLength)&&(pArray[pLength-1]!='*')) return false;
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment