Skip to content
Decoder

Word Pattern

Easy

Problem

Given a pattern of letters and a string s of words separated by single spaces, return true if s follows the pattern: there is a one-to-one match between letters and words, so each letter always stands for the same word and no two letters stand for the same word.

Example 1

Input
pattern = "abba", s = "dog cat cat dog"
Output
true

Example 2

Input
pattern = "abba", s = "dog cat cat fish"
Output
false

Example 3

Input
pattern = "abba", s = "dog dog dog dog"
Output
false
Why
a and b both mean dog

Constraints

  • 1 <= pattern.length <= 300
  • pattern contains only lowercase English letters.
  • 1 <= s.length <= 3000
  • s contains lowercase letters and single spaces, with no leading or trailing space.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.