package wildcard_matching var Cache = map[[2]string]bool{} func MatchesCached(letters, pattern string) bool { key := [2]string{letters, pattern} if matches, exists := Cache[key]; exists { return matches } result := Matches(letters, pattern) Cache[key] = result return result } func RemoveLeadingWildcards(pattern string) string { for i := 0; i < len(pattern); i++ { if pattern[i] != '*' { return pattern[i:] } } return "" } func Matches(letters string, pattern string) bool { if len(letters) == 0 && len(pattern) == 0 { return true } if len(pattern) != 0 && pattern[0] == '*' { for i := 0; i <= len(letters); i++ { if MatchesCached(letters[i:], RemoveLeadingWildcards(pattern)) { return true } } return false } if len(pattern) != 0 && len(letters) != 0 && (pattern[0] == '?' || pattern[0] == letters[0]) { return MatchesCached(letters[1:], pattern[1:]) } return false }