Problem solution · Java

Find Beautiful Indices in the Given Array II

Find Beautiful Indices in the Given Array II: a Java solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Direct simulation
Source
walkccc LeetCode Solutions
Length
58 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For Find Beautiful Indices in the Given Array II, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 58 lines of Java from the credited upstream file 3008.java.
  • The implementation visibly relies on sequence storage.
  • 5 loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeFind Beautiful Indices in the Given Array II · JavaJava
Use this to learn the idea, then write your own version.
class Solution {  // Same as 3006. Find Beautiful Indices in the Given Array I  public List<Integer> beautifulIndices(String s, String a, String b, int k) {    List<Integer> ans = new ArrayList<>();    List<Integer> indicesA = kmp(s, a);    List<Integer> indicesB = kmp(s, b);    int indicesBIndex = 0; // indicesB' index     for (final int i : indicesA) {      // The constraint is: |j - i| <= k. So, -k <= j - i <= k. So, move      // `indicesBIndex` s.t. j - i >= -k, where j := indicesB[indicesBIndex].      while (indicesBIndex < indicesB.size() && indicesB.get(indicesBIndex) - i < -k)        ++indicesBIndex;      if (indicesBIndex < indicesB.size() && indicesB.get(indicesBIndex) - i <= k)        ans.add(i);    }     return ans;  }   // Returns the starting indices of all occurrences of the pattern in `s`.  private List<Integer> kmp(final String s, final String pattern) {    List<Integer> res = new ArrayList<>();    int[] lps = getLPS(pattern);    int i = 0; // s' index    int j = 0; // pattern's index    while (i < s.length()) {      if (s.charAt(i) == pattern.charAt(j)) {        ++i;        ++j;        if (j == pattern.length()) {          res.add(i - j);          j = lps[j - 1];        }      } else if (j != 0) { // Mismatch after j matches.        // Don't match lps[0..lps[j - 1]] since they will match anyway.        j = lps[j - 1];      } else {        ++i;      }    }    return res;  }   // Returns the lps array, where lps[i] is the length of the longest prefix of  // pattern[0..i] which is also a suffix of this substring.  private int[] getLPS(final String pattern) {    int[] lps = new int[pattern.length()];    for (int i = 1, j = 0; i < pattern.length(); ++i) {      while (j > 0 && pattern.charAt(j) != pattern.charAt(i))        j = lps[j - 1];      if (pattern.charAt(i) == pattern.charAt(j))        lps[i] = ++j;    }    return lps;  }} 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗