Mid/Senior
Word Pattern II
Given a pattern and a string s, determine if s follows the same pattern.
A string s follows pattern if there exists a bijection between each character in pattern and a non-empty substring of s such that:
- Each character in
patternmaps to exactly one non-empty substring ofs. - No two different characters in
patternmap to the same substring. - Replacing each character in
patternwith its mapped substring, in order, produces exactlys.
Return true if such a mapping exists; otherwise, return false.
Example 1
Input
pattern = "abab", s = "redblueredblue"Output
trueThe mapping
a -> red and b -> blue produces redblueredblue.Example 2
Input
pattern = "aabb", s = "xyzabcxzyabc"Output
falseThere is no bijective assignment of substrings to
a and b that produces the entire string.Constraints
- 1 <= pattern.length <= 20
- 1 <= s.length <= 20
- pattern and s consist of lowercase English letters.