# Recursion problem to see if string fits in given pattern

If you are given a pattern and a string, check recursively if the string fits in the pattern. The given pattern will be like "dooo?g*a". A question mark can be replaced with one character and asterisk can be replaced with multiple characters. Other characters have to match. Help me with the asterisk part.

def fit_pattern(pattern,string):
if string == pattern:
return True
if pattern == "?" or string == pattern:
return fit_pattern(pattern[1:], string[1:])
return False


Something like...

def fits(pattern, string):
if len(pattern) == 0:
return (len(string) == 0)

if pattern == "*":
if len(string) == 0:
return fits(pattern[1:], string)
return (fits(pattern, string[1:]) or fits(pattern[1:], string))

if len(string) == 0:
return False

if pattern == "?":
return fits(pattern[1:], string[1:])

return ((pattern == string) and fits(pattern[1:], string[1:]))


Explanation:

1. An empty string for the pattern matches only the empty string.
2. A pattern starting with an asterisk matches the empty string only if the rest of the pattern matches the empty string (i.e., it's empty or consists of asterisks).
3. A pattern starting with an asterisk matches an arbitrary string if either (or both) of two conditions hold: either the pattern matches the rest of the string after the asterisk consumes the first character, or the rest of the pattern matches the string (in which case the asterisk consumes nothing).
4. Any non-empty pattern starting with something other than an asterisk cannot possibly match the empty string.
5. If the pattern starts with a question mark, the pattern matches a string if the rest of the pattern matches the string after the first character.
6. In all other cases (not asterisks or question marks), the pattern matches the string if the first characters match and if the rest of the pattern matches the rest of the string.

I suspect the part you're most interested in is the following insight: *x matches ay if and only if (*x matches y OR x matches ay).

Exercise: how would you change this code so that * has to match at least one character?
{"ft_lang_label":"__label__en","ft_lang_prob":0.7254666,"math_prob":0.9326276,"size":1599,"snap":"2020-10-2020-16","text_gpt3_token_len":371,"char_repetition_ratio":0.22946708,"word_repetition_ratio":0.13076924,"special_character_ratio":0.25641027,"punctuation_ratio":0.1318328,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97062624,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-02-28T12:58:43Z\",\"WARC-Record-ID\":\"<urn:uuid:03cacd13-e5f0-4b91-b397-918c8a09eade>\",\"Content-Length\":\"140696\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:fa9cc430-548b-4e56-8ca8-6c5ec63b79e5>\",\"WARC-Concurrent-To\":\"<urn:uuid:ebd905eb-0ccf-4817-a4df-ac79a515ffcb>\",\"WARC-IP-Address\":\"151.101.193.69\",\"WARC-Target-URI\":\"https://cs.stackexchange.com/questions/31992/recursion-problem-to-see-if-string-fits-in-given-pattern\",\"WARC-Payload-Digest\":\"sha1:QWBQT3OWA5JJ7H5ZU3WPRDXPZVRBTU4Q\",\"WARC-Block-Digest\":\"sha1:N66UOSLU6LRUYVT446MXSNZLDEFUYEYB\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-10/CC-MAIN-2020-10_segments_1581875147154.70_warc_CC-MAIN-20200228104413-20200228134413-00528.warc.gz\"}"}