Trains the technique from
LeetCode 3006Find Beautiful Indices in the Given Array IThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A transcript is the lowercase string s. You are also given two lowercase marker words a and b, and a reach k.
A position i is flagged when both of the following hold:
a appears in the transcript starting at position i, meaning s[i .. i + a.length - 1] equals a;j where b appears in the transcript, meaning s[j .. j + b.length - 1] equals b, whose distance from i satisfies abs(i - j) <= k.The positions i and j may be equal and the two markers are allowed to overlap, and a and b may be the same word. Return every flagged position, sorted from smallest to largest. Return an empty list when no position is flagged.
Example 1
`a` starts at positions 0 and 3. Marker `b` starts at position 1, one away from 0, and at position 4, one away from 3, so both positions are flagged.
Example 2
`a` starts only at position 0 and `b` starts only at position 4. The distance is 4, which is more than the reach of 3, so nothing is flagged.
Example 3
`a` starts at position 4 and `b` starts at position 0. The distance is 4, which the reach allows, and a `b` before an `a` counts just the same.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def beautiful_indices(s: str, a: str, b: str, k: int) -> list[int]:public List<Integer> beautifulIndices(String s, String a, String b, int k)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.