The main idea here is to compare each character in str1 and str2, and if the char in str1 is "*", find that character in str2, which is the character next to the "*" in str1.
, - ( find(), ), ( , , , ) -
def matchPat(str1, str2):
index1 = 0
index2 = 0
while index1 < len(str1):
c = str1[index1]
if index2 >= len(str2):
if(len(str2) == 0 and str1 == "*"):
return True
return False
if c != "*":
if c != str2[index2]:
return False
index2 += 1
else:
index1 += 1
if(index1 == len(str1)):
return True
c = str1[index1]
i = str2.find(c, index2)
if(i == -1):
return False
index2 = i + 1
index1 += 1
return True
OUTPUT -
print matchPat("abcde", "abcd")
print matchPat("a", "")
print matchPat("", "a")
print matchPat("", "")
print matchPat("abc", "abc")
print matchPat("ab*cd", "abacacd")
print matchPat("ab*cd", "abaascd")
print matchPat ('a*t*r', 'anteater')
print matchPat ('a*t*r', 'albatross')
print matchPat ('a*t*r', 'artist')