MCPcopy Index your code
hub / github.com/doocs/leetcode / dfs

Method dfs

solution/0200-0299/0291.Word Pattern II/Solution.py:3–20  ·  view source on GitHub ↗
(i, j)

Source from the content-addressed store, hash-verified

1class Solution:
2 def wordPatternMatch(self, pattern: str, s: str) -> bool:
3 def dfs(i, j):
4 if i == m and j == n:
5 return True
6 if i == m or j == n or n - j < m - i:
7 return False
8 for k in range(j, n):
9 t = s[j : k + 1]
10 if d.get(pattern[i]) == t:
11 if dfs(i + 1, k + 1):
12 return True
13 if pattern[i] not in d and t not in vis:
14 d[pattern[i]] = t
15 vis.add(t)
16 if dfs(i + 1, k + 1):
17 return True
18 d.pop(pattern[i])
19 vis.remove(t)
20 return False
21
22 m, n = len(pattern), len(s)
23 d = {}

Callers

nothing calls this directly

Calls 5

dfsFunction · 0.50
getMethod · 0.45
addMethod · 0.45
popMethod · 0.45
removeMethod · 0.45

Tested by

no test coverage detected