Advertisement

Find Longest Special Substring That Occurs Thrice II - LeetCode 2982 Solution

Find Longest Special Substring That Occurs Thrice II - Complete Solution Guide

Find Longest Special Substring That Occurs Thrice II is LeetCode problem 2982, 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 string s that consists of lowercase English letters. A string is called special if it is made up of only a single character. For example, the string "abc" is not special, whereas the strings "ddd" , "zz" , and "f" are special. Return the length of the longest special substring of s which occurs at least thrice , or -1 if no special substring occurs at least thrice . A substring is a contiguous non-empty sequence of characters within a string. Example 1: Input: s = "aaaa" Output:

Detailed Explanation

The problem asks us to find the length of the longest "special" substring (a substring consisting of only one character repeated) that appears at least three times within a given string `s`. If no such substring exists, we should return -1. A substring must be contiguous and non-empty. The input string `s` consists of lowercase English letters.

Solution Approach

The solution involves first grouping consecutive identical characters together. For each distinct character, we store a list of the lengths of its consecutive occurrences in the string. Then, for each character, we find the three largest lengths among its occurrences (l1, l2, and l3, where l1 >= l2 >= l3). Finally, we calculate the maximum possible length of a special substring that occurs at least three times based on these three lengths. We iterate through all distinct characters and return the maximum length found, or -1 if no suitable special substring is found.

Step-by-Step Algorithm

  1. Step 1: Initialize a data structure (e.g., a hash map or dictionary) to store character groupings and their respective lengths. Keys represent distinct characters present in s, and values represent lists of lengths of consecutive occurrences for that character.
  2. Step 2: Iterate through the input string `s`. For each position, count the length of the current special substring (sequence of identical characters).
  3. Step 3: Store the length of each special substring in the corresponding character group within the hash map.
  4. Step 4: Iterate through the hash map of character groups.
  5. Step 5: For each character group, find the three largest lengths (l1, l2, l3). Finding the top 3 lengths does not require sorting; direct comparison is sufficient.
  6. Step 6: Calculate the potential maximum lengths achievable using these top three lengths. Specifically consider max(l1 - 2, min(l1 - 1, l2), l3).
  7. Step 7: Update the maximum length found so far (ans) with the largest potential maximum length.
  8. Step 8: After processing all characters, return the overall maximum length (ans). If ans remains 0, return -1.
  9. Step 9: Check to see if the answer is still zero, and if so, return -1.

Key Insights

  • Insight 1: The core idea is to group consecutive identical characters and store their lengths. This helps in efficiently counting the occurrences of special substrings.
  • Insight 2: We need to iterate through each character and consider the possible lengths of special substrings formed by that character.
  • Insight 3: The maximum length of a special substring that occurs at least thrice is limited by the lengths of the three longest sequences of the same character. Sorting these sequences for each character is unnecessary; we can find the three largest lengths directly.
  • Insight 4: Consider the cases where you can form substrings with length l1-2, l1-1, and l1. These cases arise from using the longest substring and creating substrings within it. Using the other lengths allows you to create the necessary counts.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Hash Table, String, Binary Search, Sliding Window, Counting.

Companies

Asked at: Rubrik.