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.

You are given a string `s`, two pattern strings `a` and `b`, and an integer `k`. Indices are 0-based. Pattern `a` occurs at index `i` when `0 <= i <= len(s) - len(a)` and `s[i:i + len(a)] == a`, meaning the `len(a)` characters of `s` that start at `i` spell `a`. The same definition applies to `b` at an index `j`. A pattern longer than `s` never occurs. An index `i` is beautiful when `a` occurs at `i` and `b` occurs at some index `j` with `|i - j| <= k`. The index `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, in which case the occurrence of `a` at `i` also counts as an occurrence of `b` at `j = i`. Every start that satisfies the definition counts, so two occurrences of the same pattern may overlap too. Implement `beautiful_indices(s, a, b, k)` and return every beautiful index exactly once, sorted in ascending order. Return an empty list when there is none. Every index and every value of `k` is at most 10^5, so all values fit in a 32-bit signed integer. Example 1: Input: s = "isawsquirrelnearthesquirrelpark", a = "squirrel", b = "park", k = 15 Output: [19] Explanation: "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. Example 2: Input: s = "abcabcab", a = "ab", b = "ca", k = 1 Output: [3, 6] Explanation: "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. Constraints: - 1 <= k <= len(s) <= 10^5 - 1 <= len(a), len(b) <= 10 - s, a and b contain only lowercase English letters.

Constraints

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

Examples

Input: ('isawsquirrelnearthesquirrelpark', 'squirrel', 'park', 15)

Expected Output: [19]

Explanation: Source example 1. s has length 31: squirrel starts at 4 and 19, park at 27, and only 19 is within 15.

Input: ('abcd', 'a', 'a', 4)

Expected Output: [0]

Explanation: Source example: a equals b, so the only occurrence at 0 pairs with itself at distance 0, with k = len(s) = 4.

Hints

  1. Find every start of each pattern, including starts whose occurrences overlap; a pattern longer than s has no start at all.
  2. For a fixed start i of a, only starts of b in the closed range from i - k to i + k matter, and both ends of that range count.
  3. When a and b are the same string, an occurrence of a also serves as its own b at distance 0.

Beautiful Indices Within Distance K of a Second Pattern

mediumGeneralSoftware Engineer

You are given a string s, two pattern strings a and b, and an integer k. Indices are 0-based. Pattern a occurs at index i when 0 <= i <= len(s) - len(a) and s[i:i + len(a)] == a, meaning the len(a) characters of s that start at i spell a. The same definition applies to b at an index j. A pattern longer than s never occurs.

An index i is beautiful when a occurs at i and b occurs at some index j with |i - j| <= k. The index 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, in which case the occurrence of a at i also counts as an occurrence of b at j = i. Every start that satisfies the definition counts, so two occurrences of the same pattern may overlap too.

Implement beautiful_indices(s, a, b, k) and return every beautiful index exactly once, sorted in ascending order. Return an empty list when there is none. Every index and every value of k is at most 10^5, so all values fit in a 32-bit signed integer.

Example 1: Input: s = "isawsquirrelnearthesquirrelpark", a = "squirrel", b = "park", k = 15 Output: [19] Explanation: "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.

Example 2: Input: s = "abcabcab", a = "ab", b = "ca", k = 1 Output: [3, 6] Explanation: "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.

Constraints:

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

Examples

Example 1
Input
('isawsquirrelnearthesquirrelpark', 'squirrel', 'park', 15)
Output
[19]
Notes
Source example 1. s has length 31: squirrel starts at 4 and 19, park at 27, and only 19 is within 15.
Example 2
Input
('abcd', 'a', 'a', 4)
Output
[0]
Notes
Source example: a equals b, so the only occurrence at 0 pairs with itself at distance 0, with k = len(s) = 4.

Constraints

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

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def beautiful_indices(s: str, a: str, b: str, k: int) -> list[int]:
Sample call
beautiful_indices('isawsquirrelnearthesquirrelpark', 'squirrel', 'park', 15)
Language
Case 1
Input
('isawsquirrelnearthesquirrelpark', 'squirrel', 'park', 15)
Expected
[19]
Case 2
Input
('abcd', 'a', 'a', 4)
Expected
[0]

Browse More Questions

Show the approach

Approach

Scan s once for each pattern. For every start j from 0 to len(s) - len(b), compare the next len(b) characters with b and record j when they match; the recorded starts come out in ascending order. Then visit every start i of a in ascending order while keeping a pointer p into the list of b starts.

For the current i, move p forward past every b start smaller than i - k. The lower end i - k never decreases as i grows, so a start skipped for one i lies outside the window of every later i and the pointer never has to move back. After the move, the start at p (if one exists) is the smallest b start that is at least i - k. Index i is beautiful exactly when that start exists and is at most i + k: any b start in the closed range from i - k to i + k is at least i - k, so the smallest such start also lies in the range whenever any start does. Both ends of the range are included, which keeps |i - j| = k and drops |i - j| = k + 1.

Each i is appended at most once and in increasing order, so the result is sorted and free of duplicates. Edge cases: a pattern longer than s has an empty range of starts and never occurs, which gives an empty result; when a == b each occurrence finds itself at distance 0; overlapping occurrences of either pattern are all found because every start is checked. Each comparison reads at most 10 characters, so the scans take O(n * L) time with L <= 10, and the pointer moves at most once per b start.

Time complexity:
O(n * L), where n = len(s) and L = max(len(a), len(b)) <= 10
Space complexity:
O(n)