Fewest copies of a string that contain another string as a subsequence

Quick Overview

Given two lowercase strings s and t, find the fewest copies of s that, joined end to end, contain t as a subsequence, or return -1 when no number of copies works. The problem tests greedy string matching, a proof that the greedy choice is optimal, and fast lookups for inputs of up to 100,000 characters.

Fewest copies of a string that contain another string as a subsequence

Company: Uber

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given two strings `s` and `t`, find the minimum number of times `s` must be appended to itself so that `t` becomes a subsequence of the resulting string. In other words, return the smallest integer `k` such that `t` is a subsequence of `s` repeated `k` times (`s + s + ... + s`, with `k` copies of `s`). If `t` can never be a subsequence, however many times `s` is repeated, return `-1`. ### Function Signature ```python def min_appends(s: str, t: str) -> int: ``` ### Rules - The answer counts copies of `s`. If `t` is already a subsequence of `s` itself, the answer is `1`. - A subsequence keeps the relative order of the characters, and its characters do not have to be adjacent. For example, `"ace"` is a subsequence of `"abcde"`. ### Constraints - `1 <= s.length <= 10^5` - `1 <= t.length <= 10^5` - `s` and `t` consist of lowercase English letters. ### Examples ```text Input: s = "abc", t = "abcbc" Output: 2 ``` One copy, `"abc"`, holds only the first three characters of `t`. Two copies, `"abcabc"`, contain `"abcbc"`: a, b and c come from the first copy, and b and c from the second. ```text Input: s = "ab", t = "d" Output: -1 ``` The letter d never appears in `s`, so no number of copies can contain `t`. ```text Input: s = "xyz", t = "xzyxz" Output: 3 ``` In `"xyzxyz"` the earliest matches are x at index 0, z at 2 and y at 4, and no x remains after index 4, so two copies are not enough. A third copy supplies x at index 6 and z at index 8.

Overview: Given two lowercase strings s and t, find the fewest copies of s that, joined end to end, contain t as a subsequence, or return -1 when no number of copies works. The problem tests greedy string matching, a proof that the greedy choice is optimal, and fast lookups for inputs of up to 100,000 characters.

Uber logo
Uber
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given two strings s and t, find the minimum number of times s must be appended to itself so that t becomes a subsequence of the resulting string.

In other words, return the smallest integer k such that t is a subsequence of s repeated k times (s + s + ... + s, with k copies of s). If t can never be a subsequence, however many times s is repeated, return -1.

Function Signature

def min_appends(s: str, t: str) -> int:

Rules

  • The answer counts copies of s . If t is already a subsequence of s itself, the answer is 1 .
  • A subsequence keeps the relative order of the characters, and its characters do not have to be adjacent. For example, "ace" is a subsequence of "abcde" .

Constraints

  • 1 <= s.length <= 10^5
  • 1 <= t.length <= 10^5
  • s and t consist of lowercase English letters.

Examples

Input: s = "abc", t = "abcbc"
Output: 2

One copy, "abc", holds only the first three characters of t. Two copies, "abcabc", contain "abcbc": a, b and c come from the first copy, and b and c from the second.

Input: s = "ab", t = "d"
Output: -1

The letter d never appears in s, so no number of copies can contain t.

Input: s = "xyz", t = "xzyxz"
Output: 3

In "xyzxyz" the earliest matches are x at index 0, z at 2 and y at 4, and no x remains after index 4, so two copies are not enough. A third copy supplies x at index 6 and z at index 8.