← 返回 databricks 的题目列表String Index Partition Mapping (Minimum Partitions after Deletion)
类型:online_judge
Coding: Represent string b using indexed substring partitions from a
Given two strings a and b, select several contiguous ranges in a. Concatenate the corresponding substrings in order to form exactly b.
A range is represented as [l, r] (1-based, l <= r) meaning substring a[l..r].
Return a list of ranges parts = [[l1,r1],[l2,r2],...] such that:
a[l1..r1] + a[l2..r2] + ... == b
If multiple solutions exist, return one with the minimum number of ranges (minimum partitions).
If impossible, return an empty list.
Example
Input:
a = "abcdbcd"
b = "sabcd"
Output (one possible):
[[1,3],[4,4],[2,4]]
The original anecdote example may not be fully consistent; rely on the formal rules above.
Follow-up
Delete one character (you may choose the position to improve the result), then recompute and return the minimum-partition representation.
State whether you delete from a or from b and implement accordingly.
Constraints (reasonable assumptions allowed)
1 <= len(a), len(b) <= 2e5
Character set: lowercase letters or printable ASCII
I/O (stdin/stdout)
stdin: two lines: a, then b stdout:
first line: minimum number of partitions k (-1 if impossible)
next k lines: l r per partition
Example
Input
abcdbcd
abcd
Output
2
1 4
5 7