-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path291-WordPatternII.go
More file actions
80 lines (71 loc) · 2.55 KB
/
291-WordPatternII.go
File metadata and controls
80 lines (71 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
// 291. Word Pattern II
// Given a pattern and a string s, return true if s matches the pattern.
// A string s matches a pattern if there is some bijective mapping of single characters to non-empty strings such
// that if each character in pattern is replaced by the string it maps to, then the resulting string is s.
// A bijective mapping means that no two characters map to the same string,
// and no character maps to two different strings.
// Example 1:
// Input: pattern = "abab", s = "redblueredblue"
// Output: true
// Explanation: One possible mapping is as follows:
// 'a' -> "red"
// 'b' -> "blue"
// Example 2:
// Input: pattern = "aaaa", s = "asdasdasdasd"
// Output: true
// Explanation: One possible mapping is as follows:
// 'a' -> "asd"
// Example 3:
// Input: pattern = "aabb", s = "xyzabcxzyabc"
// Output: false
// Constraints:
// 1 <= pattern.length, s.length <= 20
// pattern and s consist of only lowercase English letters.
import "fmt"
func wordPatternMatch(pattern string, s string) bool {
m, n := len(pattern), len(s)
pm, sm := map[byte]string{}, map[string]byte{}
var backtrace func(pi, si int) bool
backtrace = func(pi, si int) bool {
if pi >= m && si < n { return false }
if pi < m && si >= n { return false }
if pi >= m && si >= n { return true }
res := false
for i := si; i < n; i++ {
if v, ok := pm[pattern[pi]]; ok {
if v == s[si:i+1] && sm[s[si:i+1]] == pattern[pi] {
if backtrace(pi+1, i+1) { return true }
}
continue
}
if _, ok := sm[s[si:i+1]]; ok { continue }
pm[pattern[pi]] = s[si:i+1]
sm[s[si:i+1]] = pattern[pi]
if backtrace(pi+1, i+1) { return true }
delete(pm, pattern[pi])
delete(sm, s[si:i+1])
}
return res
}
return backtrace(0, 0)
}
func main() {
// Example 1:
// Input: pattern = "abab", s = "redblueredblue"
// Output: true
// Explanation: One possible mapping is as follows:
// 'a' -> "red"
// 'b' -> "blue"
fmt.Println(wordPatternMatch("abab","redblueredblue")) // true
// Example 2:
// Input: pattern = "aaaa", s = "asdasdasdasd"
// Output: true
// Explanation: One possible mapping is as follows:
// 'a' -> "asd"
fmt.Println(wordPatternMatch("aaaa","asdasdasdasd")) // true
// Example 3:
// Input: pattern = "aabb", s = "xyzabcxzyabc"
// Output: false
fmt.Println(wordPatternMatch("aabb","xyzabcxzyabc")) // false
}