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 pattern maps to exactly one non-empty substring of s.
  • No two different characters in pattern map to the same substring.
  • Replacing each character in pattern with its mapped substring, in order, produces exactly s.

Return true if such a mapping exists; otherwise, return false.

Example 1
Inputpattern = "abab", s = "redblueredblue"
Outputtrue
The mapping a -> red and b -> blue produces redblueredblue.
Example 2
Inputpattern = "aabb", s = "xyzabcxzyabc"
Outputfalse
There 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.

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate