Count Contiguous Word Ranges Whose Concatenation Equals a Target String

Quick Overview

Count the contiguous ranges of a word list whose in-order concatenation equals a target string. The problem tests careful enumeration of index ranges, early pruning once a partial string can no longer match, and precise counting when several ranges spell the same text.

Count Contiguous Word Ranges Whose Concatenation Equals a Target String

Company: Miro

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a list of strings `words` and a string `target`. A concatenation is formed by choosing indices `i` and `j` with `0 <= i <= j < len(words)` and joining `words[i] + words[i + 1] + ... + words[j]` in order. Each concatenation is a candidate for replacement with `target`, and normally an external API would be called for every candidate to decide whether to replace it. To minimize the number of API calls, only the concatenations that exactly match `target` matter, because they are the only ones that would trigger a replacement. Return how many index ranges `(i, j)` produce a concatenation equal to `target`. ### Function Signature ```python def count_replacements(words: list[str], target: str) -> int: ``` ### Rules - Each pair `(i, j)` with `i <= j` is one concatenation and is counted at most once. Two different ranges that spell the same string are counted separately. - Words are joined exactly as given, with no separator. - Only contiguous ranges count. Words that are not next to each other in `words` cannot be combined. ### Constraints - `1 <= len(words) <= 50` - `1 <= len(words[i]) <= 10` - `1 <= len(target) <= 10` - All strings consist of lowercase English letters. ### Examples ```text Input: words = ["foo", "bar", "baz"], target = "foobar" Output: 1 ``` The range (0, 1) gives "foo" + "bar" = "foobar". Every other range gives "foo", "bar", "baz", "barbaz" or "foobarbaz", and none of those equals the target. ```text Input: words = ["a", "b", "c", "ab", "bc"], target = "abc" Output: 1 ``` Only the range (0, 2) works: "a" + "b" + "c" = "abc". The words "a" and "bc" would also spell "abc", but they sit at indices 0 and 4, so they do not form a contiguous range, and the adjacent pair "ab" + "bc" gives "abbc". ```text Input: words = ["a", "a", "a"], target = "aa" Output: 2 ``` The ranges (0, 1) and (1, 2) both give "aa", and each range counts. The full range (0, 2) gives "aaa" and single words give "a", so neither matches.

Overview: Count the contiguous ranges of a word list whose in-order concatenation equals a target string. The problem tests careful enumeration of index ranges, early pruning once a partial string can no longer match, and precise counting when several ranges spell the same text.

Miro logo
Miro
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given a list of strings words and a string target. A concatenation is formed by choosing indices i and j with 0 <= i <= j < len(words) and joining words[i] + words[i + 1] + ... + words[j] in order.

Each concatenation is a candidate for replacement with target, and normally an external API would be called for every candidate to decide whether to replace it. To minimize the number of API calls, only the concatenations that exactly match target matter, because they are the only ones that would trigger a replacement. Return how many index ranges (i, j) produce a concatenation equal to target.

Function Signature

def count_replacements(words: list[str], target: str) -> int:

Rules

  • Each pair (i, j) with i <= j is one concatenation and is counted at most once. Two different ranges that spell the same string are counted separately.
  • Words are joined exactly as given, with no separator.
  • Only contiguous ranges count. Words that are not next to each other in words cannot be combined.

Constraints

  • 1 <= len(words) <= 50
  • 1 <= len(words[i]) <= 10
  • 1 <= len(target) <= 10
  • All strings consist of lowercase English letters.

Examples

Input: words = ["foo", "bar", "baz"], target = "foobar"
Output: 1

The range (0, 1) gives "foo" + "bar" = "foobar". Every other range gives "foo", "bar", "baz", "barbaz" or "foobarbaz", and none of those equals the target.

Input: words = ["a", "b", "c", "ab", "bc"], target = "abc"
Output: 1

Only the range (0, 2) works: "a" + "b" + "c" = "abc". The words "a" and "bc" would also spell "abc", but they sit at indices 0 and 4, so they do not form a contiguous range, and the adjacent pair "ab" + "bc" gives "abbc".

Input: words = ["a", "a", "a"], target = "aa"
Output: 2

The ranges (0, 1) and (1, 2) both give "aa", and each range counts. The full range (0, 2) gives "aaa" and single words give "a", so neither matches.