MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / IsMatch

Function IsMatch

dynamic/wildcardmatching.go:10–33  ·  view source on GitHub ↗

IsMatch checks if the string `s` matches the wildcard pattern `p`

(s, p string)

Source from the content-addressed store, hash-verified

8
9// IsMatch checks if the string `s` matches the wildcard pattern `p`
10func IsMatch(s, p string) bool {
11 dp := make([][]bool, len(s)+1)
12 for i := range dp {
13 dp[i] = make([]bool, len(p)+1)
14 }
15
16 dp[0][0] = true
17 for j := 1; j <= len(p); j++ {
18 if p[j-1] == '*' {
19 dp[0][j] = dp[0][j-1]
20 }
21 }
22
23 for i := 1; i <= len(s); i++ {
24 for j := 1; j <= len(p); j++ {
25 if p[j-1] == s[i-1] || p[j-1] == '?' {
26 dp[i][j] = dp[i-1][j-1]
27 } else if p[j-1] == '*' {
28 dp[i][j] = dp[i-1][j] || dp[i][j-1]
29 }
30 }
31 }
32 return dp[len(s)][len(p)]
33}

Callers 1

TestIsMatchFunction · 0.92

Calls

no outgoing calls

Tested by 1

TestIsMatchFunction · 0.74