LeetCode 2981: Find Longest Special Substring That Occurs Thrice I Solution

Master LeetCode problem 2981 (Find Longest Special Substring That Occurs Thrice I), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2981. Find Longest Special Substring That Occurs Thrice I

Problem Explanation

Explanation:

To solve this problem, we can iterate through the string and keep track of the special substrings that occur at least thrice. We can maintain a hashmap to store the count of each special substring. If a special substring occurs thrice, we update the longest length found so far.

  • We iterate through the string and check if the current character is the same as the previous character.
  • If it is the same, we increase the current special substring length.
  • If it is different, we check if the current special substring length is greater than 2. If it is, we update the count in the hashmap.
  • Finally, we return the longest length found in the hashmap or -1 if no special substring occurs at least thrice.

Time Complexity:

The time complexity of this approach is O(n), where n is the length of the input string.

Space Complexity:

The space complexity of this approach is O(n) to store the hashmap.

:

Solution Code

class Solution {
    public int longestSpecialSubstring(String s) {
        if (s == null || s.length() < 3) {
            return -1;
        }

        int longestLength = -1;
        Map<String, Integer> specialSubstrings = new HashMap<>();

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (i > 0 && s.charAt(i - 1) == c) {
                if (i > 1 && s.charAt(i - 2) == c) {
                    String specialSubstring = String.valueOf(c).repeat(3);
                    specialSubstrings.put(specialSubstring, specialSubstrings.getOrDefault(specialSubstring, 0) + 1);
                    longestLength = Math.max(longestLength, 3);
                }
            }
        }

        return longestLength;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2981 (Find Longest Special Substring That Occurs Thrice I)?

This page provides optimized solutions for LeetCode problem 2981 (Find Longest Special Substring That Occurs Thrice I) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2981 (Find Longest Special Substring That Occurs Thrice I)?

The time complexity for LeetCode 2981 (Find Longest Special Substring That Occurs Thrice I) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2981 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2981 in Java, C++, or Python.

Back to LeetCode Solutions