3849. Glasgow Multi-Word Phonetic Palindrome III
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
- Read words from
headin the forward direction, stopping immediately before revisitinghead. A word is a maximal run of letters. - For each word
w, appendpronunciation[w][k]to the phoneme sequence. - Before pronouncing the next word, let
ibe the zero-based index of the gap just encountered, andgits number of spaces. Updatekusing both rules below, simultaneously:- Increase
kby 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
kby 1 ifg > j + i.
- Increase
- 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 ≤ initialk≤ 105. - The list is a single valid circular doubly linked list.
headis 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.