Word Pattern

Given a `pattern` of lowercase letters and a string `s` of words separated by single spaces, decide whether `s` follows the pattern. Following means there is a bijection: each letter maps to exactly one word, each word maps to exactly one letter, and replacing every letter of the pattern with its word gives `s`.

30:00

Word Pattern
easy
Free written solution
Topics
Companies

Given a pattern of lowercase letters and a string s of words separated by single spaces, decide whether s follows the pattern. Following means there is a bijection: each letter maps to exactly one word, each word maps to exactly one letter, and replacing every letter of the pattern with its word gives s.

Example 1:
Input: {"pattern":"abba","s":"dog cat cat dog"}
Output: true
Constraints:
  • 1≤pattern.length≤3001 \leq \text{pattern.length} \leq 300

  • pattern contains only lower-case English letters.

  • 1≤s.length≤30001 \leq \text{s.length} \leq 3000

  • s contains only lowercase English letters and spaces.

  • s has no leading or trailing spaces, and words are separated by a single space.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"pattern":"abba","s":"dog cat cat dog"}

Split s on spaces into 4 words.

pattern letter over its word

a

dog

b

cat

b

cat

a

dog

char_to_word

(empty)

word_to_char

(empty)

pairs checked 0 / 4

…

Variables
VariableValue
words4
DepthFunction Call
Stack empty
0/19