Find Beautiful Indices in the Given Array I - Complete Solution Guide
Find Beautiful Indices in the Given Array I is LeetCode problem 3006, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given a 0-indexed string s , a string a , a string b , and an integer k . An index i is beautiful if: 0 <= i <= s.length - a.length s[i..(i + a.length - 1)] == a There exists an index j such that: 0 <= j <= s.length - b.length s[j..(j + b.length - 1)] == b |j - i| <= k Return the array that contains beautiful indices in sorted order from smallest to largest . Example 1: Input: s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15 Output: [16,33] Explanation: There a
Detailed Explanation
The problem asks us to find "beautiful indices" within a string `s`. An index `i` is considered beautiful if the substring of `s` starting at `i` and of length `a` is equal to the string `a`, AND there exists another index `j` such that the substring of `s` starting at `j` and of length `b` is equal to the string `b`, AND the absolute difference between `i` and `j` is less than or equal to `k`. The goal is to return a sorted list of all such beautiful indices.
Solution Approach
The solution first finds all starting indices of string `a` and string `b` within string `s`. Then, for each index `i` where `s[i:i + len(a)] == a`, it checks if there exists an index `j` where `s[j:j + len(b)] == b` and `|j - i| <= k`. The algorithm iterates through the `a` indices and uses a pointer to optimize the search for a valid `b` index within the specified range.
Step-by-Step Algorithm
- Step 1: Find all starting indices of string `a` in `s` and store them in a list called `a_indices`.
- Step 2: Find all starting indices of string `b` in `s` and store them in a list called `b_indices`.
- Step 3: Initialize an empty list called `result` to store the beautiful indices.
- Step 4: Initialize a pointer `ptr_b` to 0, which will be used to iterate through the `b_indices` list.
- Step 5: Iterate through each index `i` in the `a_indices` list.
- Step 6: While `ptr_b` is within the bounds of `b_indices` and `b_indices[ptr_b]` is less than `i - k`, increment `ptr_b`. This effectively skips all `b` indices that are too far to the left of the current `a` index.
- Step 7: If `ptr_b` is still within the bounds of `b_indices` and `b_indices[ptr_b]` is less than or equal to `i + k`, it means we have found a valid `b` index within the required range. Add `i` to the `result` list.
- Step 8: After iterating through all indices in `a_indices`, return the `result` list.
Key Insights
- Insight 1: The problem can be solved by first identifying all possible starting indices for string `a` and string `b` within the given string `s`. This can be done through simple string matching.
- Insight 2: After identifying all potential starting indices, the condition `|j - i| <= k` becomes the central element for determining beautiful indices. Essentially, we need to check for each `a` index if there is a `b` index within the range `[i-k, i+k]`.
- Insight 3: Using two pointers to iterate through the indices of `a` and `b` can help to reduce the number of comparisons, optimizing the time complexity slightly.
Complexity Analysis
Time Complexity: O(n^2)
Space Complexity: O(n)
Topics
This problem involves: Two Pointers, String, Binary Search, Rolling Hash, String Matching, Hash Function.
Companies
Asked at: Palantir Technologies, Samsara.