Find Array Peak Index and Form Unique Word Abbreviation List
Heaps, Sorting & SearchAuto-graded · 23 test casesLast updated
Locate a local peak index in an array via binary search and generate minimal unambiguous string abbreviations for a list of words.
Write a function that solves two combined sub-problems and returns their outputs in a single list format.
Sub-problem 1: Binary Search Peak Finding
Given an array of integers a of length $N \ge 1$:
- Assume virtual values out of bounds are $-\infty$: $a[-1] = a[N] = -\infty$.
- An index $i$ is a peak if $a[i] > a[i-1]$ and $a[i] > a[i+1]$.
- Locate a peak index using binary search by maintaining standard low/high boundaries
lo = 0,hi = N - 1,mid = (lo + hi) // 2, movinglo = mid + 1if $a[\text{mid}] < a[\text{mid}+1]$ andhi = midotherwise.
Sub-problem 2: Unique String Abbreviations
Given a list of distinct strings k (words composed of lowercase letters):
- Words of length $\le 3$ cannot be abbreviated to a shorter form, so they always remain unchanged as the full word.
- For a word of length $> 3$, an abbreviation with prefix length $p$ ($1 \le p < \text{len}(w) - 1$) takes the form: $$\text{word}[:p] + \text{str}(\text{len}(\text{word}) - p - 1) + \text{word}[-1]$$
- If $p \ge \text{len}(\text{word}) - 2$, the abbreviation length would not be strictly shorter than the word length, so the original full word is used instead.
- Initially, set prefix length $p = 1$ for all words of length $> 3$.
- In each step, form the current candidate string (either abbreviated form or full word) for every word in
k. - Group the words by their candidate string.
- Identify all groups that contain 2 or more words (collisions).
- For every word belonging to any colliding group, increment its prefix length $p$ by 1 (up to $\text{len}(\text{word})$ if $p$ reaches $\text{len}(\text{word}) - 2$, effectively reverting it to its full word). Words not in a collision keep their current $p$.
- Repeat the process iteratively until all candidate strings across the list are mutually distinct (i.e. no collision groups exist).
Return Format
Return a list containing two elements:
[peak_index]: A list with the single peak index found in sub-problem 1.abbreviations: A list of strings containing the final unique abbreviation or full word for each word inkin the original input order.
Constraints
- $1 \le \text{len}(a) \le 10^5$
- $-2^{31} \le a[i] \le 2^{31} - 1$
- $0 \le \text{len}(k) \le 1000$
- $1 \le \text{len}(w) \le 100$ for each word $w \in k$
- All words in $k$ consist of lowercase English letters and are distinct.
Examples
Example 1
Input: find_peak_and_unique_abbreviations([1, 2, 3, 1], ['international', 'interview', 'internet'])
Output: [[2], ['i11l', 'i7w', 'i6t']]
Example 2
Input: find_peak_and_unique_abbreviations([1], [])
Output: [[0], []]
Example 3
Input: find_peak_and_unique_abbreviations([3, 2, 1], ['apple', 'apply'])
Output: [[0], ['a3e', 'a3y']]
Example 4
Input: find_peak_and_unique_abbreviations([1, 3, 2, 4, 1], ['abcdef', 'abcxef', 'abcyef'])
Output: [[3], ['abcdef', 'abcxef', 'abcyef']]
Hints
Hint 1
For peak finding, compare a[mid] to a[mid + 1]. If a[mid] < a[mid + 1], a peak must lie to the right; otherwise, it lies at mid or to the left.
Hint 2
Start each word of length > 3 with prefix length p = 1 and generate initial candidate abbreviations.
Hint 3
Group words by their current candidate string. Any candidate string with multiple words constitutes a collision. Increment p for all words in colliding groups and repeat until no collisions remain.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This task consists of two clear steps:
-
Peak Finding:
- Perform binary search on array
a. - Compute
mid = (lo + hi) // 2. - Compare
a[mid]witha[mid + 1]. - If
a[mid] < a[mid + 1], an ascending slope guarantees a peak exists in[mid + 1, hi]. Setlo = mid + 1. - Otherwise,
a[mid] >= a[mid + 1], so a peak exists in[lo, mid]. Sethi = mid. - When
lo == hi,lois a valid peak index.
- Perform binary search on array
-
Iterative Abbreviation Collision Resolution:
- For each word of length $> 3$, track a prefix length $p$ starting at $1$. For words of length $\le 3$, set $p = \text{len}(w)$.
- Generate candidate abbreviations: if $p < \text{len}(w) - 2$, form $w[:p] + \text{str}(\text{len}(w) - p - 1) + w[-1]$. Otherwise, output the full word $w$.
- Group word indices by candidate abbreviation.
- If any candidate abbreviation has more than 1 assigned index, a collision occurs.
- Increment $p$ for every word in any colliding group.
- Repeat until no collisions exist, then return
[[peak_index], abbreviations].
Complexity
- Peak Finding: Runs in $O(\log N)$ time and $O(1)$ extra space, where $N = \text{len}(a)$.
- Unique Abbreviations: Let $M = \text{len}(k)$ and $L = \max(\text{len}(w))$. Each iteration takes $O(M \cdot L)$ time to compute abbreviations and hash them. In the worst case, $p$ increments up to $L$ times, resulting in $O(M \cdot L^2)$ total time.
- Space Complexity: $O(M \cdot L)$ to store candidate abbreviations and mapping groups.
from typing import List
def find_peak_and_unique_abbreviations(a: List[int], k: List[str]) -> List[List]:
lo, hi = 0, len(a) - 1
while lo < hi:
mid = (lo + hi) // 2
if a[mid] < a[mid + 1]:
lo = mid + 1
else:
hi = mid
peak = lo
prefixes = [1 if len(word) > 3 else len(word) for word in k]
abbreviations = list(k)
while True:
groups = {}
for i, word in enumerate(k):
prefix_len = prefixes[i]
if len(word) > 3 and prefix_len < len(word) - 2:
abbreviation = (
word[:prefix_len]
+ str(len(word) - prefix_len - 1)
+ word[-1]
)
else:
abbreviation = word
abbreviations[i] = abbreviation
groups.setdefault(abbreviation, []).append(i)
collisions = [indices for indices in groups.values() if len(indices) > 1]
if not collisions:
break
for indices in collisions:
for i in indices:
if prefixes[i] < len(k[i]) - 2:
prefixes[i] += 1
else:
prefixes[i] = len(k[i])
return [[peak], abbreviations]