Check whether a string follows a letter pattern with a one-to-one mapping to substrings

Quick Overview

Decide whether a string can be cut into non-empty pieces that follow a letter pattern, where each letter always stands for the same piece and different letters stand for different pieces. It tests exhaustive search with a one-to-one mapping kept consistent in both directions, and reasoning about the cost of trying every split.

Check whether a string follows a letter pattern with a one-to-one mapping to substrings

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

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 ```python 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 ```text 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. ```text 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". ```text 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.

Overview: Decide whether a string can be cut into non-empty pieces that follow a letter pattern, where each letter always stands for the same piece and different letters stand for different pieces. It tests exhaustive search with a one-to-one mapping kept consistent in both directions, and reasoning about the cost of trying every split.

General logo
General
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

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.