Advertisement

Find Beautiful Indices in the Given Array II - LeetCode 3008 Solution

Find Beautiful Indices in the Given Array II - Complete Solution Guide

Find Beautiful Indices in the Given Array II is LeetCode problem 3008, a Hard 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 all "beautiful" indices in a given string `s`. An index `i` is considered beautiful if the substring of `s` starting at `i` matches string `a`, and there exists another index `j` such that the substring of `s` starting at `j` matches 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 provided solutions utilize the Knuth-Morris-Pratt (KMP) algorithm to efficiently find all occurrences of strings `a` and `b` within string `s`. Then, it iterates through the occurrences of `a`, and for each occurrence, it uses a two-pointer technique to check if there exists an occurrence of `b` within the distance `k`. If a matching occurrence of `b` is found within the distance `k`, the index `i` is added to the result.

Step-by-Step Algorithm

  1. Step 1: Implement KMP Search: Implement the KMP algorithm to find all starting indices where string `a` appears in string `s` and store these indices in a list called `indices_a`. Repeat the same process for string `b`, storing the indices in `indices_b`.
  2. Step 2: Handle Empty Cases: If either `indices_a` or `indices_b` is empty, then no beautiful indices exist, and return an empty list.
  3. Step 3: Iterate through 'a' indices and find valid 'b' indices: Iterate through each index `i` in `indices_a`. For each `i`, use a second pointer (`j_ptr`) to find an index `j` in `indices_b` such that `|i - j| <= k`.
  4. Step 4: Two-Pointer Optimization for 'b' index search: Increment `j_ptr` until `indices_b[j_ptr]` is greater than or equal to `i - k`. If `j_ptr` reaches the end of `indices_b` or if `indices_b[j_ptr]` is greater than `i + k`, then there is no suitable `j` in `indices_b` for the current `i`.
  5. Step 5: Check the 'k' distance constraint: If `j_ptr` is within the bounds of `indices_b` and `indices_b[j_ptr]` is less than or equal to `i + k`, then the 'k' distance condition is met, and `i` is a beautiful index. Add it to result.
  6. Step 6: Return Result: After iterating through all indices in `indices_a`, return the sorted list of beautiful indices.

Key Insights

  • Insight 1: Efficient String Matching: The core of the problem involves finding all occurrences of substrings `a` and `b` within `s`. A naive approach of comparing every possible substring would be inefficient, leading to a higher time complexity. Thus KMP Algorithm must be used.
  • Insight 2: Two-Pointer Optimization: After identifying the indices of occurrences of `a` and `b`, the problem requires finding pairs of indices `i` and `j` within a specified distance `k`. A brute-force approach would be inefficient. The efficient approach uses the sorted indices of 'b' to quickly check the 'k' distance constraint.
  • Insight 3: Handling Empty Result: Pay attention to the edge cases where no occurrences of `a` or `b` are found in `s`. In this situation, the function must return an empty list.

Complexity Analysis

Time Complexity: O(n*m + n*l + len(indices_a)*len(indices_b))

Space Complexity: O(n + m + l)

Topics

This problem involves: Two Pointers, String, Binary Search, Rolling Hash, String Matching, Hash Function.

Companies

Asked at: Palantir Technologies, PhonePe.