1624. Largest Substring Between Two Equal Characters LeetCode Solution

In this guide, you will get 1624. Largest Substring Between Two Equal Characters LeetCode Solution with the best time and space complexity. The solution to Largest Substring Between Two Equal Characters problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Largest Substring Between Two Equal Characters solution in C++
  4. Largest Substring Between Two Equal Characters solution in Java
  5. Largest Substring Between Two Equal Characters solution in Python
  6. Additional Resources
1624. Largest Substring Between Two Equal Characters LeetCode Solution image

Problem Statement of Largest Substring Between Two Equal Characters

Given a string s, return the length of the longest substring between two equal characters, excluding the two characters. If there is no such substring return -1.
A substring is a contiguous sequence of characters within a string.

Example 1:

Input: s = “aa”
Output: 0
Explanation: The optimal substring here is an empty substring between the two ‘a’s.
Example 2:

Input: s = “abca”
Output: 2
Explanation: The optimal substring here is “bc”.

Example 3:

Input: s = “cbzxy”
Output: -1
Explanation: There are no characters that appear twice in s.

Constraints:

1 <= s.length <= 300
s contains only lowercase English letters.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

1624. Largest Substring Between Two Equal Characters LeetCode Solution in C++

class Solution {
 public:
  int maxLengthBetweenEqualCharacters(string s) {
    int ans = -1;
    vector<int> lastSeen(26, -1);

    for (int i = 0; i < s.length(); ++i) {
      const int c = s[i] - 'a';
      if (lastSeen[c] == -1)
        lastSeen[c] = i;
      else
        ans = max(ans, i - lastSeen[c] - 1);
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

1624. Largest Substring Between Two Equal Characters LeetCode Solution in Java

class Solution {
  public int maxLengthBetweenEqualCharacters(String s) {
    int ans = -1;
    int[] lastSeen = new int[26];
    Arrays.fill(lastSeen, -1);

    for (int i = 0; i < s.length(); ++i) {
      final int c = s.charAt(i) - 'a';
      if (lastSeen[c] == -1)
        lastSeen[c] = i;
      else
        ans = Math.max(ans, i - lastSeen[c] - 1);
    }

    return ans;
  }
}
// code provided by PROGIEZ

1624. Largest Substring Between Two Equal Characters LeetCode Solution in Python

class Solution:
  def maxLengthBetweenEqualCharacters(self, s: str) -> int:
    ans = -1
    lastSeen = {}

    for i, c in enumerate(s):
      if c not in lastSeen:
        lastSeen[c] = i
      else:
        ans = max(ans, i - lastSeen[c] - 1)

    return ans
# code by PROGIEZ

Additional Resources

See also  1451. Rearrange Words in a Sentence LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.