Find Beautiful Indices in the Given Array II - Problem

You're tasked with finding beautiful indices in a string - a fascinating pattern matching challenge!

Given a string s, two pattern strings a and b, and an integer k, an index i is considered beautiful if:

  • Pattern Match: The substring starting at index i matches pattern a
  • Proximity Condition: There exists another index j where pattern b appears, and the distance between i and j is at most k

Your goal is to return all beautiful indices in sorted order.

Example: If s = "isawsquirrelsaw", a = "saw", b = "sq", and k = 6, then index 2 is beautiful because "saw" appears at index 2, "sq" appears at index 5, and |2-5| = 3 โ‰ค 6.

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "isawsquirrelsaw", a = "saw", b = "sq", k = 6
โ€บ Output: [2, 11]
๐Ÿ’ก Note: Pattern 'saw' appears at indices 2 and 11. Pattern 'sq' appears at index 5. For index 2: |2-5| = 3 โ‰ค 6, so it's beautiful. For index 11: |11-5| = 6 โ‰ค 6, so it's also beautiful.
example_2.py โ€” No Beautiful Indices
$ Input: s = "abcd", a = "a", b = "a", k = 4
โ€บ Output: [0]
๐Ÿ’ก Note: Pattern 'a' appears only at index 0. Since both patterns are the same, the distance is |0-0| = 0 โ‰ค 4, making index 0 beautiful.
example_3.py โ€” Distance Constraint
$ Input: s = "aba", a = "a", b = "a", k = 1
โ€บ Output: [0, 2]
๐Ÿ’ก Note: Pattern 'a' appears at indices 0 and 2. For index 0: distance to index 2 is |0-2| = 2 > 1, but distance to itself is 0 โ‰ค 1. For index 2: distance to index 0 is 2 > 1, but distance to itself is 0 โ‰ค 1. Both are beautiful.

Visualization

Tap to expand
Beautiful Indices: Pattern Matching with ProximityString: "isawsquirrelsaw"Pattern 'saw' matches:211Position 2Position 11Pattern 'sq' matches:5Position 5|2-5| = 3 โ‰ค 6 โœ“|11-5| = 6 โ‰ค 6 โœ“Beautiful Indices Found!Result: [2, 11]Both indices have pattern 'b' within distance k=6Algorithm Visualization LegendPattern 'a' matches (candidates)Pattern 'b' matches (validators)Proximity connections (โ‰ค k)
Understanding the Visualization
1
Scan for Primary Patterns
Find all occurrences of pattern 'a' in the string - these are potential beautiful indices
2
Locate Secondary Patterns
Find all occurrences of pattern 'b' - these provide the proximity validation
3
Check Proximity Constraint
For each pattern 'a' occurrence, verify if any pattern 'b' is within distance k
4
Collect Beautiful Indices
Indices that satisfy both pattern match and proximity constraints are beautiful
Key Takeaway
๐ŸŽฏ Key Insight: The algorithm transforms a quadratic proximity search into efficient pattern preprocessing plus logarithmic lookups, making it scalable for large inputs while maintaining correctness.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n + m + p)

O(n) pattern finding + O(m + p) two-pointer traversal where m,p are pattern occurrence counts

n
2n
โœ“ Linear Growth
Space Complexity
O(m + p)

Space to store pattern occurrences

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
  • Pattern lengths are much smaller than string length
Asked in
Google 28 Amazon 22 Microsoft 18 Meta 15
28.4K Views
Medium-High Frequency
~25 min Avg. Time
856 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