← 返回 databricks 的题目列表Remove an interval by index and simplify (merge) cover intervals
类型:online_judge
You are given a string book and a list of intervals cover, where each interval (start, end) (possibly out of order) represents a substring cut from book. The collection of these substrings forms a cipher.
Task 1: Remove an interval by index
Remove the interval at position idx in the cover list (note: idx refers to the index in cover, not an index in book) and return the updated cover.
Task 2: Produce a simplified cipher after removal
After removing cover[idx], simplify the remaining intervals so that no two intervals are mergeable. Equivalently, merge all intervals that overlap or are adjacent.
Return:
the merged interval list sorted by start, and
the final cipher string obtained by concatenating book[start..end] (inclusive) for each merged interval in order.
Constraints
0 <= start <= end < n
cover may be unsorted
intervals may overlap or be adjacent
0 <= idx < len(cover)
Example
book = "abcdefghijk", cover = [(2,4),(0,1),(5,6)], idx=1. After removal: [(2,4),(5,6)]. After merging adjacency: [(2,6)]. Cipher: "cdefg".
Example
Input
book=abcdefghijk
cover=[(2,4),(0,1),(5,6)]
idx=1
Output
merged=[(2,6)]
cipher=cdefg