390. 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: We can assign 'a' -> "red" and 'b' -> "blue", forming the string "redblueredblue" as per the pattern. The mapping is bijective since no two characters map to the same string and no character maps to multiple different strings.

Example 2:

Input: pattern = "aaaa", s = "asdasdasdasd"

Output: true

Explanation: We can assign 'a' -> "asd", and since all occurrences of 'a' map to "asd", the final string matches "asdasdasdasd". The mapping remains bijective.

Now Your Turn!

Pick the correct output for the given input

Input: pattern = "aabb", s = "xyzabcxzyabc"

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= pattern.length, s.length <= 20
  • pattern and s consist of only lowercase English letters.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
bool wordPatternMatch(string pattern, string s) {
// Your code goes here
}
};
Test Case

Input:

Pattern
Str