Beats 100%, explanation with example. Simple Hash map Solution
Approach\n Describe your approach to solving the problem. \n### Initialize Variables:\n\n- max_count: This variable serves as a counter to track the maximum len
# Approach\n<!-- Describe your approach to solving the problem. -->\n### Initialize Variables:\n\n- **max_count:** This variable serves as a counter to track the maximum length of a substring between two equal characters. It gets updated during the iteration if a longer substring is encountered.\n- **mp:** An unordered map (mp) is employed to store the last index of each character in the string. This map is crucial for efficiently determining the distance between two occurrences of the same character.\nIterate Through the String:\n\n**The function traverses the input string character by character.**\n### For each character:\nIf the character is found in mp, it implies a repeating character. The function then compares the current index with the last index of the character stored in mp. This step is essential for calculating the distance between two occurrences of the same character, thus updating max_count accordingly.\nIf the character is not found in mp, its index is stored in the map. This step ensures that the map keeps track of the most recent occurrence of each character.\nCalculate Result:\n\nThe final result is obtained by returning max_count - 1. This subtraction is necessary to exclude the two characters themselves from the length calculation, aligning with the problem requirement of determining the substring length between two equal characters.\n### Example:\nConsider the input string: "abacdabe"\nExecution:\nInitialize max_count to 0 and an empty unordered_map mp.\nStart iterating through the string.\n<table>\n    <tr>\n        <td>Character</td>\n        <td>mp</td>\n        <td>max_count</td>\n        <td>Action</td>\n    </tr>\n    <tr>\n        <td>a</td>\n        <td>&quot;{&#39;a&#39;: 0}&quot;</td>\n        <td>0</td>\n        <td>Store index of &#39;a&#39; (0) in mp.</td>\n    </tr>\n    <tr>\n        <td>b</td>\n        <td>&quot;{&#39;a&#39;: 0, &#39;b&#39;: 1}&quot;</td>\n        <td>0</td>\n        <td>Store index of &#39;b&#39; (1) in mp.</td>\n    </tr>\n    <tr>\n        <td>a</td>\n        <td>&quot;{&#39;a&#39;: 0, &#39;b&#39;: 1}&quot;</td>\n        <td>1</td>\n        <td>Update max_count to 1 (1 - 0).</td>\n    </tr>\n    <tr>\n        <td>c</td>\n        <td>&quot;{&#39;a&#39;: 0, &#39;b&#39;: 1, &#39;c&#39;: 3}&quot;</td>\n        <td>1</td>\n        <td>Store index of &#39;c&#39; (3) in mp.</td>\n    </tr>\n    <tr>\n        <td>d</td>\n        <td>&quot;{&#39;a&#39;: 0, &#39;b&#39;: 1, &#39;c&#39;: 3, &#39;d&#39;: 4}&quot;</td>\n        <td>1</td>\n        <td>Store index of &#39;d&#39; (4) in mp.</td>\n    </tr>\n    <tr>\n        <td>a</td>\n        <td>&quot;{&#39;a&#39;: 0, &#39;b&#39;: 1, &#39;c&#39;: 3, &#39;d&#39;: 4}&quot;</td>\n        <td>3</td>\n        <td>Update max_count to 3 (4 - 1).</td>\n    </tr>\n    <tr>\n        <td>b</td>\n        <td>&quot;{&#39;a&#39;: 0, &#39;b&#39;: 1, &#39;c&#39;: 3, &#39;d&#39;: 4}&quot;</td>\n        <td>2</td>\n        <td>Update max_count to 2 (4 - 2).</td>\n    </tr>\n    <tr>\n        <td>e</td>\n        <td>&quot;{&#39;a&#39;: 0, &#39;b&#39;: 1, &#39;c&#39;: 3, &#39;d&#39;: 4, &#39;e&#39;: 7}&quot;</td>\n        <td>2</td>\n        <td>Store index of &#39;e&#39; (7) in mp.</td>\n    </tr>\n</table>\n\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe function aims to find the length of the longest substring between two equal characters in a given string, excluding those two characters. It maintains a count of the maximum length (max_count) and uses an unordered_map (mp) to store the last index of each character seen in the string. During iteration through the string, if a character repeats, it updates max_count by comparing the current index with the last index stored in the map. If a character is not found, it stores its index in the map. The final result is max_count - 1 to exclude the two characters themselves.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThe time complexity is O(N) as the function processes each character in the input string once within a single for loop. The operations inside the loop, like searching in the unordered_map and updating max_count, are constant time on average.\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe space complexity is O(min(N, K)), where K is the number of distinct characters in the input string. The space required for the unordered_map mp is proportional to the number of distinct characters. In the best case (all characters are the same), the space complexity is O(1). In the worst case (all characters are unique), it is O(N), where N is the length of the input string.\n\n# Code\n```\nstatic auto __ = []()\n{\n    std::ios_base::sync_with_stdio(false);\n    std::cin.tie(nullptr);\n    std::cout.tie(nullptr);\n    return 0;\n}();\nclass Solution {\npublic:\n    int maxLengthBetweenEqualCharacters(string s) {\n        int max_count = 0;\n        unordered_map<char , int> mp;\n        for(int i = 0;i<s.length();i++){\n            if(mp.find(s[i]) != mp.end()){ // s[i] is found\n                max_count = std::max(max_count , i-mp[s[i]] );\n            }\n            else{\n                mp[s[i]] = i;\n            }\n        }\n        return max_count-1;\n    }\n};\n```\n\n# Same Old Shameless Begging For Upvote\npleej
['C++']