Stable Unique Values and Chained Word Stitching
Arrays & HashingAuto-graded · 19 test casesLast updated
Tests order-preserving deduplication with a set and a sequential string merge that collapses a single shared boundary character, including empty-string edge cases.
Stable Unique Values and Chained Word Stitching
Write a single function that solves two small, independent tasks and returns both answers together.
Task A: Keep first occurrences
Given a list of integers values, build a new list that contains each distinct value exactly once. A value must appear at the position of its first occurrence in the input, and the relative order of these first occurrences must match the input.
Task B: Stitch words in order
Given a list of lowercase strings pieces, build one string by processing the pieces strictly from left to right, in the order given. Do not reorder them.
- Start with the first piece as the current result. If
piecesis empty, the result is the empty string. - For each following piece:
- If both the current result and the piece are non-empty, and the last character of the result equals the first character of the piece, append the piece without its first character (the shared character is written only once).
- Otherwise, append the piece unchanged.
Only a single character can ever be shared at a boundary. Never look for longer suffix/prefix overlaps.
Output
Return a two-element list [unique_values, stitched]:
unique_values: the list from Task Astitched: the string from Task B
Constraints
0 <= len(values) <= 10^50 <= len(pieces) <= 10^4- Values are integers.
- Pieces consist of lowercase English letters and may be empty strings.
- The total number of characters across all pieces is at most
10^5. - Pieces are merged in the given order only; no reordering.
Examples
Example 1
Input: unique_and_stitch([1, 2, 1, 3, 2, 4], ['abc', 'rgn', 'ctr'])
Output: [[1, 2, 3, 4], 'abcrgnctr']
Example 2
Input: unique_and_stitch([5, 5, 5, 2, 2, 1], ['cat', 'top', 'pear'])
Output: [[5, 2, 1], 'catopear']
Example 3
Input: unique_and_stitch([], [])
Output: [[], '']
Hints
Hint 1
For the first task, scan once while keeping a set of values already seen; append a value to the output only when it is not in the set yet.
Hint 2
For the second task, the only thing that matters at each step is the last character of what you have built so far and the first character of the next piece.
Hint 3
Guard against empty strings before indexing the last or first character, and remember that a one-letter piece that overlaps contributes nothing new.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Task A. Scan values once, keeping a set of values already seen and an output list. When a value is not in the set, add it to the set and append it to the output. The set gives O(1) average membership checks, and the output list preserves order.
Task B. Process pieces left to right while tracking the last character of the result built so far. For each non-empty piece:
- If the tracked last character equals the piece's first character, append
piece[1:]. - Otherwise append the whole piece.
Then update the tracked last character to the piece's last character. Empty pieces add nothing and leave the tracked character unchanged (so ["ab", "", "bc"] gives "abc"). If the first piece is empty, the tracked character is empty, so no overlap is possible at the next step. Collecting parts in a list and joining once avoids repeated string copying.
Why it is correct
- Dedup: A value is added only the first time it is seen, and the scan is left to right, so each distinct value appears once, at the position of its first occurrence, with first-occurrence order preserved.
- Stitch: After each step, the collected parts concatenate to exactly the correct merge of the pieces processed so far, and the tracked character equals that merge's last character. Each step depends only on that boundary, so the result is deterministic. Skipping empty pieces is correct since they contribute no characters and cannot change the boundary.
Complexity
With n = len(values) and L the total number of characters across pieces:
- Time: O(n + L) on average
- Space: O(n + L)
Reference solution
from typing import Any, List
def unique_and_stitch(values: List[int], pieces: List[str]) -> List[Any]:
seen = set()
unique_values = []
for v in values:
if v not in seen:
seen.add(v)
unique_values.append(v)
if not pieces:
return [unique_values, ""]
parts = [pieces[0]]
last = pieces[0][-1] if pieces[0] else ""
for piece in pieces[1:]:
if not piece:
continue
if last and last == piece[0]:
parts.append(piece[1:])
else:
parts.append(piece)
last = piece[-1]
return [unique_values, "".join(parts)]