Leetcode Problem 1624. Largest Substring Between Two Equal Characters

1624. Largest Substring Between Two Equal Characters

Leetcode Solutions

Approach: Hash Map

  1. Initialize a hash map firstIndex to store the first occurrence of each character.
  2. Initialize the answer ans to -1 to represent no valid substring found initially.
  3. Iterate over the indices i of the string s:
    • If s[i] is in firstIndex, calculate the length of the substring as i - firstIndex[s[i]] - 1 and update ans if this length is larger.
    • Otherwise, set firstIndex[s[i]] to i to record the first occurrence of the character.
  4. Return ans as the final result.
UML Thumbnail

Approach: Brute Force

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...