To solve this problem, iterate through each index i where substring a occurs and check if a substring b exists within distance k using an optimized search. Binary search over the valid answer space accelerates finding qualifying indices. Using rolling hash or two pointers ensures efficient substring comparison and distance checks.
Problem Statement
Given a 0-indexed string s, a string a, a string b, and an integer k, determine which indices in s are beautiful.
An index i is considered beautiful if the substring starting at i matches a and there exists an index j such that the substring starting at j matches b, with the absolute difference |i - j| not exceeding k. Return the sorted list of all beautiful indices.
Examples
Example 1
Input: s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
Output: [16,33]
There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15. Thus we return [16,33] as the result.
Example 2
Input: s = "abcd", a = "a", b = "a", k = 4
Output: [0]
There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4. Thus we return [0] as the result.
Constraints
- 1 <= k <= s.length <= 105
- 1 <= a.length, b.length <= 10
- s, a, and b contain only lowercase English letters.
Solution Approach
Precompute substring matches
Scan the string s to record all starting indices where substring a and substring b occur. This prepares for efficient distance checks without repeated string comparisons.
Check distances with binary search
For each index i where a occurs, use binary search on the list of b indices to determine if any are within k distance. This leverages the pattern of searching over a valid answer space for speed.
Optimize substring checks
Use rolling hash or string hashing to compare substrings in constant time, reducing unnecessary comparisons and ensuring the solution scales to the maximum s length.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
Time complexity is O(len(s) * log(number_of_b_indices)) using binary search for each a occurrence. Space complexity is O(len(s)) to store indices of a and b occurrences. Rolling hash adds minimal extra space for hashes.
What Interviewers Usually Probe
- Looking for understanding of binary search over the answer space.
- Checks if you notice distance constraints between a and b occurrences.
- Observes whether you optimize substring matching to avoid repeated scanning.
Common Pitfalls or Variants
Common pitfalls
- Not handling overlapping substrings correctly.
- Iterating over all pairs of a and b indices without binary search, causing TLE.
- Ignoring distance k constraint or miscalculating absolute difference.
Follow-up variants
- Find beautiful indices where multiple substrings b can satisfy the distance condition.
- Change the problem to count the number of beautiful indices instead of listing them.
- Extend to 2D grids where 'beautiful' positions depend on nearby matching patterns.
How GhostInterview Helps
- Automatically precomputes indices and applies binary search to detect valid matches.
- Highlights distance violations and overlapping substring edge cases in real time.
- Offers step-by-step rolling hash or two-pointer solutions for efficient substring comparison.
Topic Pages
Related GhostInterview Pages
- LeetCode Interview Copilot - Use GhostInterview as a live solver when you want direct help with LeetCode-style coding questions.
- Coding Interview Assistant - See how GhostInterview supports array, string, linked list, graph, and tree interview workflows.
- How GhostInterview Works - Review the screenshot, reasoning, and answer flow before using the solver in a live interview.
FAQ
What defines a beautiful index in this problem?
A beautiful index i is where substring a starts and there exists a substring b within absolute distance k.
Can I use brute force to find all beautiful indices?
Brute force works for small strings but fails for s up to 10^5; using binary search over b indices is essential.
How does the binary search pattern apply here?
Binary search checks efficiently if a b index exists within distance k from each a index, matching the valid answer space pattern.
Is rolling hash necessary for this problem?
Not strictly, but it optimizes substring comparisons to constant time and avoids repeated scans.
What if multiple b occurrences satisfy the distance k?
Any occurrence within k distance makes the index i beautiful; return i once in the sorted list.
Need direct help with Find Beautiful Indices in the Given Array I instead of spending more time grinding it?
Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Find Beautiful Indices in the Given Array I from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.
Capture the prompt fast instead of rewriting the problem by hand.
Get the solution path, trade-offs, and complexity summary in one pass.
Stay outside captured layers on supported screen-share workflows.
Stay in the same pattern family
Find Beautiful Indices in the Given Array II challenges you to use binary search and string matching techniques.
Open problem page#3291 Minimum Number of Valid Strings to Form Target IUse dynamic programming to split target into the fewest prefixes that match any word prefix, while ruling out dead positions early.
Open problem page#3292 Minimum Number of Valid Strings to Form Target IICompute the minimum number of valid strings from an array needed to construct a given target string efficiently using dynamic programming.
Open problem page