Given a string pattern and a string s, decide whether s follows the pattern.
s follows pattern when each distinct letter of pattern can be assigned a non-empty string so that replacing every letter of pattern by its assigned string, in order, produces exactly s. The assignment must be a bijection: a letter always stands for the same string, and two different letters never stand for the same string. Return True if such an assignment exists and False otherwise.
Function Signature
def word_pattern_match(pattern: str, s: str) -> bool:
Rules
-
Every letter of
pattern
maps to a non-empty substring of
s
.
-
Two different letters must map to different strings, even if sharing a string would make the concatenation equal
s
.
-
The concatenation must equal all of
s
. Matching only a prefix of
s
does not count.
Constraints
-
1 <= len(pattern) <= 20
-
1 <= len(s) <= 20
-
pattern
and
s
consist of lowercase English letters.
Examples
Input: pattern = "abab", s = "redblueredblue"
Output: True
Map a to "red" and b to "blue". Then "red" + "blue" + "red" + "blue" is "redblueredblue", and the two letters use different strings.
Input: pattern = "aaaa", s = "asdasdasdasd"
Output: True
All four positions use the same letter, so s must be one string repeated four times. Mapping a to "asd" gives "asdasdasdasd".
Input: pattern = "ab", s = "xx"
Output: False
Both pieces must be non-empty, so the only split is "x" and "x". That would map a and b to the same string, which the bijection forbids.