Problem 202956 · medium · Phase 02 Linear Data Structures

Rhyme Scheme

hash maps · bijection · strings

A poem's rhyme scheme is a string such as "ABAB": lines with the same letter must end with the same word, and lines with different letters must end with different words. Given scheme and the list endings of last words (one per line), return True if the poem follows the scheme exactly.

Examples

Input:  scheme = "ABAB", endings = ["moon", "night", "moon", "night"]
Output: True

Input:  scheme = "ABAB", endings = ["moon", "night", "june", "night"]
Output: False
Explanation: both A lines must end with the same word.

Input:  scheme = "AA", endings = ["sea", "sea", "sea"]
Output: False

Constraints

  • 0 <= len(scheme), len(endings) <= 10**5
  • Target complexity: O(n) time.

Goals

  • Map characters to words and words back to characters
  • Reject partial matches when lengths differ
Starting Python…