/291. Bijective Pattern String Mapping

291. Bijective Pattern String Mapping

Medium
Hash Table48.8% acceptance

Given a pattern string consisting of lowercase English letters and a target string consisting of lowercase English letters separated by spaces, determine if there exists a bijective mapping from characters in the pattern to non-empty substrings of the target string such that replacing each character in the pattern with its mapped substring yields the target string. Each character in the pattern must map to a unique substring, and each substring must map to a unique character.

Example 1

Input: pattern=abc, target=red blue green

Output: True

Explanation: Possible mapping: a->red, b->blue, c->green.

Example 2

Input: pattern=aba, target=cat dog cat

Output: True

Explanation: Possible mapping: a->cat, b->dog.

Example 3

Input: pattern=aaa, target=dog dog dog

Output: True

Explanation: Possible mapping: a->dog.

Example 4

Input: pattern=ab, target=dog dog

Output: False

Explanation: Both pattern characters map to the same substring, violating bijection.

Constraints

  • 1 <= len(pattern) <= 20
  • 1 <= len(target) <= 100
  • pattern consists of lowercase English letters only
  • target consists of lowercase English letters and spaces only
Python (current runtime)

Case 1

Input: pattern='xyz', target='one two three'

Expected: True

Case 2

Input: pattern='xyx', target='apple banana apple'

Expected: True

Case 3

Input: pattern='xyx', target='apple banana banana'

Expected: False

Case 4

Input: pattern='abcd', target='alpha beta gamma delta'

Expected: True

Case 5

Input: pattern='aabb', target='foo foo bar bar'

Expected: True