Largest Substring Between Two Equal Characters - C++ Solution - Beats 100% - TC:O(n), SC:O(1)
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem requires finding the length of the longest substring between two equal char
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem requires finding the length of the longest substring between two equal characters, excluding those two characters. The idea is to keep track of the last occurrence index of each character in the string and update the maximum length accordingly.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Initialize a vector `mp` of size 26 (assuming only lowercase English letters) to store the last occurrence index of each character. Initialize it with -1, indicating that the character has not occurred yet.\n- Initialize a variable `maxd` to store the maximum length of the substring.\n- Iterate through each character of the string.\n    - If the character has not occurred before `(mp[s[i]-\'a\'] == -1)`, update its last occurrence index in the `mp` vector.\n    - If the character has occurred before, update `maxd` with the maximum length between the current index and the last occurrence index of the character. `(i - mp[s[i]-\'a\'] - 1)`.\n- Return the final result stored in `maxd`.\n\n# Complexity\n### Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThe time complexity is `O(n)`, where n is the length of the input string. This is because the algorithm iterates through the string once, and each iteration involves constant-time operations.\n\n### Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe space complexity is `O(1)` because the size of the `mp` vector is constant (26 elements) and does not depend on the input size. Hence, the space complexity is constant and not proportional to the input size.\n\n---\n**Elasped Time:** 0 ms\n**Speed Record:** 100%\n**Memory** 6.59MB\n\n---\n\n\n# Code\n```\nclass Solution {\npublic:\n    int maxLengthBetweenEqualCharacters(string s) {\n        int n  = s.size();\n        vector<int>mp(26,-1);\n        int maxd = -1;\n        for(int i=0;i<n;i++)\n        {\n            mp[s[i]-\'a\']==-1?mp[s[i]-\'a\']=i:maxd=max(maxd,i-mp[s[i]-\'a\']-1); \n        }\n        return maxd;\n    }\n};\n```
['Array', 'Counting', 'C++']