Back to DSA sheet

Word Pattern

EasyArrays

Given a pattern of letters and a string of space-separated words, decide whether the words follow the pattern with a one-to-one correspondence: each letter maps to exactly one word and each word to exactly one letter.

Input format (stdin): the first line is the pattern, the second line is the space-separated words. Output true or false.

Examples
Input: abba dog cat cat dog
Output: true
a<->dog, b<->cat is consistent.
Input: abba dog cat cat fish
Output: false
a maps to both dog and fish.
Constraints
  • 1 <= |pattern| <= 300
  • Words are lowercase, space separated
Sheets
NeetCode 250
word-pattern.cpp3 sample tests
Loading editor
Test results

Run the sample tests to check your solution against expected output.

Custom input (stdin)
Output

Run your code to see its output.