✅Beats 99% || ✅Easy Explanation
Intuition\nStore the first index of every character and update the max substring every time. It is optimal to take the firstIndex of the character.\n\n# Approac
# Intuition\nStore the first index of every character and update the max substring every time. It is optimal to take the firstIndex of the character.\n\n# Approach\n\n**firstIndex[c - \'a\'] means first Occurance of character c.**\n1. Take a array `firstIndex` of size`26` to store the firstIndex where 36 aplhabetic characters are present in string e.g., "aabacbad" , [a=0, b=2, c=4, d=5].\n2. Intialize the `firstIndex` array with `-1`. and take a `max` variable to store the answer.\n3. Use a for loop to iterate over the string.\n4. At each iteration store the character at `i` in string in variable `c`.\n5. Check the `firstIndex` of Character `c `.\n        **--**  If `lastIndex[c - \'a\'] == -1` means `c` is not present in string so for `(<i)`, update the `firstIndex[c - \'a\'] = i`\n    **--** Else calculate the substring size and update the `max = Math.max(max, substring.length)`\n\n# Complexity\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n$$O(26)$$ due to `firstIndex` array.\n\n# Code\n```java []\nclass Solution {\n    public int maxLengthBetweenEqualCharacters(String s) {\n// array to store the first index of the 26 aplhabetic characters\n        int firstIndex[] = new int[26];\n// initialize the firstIndex of each character to -1.\n// initialize a max variable with -1 to store the answer.\n        Arrays.fill(firstIndex, -1);\n        int max = -1;\n        for(int i = 0; i < s.length(); i++) {\n// extract the character at i in variable c.\n            char c = s.charAt(i);\n// if c is presented not presenting in substring means firstIndex[c - \'a\'] == -1, update its firstIndex[c - \'a\'] = i.\n// else calculate the answer and updae the max = Math.max(max, i - firstIndex[c - \'a\'] - 1);\n            if(firstIndex[c - \'a\'] == -1) firstIndex[c - \'a\'] = i;\n            else max = Math.max(max, i - firstIndex[c - \'a\'] - 1);\n        }\n// return max substring.\n        return max;\n    }\n}\n```\n```python []\nclass Solution:\n    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n        first_index = [-1] * 26\n        # initialize a max variable with -1 to store the answer.\n        max_length = -1\n\n        for i in range(len(s)):\n            # extract the character at i in variable c.\n            c = s[i]\n            # if c is not presented in substring (first_index[ord(c) - 97] == -1),\n            # update its first_index[ord(c) - 97] = i.\n            # else calculate the answer and update max_length = max(max_length, i - first_index[ord(c) - ord(\'a\')] - 1)\n            if first_index[ord(c) - 97] == -1:\n                first_index[ord(c) - 97] = i\n            else:\n                max_length = max(max_length, i - first_index[ord(c) - 97] - 1)\n\n        # return max substring\n        return max_length\n```\n```cpp []\nclass Solution {\npublic:\n    int maxLengthBetweenEqualCharacters(std::string s) {\n        // array to store the first index of the 26 alphabetic characters\n        std::vector<int> firstIndex(26, -1);\n        // initialize a max variable with -1 to store the answer.\n        int max = -1;\n\n        for (int i = 0; i < s.length(); i++) {\n            // extract the character at i in variable c.\n            char c = s[i];\n            // if c is not presented in substring (firstIndex[c - \'a\'] == -1),\n            // update its firstIndex[c - \'a\'] = i.\n            // else calculate the answer and update max = std::max(max, i - firstIndex[c - \'a\'] - 1);\n            if (firstIndex[c - \'a\'] == -1)\n                firstIndex[c - \'a\'] = i;\n            else\n                max = std::max(max, i - firstIndex[c - \'a\'] - 1);\n        }\n\n        // return max substring\n        return max;\n    }\n};\n```\n
['Array', 'Hash Table', 'String', 'C++', 'Java', 'Python3']