Just array 1 pass O(n)||0ms beats 100%
Intuition\n Describe your first thoughts on how to solve this problem. \nIt needs a container to store the position for the first occurrence of every letter. An
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIt needs a container to store the position for the first occurrence of every letter. An array is enough, no need for hash table (C++ unordered_map, Python dict).\n\nPure C is rare which is also implemented & runs in 0ms beating 100%!\n# Approach\n<!-- Describe your approach to solving the problem. -->\n[Please turn English subtitles if necessary]\n[https://youtu.be/5PWd-fXVRr8?si=XC1W5R8JGDvWGfa0](https://youtu.be/5PWd-fXVRr8?si=XC1W5R8JGDvWGfa0)\n\nIterate the loop & find the max length\n```\nint idx = s[i]-\'a\';\n    if (alpha[idx]!= -1){\n        maxLen= max(maxLen, i-alpha[idx]-1);\n    }\n    else alpha[idx]=i;\n```\n|Method|Elasped Time|speed record|Memory|\n|---|---|---|---|\n|C++ `vector<int>` loop|0ms| 100%|6.78MB|\n|C  array loop| 0ms|100%|6.97MB|\n|Python3 list loop|23ms|99.76%|17.36MB|\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(26)$$\n# Codes of  C & C++ 0ms beat 100%\n```C []\n#pragma GCC optimize("O3", "unroll-loops")\nint maxLengthBetweenEqualCharacters(char* s) {\n    int alpha[26];\n    memset(alpha, -1, sizeof(alpha));\n    int maxLen=-1;\n    for(register int i=0; s[i]!=\'\\0\'; i++){\n        int idx=s[i]-\'a\';\n        int newLen=i-alpha[idx]-1;\n        if (alpha[idx]!=-1 && newLen>maxLen) maxLen=newLen;\n        else if (alpha[idx]==-1) alpha[idx]=i;\n    }\n    return maxLen;\n}\n```\n```C++ []\n\n#pragma GCC optimize("O3", "unroll-loops")\nclass Solution {\npublic:\n    int maxLengthBetweenEqualCharacters(string& s) {\n        vector<int> alpha(26, -1);\n        int n=s.size(), maxLen=-1;\n         for (int i = 0; i < n; i++) {\n            int idx = s[i]-\'a\';\n            if (alpha[idx]!= -1){\n            //    cout<<s[i]<<":"<<maxLen<<endl;\n                maxLen= max(maxLen, i-alpha[idx]-1);\n            }\n            else alpha[idx]=i;\n        } \n        return maxLen;    \n    }\n};\n\nauto init = []()\n{ \n    ios::sync_with_stdio(0);\n    cin.tie(0);\n    cout.tie(0);\n    return \'c\';\n}();\n```\n# Python code with Explanation in comments\n\n```\nclass Solution:\n    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n        n=len(s)\n        alpha=[-1]*26 #initial position for every letter\n        maxLen=-1 \n        for i, c in enumerate(s):\n            if alpha[ord(c)-ord(\'a\')]!=-1: # letter c already found\n                maxLen =max(maxLen, i-alpha[ord(c)-ord(\'a\')]-1)\n            else: # first found for the letter c\n               alpha[ord(c)-ord(\'a\')]=i\n        return maxLen \n        \n```
['Array', 'C', 'C++', 'Python3']