Find Beautiful Indices in the Given Array I - Problem

You are given a 0-indexed string s, two pattern strings a and b, and an integer k representing the maximum allowed distance.

Your task is to find all "beautiful indices" in the string. An index i is considered beautiful if:

  • The substring starting at index i matches pattern a exactly
  • There exists at least one index j where the substring starting at j matches pattern b exactly
  • The distance between indices i and j is at most k (i.e., |i - j| ≀ k)

Goal: Return all beautiful indices in sorted ascending order.

Example: If s = "isawsquirrelnearmyhouse", a = "saw", b = "house", and k = 15, then index 2 is beautiful because s[2:5] = "saw" matches pattern a, and s[19:24] = "house" matches pattern b, with distance |19 - 2| = 17 ≀ 15 being false, but there might be other valid j positions.

Input & Output

example_1.py β€” Basic Case
$ Input: s = "isawsquirrelnearmyhouse", a = "saw", b = "house", k = 15
β€Ί Output: [2]
πŸ’‘ Note: Pattern 'saw' is found at index 2. Pattern 'house' is found at index 19. Since |19 - 2| = 17 > 15, this doesn't satisfy the distance constraint. However, if there were other 'house' occurrences within distance 15, index 2 would be beautiful.
example_2.py β€” Multiple Matches
$ Input: s = "abcd", a = "a", b = "c", k = 4
β€Ί Output: [0]
πŸ’‘ Note: Pattern 'a' is found at index 0. Pattern 'c' is found at index 2. Since |2 - 0| = 2 ≀ 4, index 0 is beautiful.
example_3.py β€” No Valid Indices
$ Input: s = "a", a = "aa", b = "aaa", k = 1
β€Ί Output: []
πŸ’‘ Note: Neither pattern 'aa' nor pattern 'aaa' can be found in string 's' = 'a', so there are no beautiful indices.

Visualization

Tap to expand
Example: s = "abcdefghijk", a = "cd", b = "gh", k = 4cdi=2ghj=6|6-2| = 4 ≀ k βœ“Beautiful!Index 2
Understanding the Visualization
1
Scan for Pattern A
Find all positions where pattern 'a' appears in the string
2
Scan for Pattern B
Find all positions where pattern 'b' appears in the string
3
Distance Check
For each 'a' position, use binary search to check if any 'b' position is within distance k
4
Collect Results
Gather all 'a' positions that have a nearby 'b' position
Key Takeaway
🎯 Key Insight: Pre-computing all pattern positions and using binary search for range queries transforms an O(n³) problem into an efficient O(n*m + |A|*log|B|) solution

Time & Space Complexity

Time Complexity
⏱️
O(nΒ³)

O(nΒ²) to check all pairs of positions, O(n) for substring comparison in worst case

n
2n
⚠ Quadratic Growth
Space Complexity
O(1)

Only storing the result array, no additional data structures

n
2n
βœ“ Linear Space

Constraints

  • 1 ≀ s.length ≀ 105
  • 1 ≀ a.length, b.length ≀ 10
  • 0 ≀ k ≀ s.length
  • s, a, and b consist only of lowercase English letters
Asked in
Google 35 Amazon 28 Microsoft 22 Meta 18
43.6K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen