Answer to Question #328340 in Python for Sunny

Question #328340

Compare the two strings is equal or not....



The "*" and "?" Can treated as a replace for characters



?=is used as replace for 1 character



* = It can be as replace for 1 to length of the character and can also used as a empty string



1st question:



3



Maggi Ma*i



Maggi Ma?gi



Maggi M*?



Answer:



True



True



True



2nd question:



Tree tr*



Plant Pl**



Dance D*ance



Answer:



False



True



True





1
Expert's answer
2022-04-14T06:16:03-0400
def isMatching(string1, string2):
  i = 0
  j = 0
  while j < len(string2) and i < len(string1):
    if string2[j] == "?" or string2[j] == string1[i]:
      if j == len(string2) - 1 and i == len(string1) - 1:
        return True
    elif string2[j] == "*":
      count = 0
      while string2[j] == "*" or string2[j] == "?":
        if j == len(string2) - 1 and i + count < len(string1):
          return True
        if string2[j] == "?":
          count += 1
        j += 1
      if i + count >= len(string1):
        return False
      i += count
      if string2[j - 1] == "*" and string2[j] == string1[i]:
        continue
      while string1[i] != string2[j]:
        if i == len(string1) - 1:
          return False
        i += 1
      continue
    else:
      return False
    j += 1
    i += 1
  return False


strings = input("Enter two strings separated with space:\n")
strings = strings.split(" ")
print(isMatching(strings[0], strings[1])

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS