PracticeArrays

Aligned Word Joining

BeginnerArraystraversalaccumulationcomparison Time not estimated Not started

Context

Let two finite sequences be written in order. At each shared position, join the two strings into one string. The resulting sequence contains only positions present in both input sequences.

Problem

Implement a function that receives two lists of strings. Process their positions from left to right in alignment. For every position available in both lists, concatenate the first string with the second string, without adding characters or separators, and append the combined string to the result. Stop as soon as either list has no remaining element. Preserve the order of processed positions and omit all unmatched trailing elements. If either input list is empty, return an empty list.

Examples

Example 1
Input: left = ["red", "green", "blue"]right = ["apple", "pear", "berry"]
Output: ["redapple", "greenpear", "blueberry"]
Explanation: The three corresponding pairs are concatenated in order, producing ["redapple", "greenpear", "blueberry"].
Example 2
Input: left = ["sun", "moon", "star"]right = ["light", "rise"]
Output: ["sunlight", "moonrise"]
Explanation: Only the first two positions are shared, so the output is ["sunlight", "moonrise"].
Example 3
Input: left = []right = ["alpha", "beta"]
Output: []
Explanation: Because the left list is empty, the output is an empty list.

Constraints

  • 0 <= len(left) <= 50
  • 0 <= len(left[i]) <= 30 for every i
  • 0 <= len(right) <= 50
  • 0 <= len(right[i]) <= 30 for every i
  • left and right contain only lowercase letters.
  • Types: left is str[], right is str[]; result is str[]

Function signature

def join_aligned_words(left: list[str], right: list[str]) -> list[str]
left list[str]
The first ordered list of strings.
right list[str]
The second ordered list of strings.
returns list[str]
The ordered list formed by concatenating corresponding strings until the shorter input list is exhausted.

Adapted from MBPP problem task_213 (CC BY 4.0). Rewritten, extended and verified by Iksha.

Approved · c035-abs_667_v114 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1