LeetCruel
It's actually not too bad.

3849. Glasgow Multi-Word Phonetic Palindrome III

Easy Linked List String Simulation

You are given the head of a circular doubly linked list. Each node contains a lowercase English letter or a space. Starting at head, one complete traversal represents an utterance by a speaker in Glasgow.

You are also given integers k (the speaker's initial intoxication level) and j (the initial pause threshold), and a pronunciation dictionary pronunciation. The dictionary maps a word and an integer intoxication level to a nonempty list of phoneme tokens. All pronunciations are supplied; you do not need to know the dialect.

Return true if the phoneme sequence of the utterance is a palindrome, and false otherwise.

Pronunciation rules

  1. Read words from head in the forward direction, stopping immediately before revisiting head. A word is a maximal run of letters.
  2. For each word w, append pronunciation[w][k] to the phoneme sequence.
  3. Before pronouncing the next word, let i be the zero-based index of the gap just encountered, and g its number of spaces. Update k using both rules below, simultaneously:
    • Increase k by 1 if the current word ends in two consecutive consonants, or its last letter and the next word's first letter are both consonants. If both conditions hold, increase it only once.
    • Decrease k by 1 if g > j + i.
  4. The last word has no outgoing gap. Do not process the circular link as another word boundary. Spaces produce no phonemes.

A consonant is any letter other than a, e, i, o, u. Phoneme tokens are atomic and case-sensitive. The supplied dictionary is authoritative, even if a pronunciation is unusual.

Example 1:

Input: head = "no   own", k = 0, j = 1
pronunciation = {
  "no": {0: ["N", "OW"]},
  "own": {-1: ["OW", "N"]}
}
Output: true

Explanation: The first word uses level 0. The gap contains 3 spaces, exceeding the threshold 1, so k = -1. The phoneme sequence is [N, OW, OW, N], a palindrome.

Example 2:

Input: head = "paul    potts", k = 0, j = 2
pronunciation = {
  "paul": {0: ["P", "AO", "L"]},
  "potts": {0: ["P", "O", "T", "S"]}
}
Output: false

Explanation: The consonants l and p increase the level by 1. The four-space pause decreases it by 1. Both changes cancel, so Potts is pronounced at level 0.

Example 3:

Input: head = "hush  no   own", k = 0, j = 1
pronunciation = {
  "hush": {0: ["HH", "AH", "SH"]},
  "no": {0: ["N", "OW"]},
  "own": {-1: ["OW", "N"]}
}
Output: false

Explanation: The first gap has threshold 1: the consonant rule and pause rule cancel. The second gap has threshold 2, so the level decreases to -1. The phoneme sequence is [HH, AH, SH, N, OW, OW, N], which is not a palindrome.

Example 4:

Input: head = "last  stall", k = 0, j = 2
pronunciation = {
  "last": {0: ["L", "AA", "S", "T"]},
  "stall": {
    0: ["S", "T", "AO", "L"],
    1: ["S", "AA", "L"],
    2: ["S", "T", "AO", "L"]
  }
}
Output: true

Explanation: "last" ends in two consonants, and t and s are also both consonants. Both conditions hold, so the level increases only once, to 1. The two-space gap does not exceed the threshold 2. The phoneme sequence is [L, AA, S, T, S, AA, L], a palindrome, although the two words contribute different numbers of phonemes.

Constraints:

  • 1 ≤ number of nodes ≤ 105.
  • 0 ≤ j ≤ 105; −105 ≤ initial k ≤ 105.
  • The list is a single valid circular doubly linked list. head is the first letter of the utterance, and its predecessor is the final letter.
  • Every required (word, level) pair exists in the dictionary.
  • At most 105 phoneme tokens are emitted in one traversal.
  • You may not modify the linked list or the dictionary.

Follow-up: Can you solve it in O(n + p) time and O(L) extra space, where n is the number of nodes, p is the number of emitted phonemes, and L is the length of the longest word? You may not store the phoneme sequence or the list of words.

Hint 1Keep track of the current intoxication level and the gap index as you traverse.
Hint 2For the follow-up, the total change in level can be computed in a first pass. Can you undo a boundary update when traversing backwards?
Contest problem · Weekly Contest 487   |   Practice version
Code
Python3
Editable practice editor · Local execution unavailable
Testcase
Sample inputs and expected outputs are provided for manual verification.