The Indian Engineer

Problem 2981 Find Longest Special Substring That Occurs Thrice I

Posted on 6 mins

Hash-Table String Binary-Search Sliding Window Counting

Problem Statement

Link - Problem 2981

Question

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: 2
Explanation: The longest special substring which occurs thrice is "aa": substrings "aaaa", "aaaa", and "aaaa".
It can be shown that the maximum length achievable is 2.

Example 2:

Input: s = "abcdef"
Output: -1
Explanation: There exists no special substring which occurs at least thrice. Hence return -1.

Example 3:

Input: s = "abcaba"
Output: 1
Explanation: The longest special substring which occurs thrice is "a": substrings "abcaba", "abcaba", and "abcaba".
It can be shown that the maximum length achievable is 1.

Constraints:

Solution

class Solution {
public:
    int maximumLength(string s) {
        map<string,int> freq;
        for(int i = 0; i<s.size(); i++){
            string temp;
            for(int j = i; j<s.size(); j++){
                if(temp.empty() || temp.back() == s[j]){
                    temp.push_back(s[j]);
                    freq[temp]++;
                }
                else
                    break;
            }
        }
        int ans = 0;
        for(auto f:freq){
            if(f.second>=3 && f.first.size() > ans)
                ans = f.first.size();
        }
        if(ans ==0)
            return -1;

        return ans;
    }
};

Complexity Analysis

| Algorithm                            | Time Complexity | Space Complexity |
| ------------------------------------ | --------------- | ---------------- |
| Dynamic Programming with Nested Loop | O(n^2)          | O(n)             |

Explanation

Intial Thoughts

To solve this problem, consider breaking down the string into substrings and checking if each substring is ‘special’. A special substring is one that consists of only a single character. Think of it like grouping similar letters together in a word. Start by generating all possible substrings of the given string. Then, filter out the special substrings by checking if all characters in the substring are the same. After that, count the frequency of each special substring to find the ones that occur at least thrice.

Intuitive Analysis

Imagine sliding a window over the string to generate substrings. Once you have all the substrings, use a frequency counter to keep track of how many times each special substring appears. Picture a dictionary where the keys are the substrings and the values are their frequencies. By iterating through this dictionary, you can find the longest special substring that occurs at least three times. If no such substring exists, return -1. This process involves systematically generating substrings, checking for special ones, counting their occurrences, and identifying the longest one that meets the criteria.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Proof of correctness

$$ \begin{aligned} & \text{Let } s \text{ be the given string.} \\ & \text{Let } \forall i \in [1, |s|] \text{ and } \forall j \in [i, |s|] \text{ be the start and end indices of a substring.} \\ & \text{Let } \text{freq} \text{ be a hashmap to store the frequency of each substring.} \\ & \text{For each substring, we check if it is special by comparing the current character with the previous character.} \\ & \text{If the substring is special, we increment its frequency in the hashmap.} \\ & \text{After generating all substrings, we iterate over the hashmap to find the longest special substring that occurs at least thrice.} \\ & \text{If such a substring is found, we update the answer with its length.} \\ & \text{Finally, we return the length of the longest special substring that occurs at least thrice, or -1 if no such substring is found.} \\ \end{aligned} $$

Footnote

This question is rated as Medium difficulty.

Hints

The constraints are small.

Brute force checking all substrings.


Similar Questions:

Title URL Difficulty
Longest Substring Without Repeating Characters https://leetcode.com/problems/longest-substring-without-repeating-characters Medium
Longest Substring with At Least K Repeating Characters https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters Medium

$$

$$

$$ $$