Recursive function to match a string against a wildcard pattern

后端 未结 4 1639
甜味超标
甜味超标 2021-02-14 20:07

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:47

    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
    

提交回复
热议问题