#K6616. Word Pattern Matching

    ID: 32359 Type: Default 1000ms 256MiB

Word Pattern Matching

Word Pattern Matching

Given two strings: a pattern and a sentence s, determine whether the sentence follows the same pattern. Each character in the pattern must uniquely map to one word in s and vice versa. Formally, let \( pattern \) be a string of characters, and let \( s \) be a string of space-separated words. The task is to check if there exists a bijection between characters in \( pattern \) and words in \( s \).

For example, when pattern is "abba" and s is "dog cat cat dog", the answer is True because the mapping is consistent. However, for pattern "abba" and s "dog cat cat fish", the answer is False.

inputFormat

The input consists of two lines:

  • The first line contains the pattern string.
  • The second line contains the sentence s with words separated by spaces.

outputFormat

Output a single line with either "True" or "False" (case sensitive) indicating whether the sentence follows the given pattern.

## sample
abba
dog cat cat dog
True