Talentd
FleetCode

291. Word Pattern II

Medium49.1% acceptance6 companiesPremium elsewhere, free here

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.
9:32
Still stuck?Read the written solution, or watch a walkthrough.
Open solution

Similar problems

Word PatternEasy
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
"abab"
"redblueredblue"
Input
"aaaa"
"asdasdasdasd"
Input
"aabb"
"xyzabcxzyabc"
Your code is saved on this device.Solution