Find Beautiful Indices in the Given Array I
Real-world version: Nearby Marker PositionsThe statement here is our own: the same inputs, outputs and technique.
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:
aappears in the transcript starting at positioni, meanings[i .. i + a.length - 1]equalsa;- there is some position
jwherebappears in the transcript, meanings[j .. j + b.length - 1]equalsb, whose distance fromisatisfiesabs(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.
Examples
Example 1
- Input
- s = "abcabc", a = "abc", b = "bc", k = 1
- Output
- [0, 3]
`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
- Input
- s = "axxxb", a = "a", b = "b", k = 3
- Output
- []
`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
- Input
- s = "bqqqa", a = "a", b = "b", k = 4
- Output
- [4]
`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.
Constraints
- 1 <= k <= s.length <= 10^5
- 1 <= a.length, b.length <= 10
- s, a and b contain only lowercase English letters.
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard 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)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 16 test cases: 16 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Find the Index of the First Occurrence in a StringReal-world version: Locate the Routing TagEasy
- Shortest PalindromeReal-world version: Bead Strand MirrorHard
- Longest Duplicate SubstringReal-world version: Longest Repeated Stretch on a Log LineHard
- Longest Happy PrefixReal-world version: Longest Overlapping BorderHard