Ctrl K

Words Following a Letter Pattern

Easy
You get a pattern pattern of lowercase Latin letters and a sentence sentence made of words separated by single spaces. The sentence fits the pattern when it has exactly as many words as the pattern has letters and letters and words correspond one to one: equal letters stand for equal words and different letters stand for different words. The letter at position i stands for the i-th word. Return True if the sentence fits the pattern, and False otherwise.
Example 1
pattern="xyyx", sentence="red blue blue red" → True

x ↔ red and y ↔ blue: a one-to-one match, so the answer is True.

Example 2
pattern="xyz", sentence="sun sun moon" → False

x and y are different letters but would both stand for sun, which is not allowed, so the answer is False.

Example 3
pattern="aab", sentence="go go go" → False
Constraints
  • 1 ≤ len(pattern) ≤ 300, and pattern consists of lowercase Latin letters
  • 1 ≤ len(sentence) ≤ 3000
  • sentence consists of lowercase Latin letters and spaces
  • Words are separated by exactly one space, with no leading or trailing spaces
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsValue of a Roman Numeral EasySame Letter Cipher EasySame Letters, Reshuffled EasySame When Flipped EasyShuffle Into a Palindrome Easy
loading Python…
Input
pattern="xyyx", sentence="red blue blue red"
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 150 tests on the server, hidden ones included.