Recursive function to match a string against a wildcard pattern

后端 未结 4 2319
长发绾君心
长发绾君心 2021-02-14 20:10

So I\'ve been trying to solve this assignment whole day, just can\'t get it.

The following function accepts 2 strings, the 2nd (not 1st) possibly containing *

4条回答
  •  庸人自扰
    2021-02-14 20:42

    Here's some Python "psudocode" that may help

    def samePattern(s1,s2):
        if s2 == "*" or s1 == s2: return True
        if s1 == "": return False
        if s1[0] == s2[0]: return samePattern(s1[1:], s2[1:])
        if s2[0] == "*": return samePattern(s1, s2[1:]) or samePattern(s1[1:], s2)
        return False
    

    Here is a rough guide for converting the code

    s[0] = the first character
    s[1:] = the string minus the first character
    

提交回复
热议问题