Find start indices of a pattern that lie within k positions of another pattern's occurrence

Quick Overview

Find every start index of pattern a in a string that has an occurrence of pattern b starting within k positions of it, returned in ascending order. It tests substring matching, working with sorted lists of positions, and using binary search or two pointers to keep the distance check fast on long inputs.

Find start indices of a pattern that lie within k positions of another pattern's occurrence

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a string `s`, two pattern strings `a` and `b`, and an integer `k`. An index `i` is beautiful when an occurrence of `a` starts at `i` and some occurrence of `b` starts at an index `j` with `|i - j| <= k`. Return all beautiful indices in ascending order. ### Function Signature ```python def beautiful_indices(s: str, a: str, b: str, k: int) -> list[int]: ... ``` ### Rules - Indices are 0-based. `a` occurs at `i` when `0 <= i <= len(s) - len(a)` and `s[i:i + len(a)] == a`. The same definition applies to `b` at `j`. - `j` may be smaller than, equal to or larger than `i`, and the two occurrences may overlap. `a` and `b` may be the same string. - Return each beautiful index once, sorted ascending. Return an empty list when there is none. ### Constraints - `1 <= k <= len(s) <= 10^5` - `1 <= len(a), len(b) <= 10` - `s`, `a` and `b` contain only lowercase English letters. ### Examples ```text Input: s = "isawsquirrelnearthesquirrelpark", a = "squirrel", b = "park", k = 15 Output: [19] ``` "squirrel" starts at 4 and at 19, and "park" starts only at 27. Index 19 qualifies because |19 - 27| = 8 <= 15, while index 4 does not because |4 - 27| = 23. ```text Input: s = "abcd", a = "a", b = "a", k = 4 Output: [0] ``` "a" occurs only at 0, and the same occurrence counts as `b` with j = 0, so |0 - 0| = 0 <= 4. ```text Input: s = "abcabcab", a = "ab", b = "ca", k = 1 Output: [3,6] ``` "ab" starts at 0, 3 and 6, and "ca" starts at 2 and 5. Index 3 is next to the "ca" at 2 and index 6 is next to the "ca" at 5. Index 0 is 2 away from the nearest "ca", which is more than k.

Overview: Find every start index of pattern a in a string that has an occurrence of pattern b starting within k positions of it, returned in ascending order. It tests substring matching, working with sorted lists of positions, and using binary search or two pointers to keep the distance check fast on long inputs.

Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a string s, two pattern strings a and b, and an integer k. An index i is beautiful when an occurrence of a starts at i and some occurrence of b starts at an index j with |i - j| <= k.

Return all beautiful indices in ascending order.

Function Signature

def beautiful_indices(s: str, a: str, b: str, k: int) -> list[int]:
    ...

Rules

  • Indices are 0-based. a occurs at i when 0 <= i <= len(s) - len(a) and s[i:i + len(a)] == a . The same definition applies to b at j .
  • j may be smaller than, equal to or larger than i , and the two occurrences may overlap. a and b may be the same string.
  • Return each beautiful index once, sorted ascending. Return an empty list when there is none.

Constraints

  • 1 <= k <= len(s) <= 10^5
  • 1 <= len(a), len(b) <= 10
  • s , a and b contain only lowercase English letters.

Examples

Input: s = "isawsquirrelnearthesquirrelpark", a = "squirrel", b = "park", k = 15
Output: [19]

"squirrel" starts at 4 and at 19, and "park" starts only at 27. Index 19 qualifies because |19 - 27| = 8 <= 15, while index 4 does not because |4 - 27| = 23.

Input: s = "abcd", a = "a", b = "a", k = 4
Output: [0]

"a" occurs only at 0, and the same occurrence counts as b with j = 0, so |0 - 0| = 0 <= 4.

Input: s = "abcabcab", a = "ab", b = "ca", k = 1
Output: [3,6]

"ab" starts at 0, 3 and 6, and "ca" starts at 2 and 5. Index 3 is next to the "ca" at 2 and index 6 is next to the "ca" at 5. Index 0 is 2 away from the nearest "ca", which is more than k.