3 Approach || Simple Solution || Using Hash ||  C++,Java & Python
\nMethod 3 is an efficient linear solution with hash map optimization,  Time complexity - O(n) and Space complexity- O(1).\n\nMethod 1 -Simple Solution\nTime Co
\n**Method 3 is an efficient linear solution with hash map optimization,  Time complexity - O(n) and Space complexity- O(1).**\n\n`Method 1 -Simple Solution`\n**Time Complexity- O(n^2)**\n**Space Complexity: O(1)**\n\n**C++**\n```\nclass Solution {\npublic:\n    int maxLengthBetweenEqualCharacters(string s) {\n        int maxi=-1;\n        for(int i=0;i<s.size();i++){\n            char c=s[i];\n            for(int j=0;j<s.size();j++){\n                if(c==s[j]){\n                    maxi=max(maxi,j-i-1);\n                }\n            }\n        }\n        return maxi;\n    }\n};\n```\n**Java**\n```\nclass Solution {\n    public int maxLengthBetweenEqualCharacters(String s) {\n        int maxi = -1;\n        for (int i = 0; i < s.length(); i++) {\n            char c = s.charAt(i);\n            for (int j = 0; j < s.length(); j++) {\n                if (c == s.charAt(j)) {\n                    maxi = Math.max(maxi, j - i - 1);\n                }\n            }\n        }\n        return maxi;\n    }\n}\n```\n**Python**\n```\nclass Solution(object):\n    def maxLengthBetweenEqualCharacters(self, s):\n\n        maxi = -1\n        for i in range(len(s)):\n            c = s[i]\n            for j in range(len(s)):\n                if c == s[j]:\n                    maxi = max(maxi, j - i - 1)\n        return maxi\n```\n`Method 2 -Optimized Linear Solution with Character Index Arrays`\n**Time Complexity- O(n)**\n**Space Complexity: O(n)**\n**C++**\n```\nclass Solution {\npublic:\n    int maxLengthBetweenEqualCharacters(string s) {\n        vector<int> f(26, -1);\n        vector<int> l(26, -1);\n        int maxi = -1;\n\n        for (int i = 0; i < s.size(); ++i) {\n            int m = s[i] - \'a\';\n\n            if (f[m] == -1) {\n                f[m] = i;\n            } else {\n                l[m] = i;\n                maxi = max(maxi, l[m] - f[m] - 1);\n            }\n        }\n\n        return maxi;\n    }\n};\n```\n**Java**\n```\nclass Solution {\n    public int maxLengthBetweenEqualCharacters(String s) {\n        int[] firstOccur = new int[26];\n        int[] lastOccur = new int[26];\n        int maxi = -1;\n\n        for (int i = 0; i < s.length(); ++i) {\n            int index = s.charAt(i) - \'a\';\n\n            if (firstOccur[index] == 0) {\n                firstOccur[index] = i + 1;\n            } else {\n                lastOccur[index] = i + 1;\n                maxi = Math.max(maxi, lastOccur[index] - firstOccur[index] - 1);\n            }\n        }\n\n        return maxi;\n    }\n}\n```\n**Python**\n```\nclass Solution(object):\n    def maxLengthBetweenEqualCharacters(self, s):\n\n        firstOccur = [0] * 26\n        lastOccur = [0] * 26\n        maxi = -1\n\n        for i in range(len(s)):\n            index = ord(s[i]) - ord(\'a\')\n\n            if firstOccur[index] == 0:\n                firstOccur[index] = i + 1\n            else:\n                lastOccur[index] = i + 1\n                maxi = max(maxi, lastOccur[index] - firstOccur[index] - 1)\n\n        return maxi\n```\n`Method 3 -Efficient Linear Solution with Hash Map Optimization`\n**Time Complexity- O(n)**\n**Space Complexity: O(1)**\n**C++**\n```\nclass Solution {\npublic:\n    int maxLengthBetweenEqualCharacters(string s) {\n    unordered_map<char, int> mp;\n    int maxi = -1;\n\n    for (int i = 0; i < s.length(); ++i) {\n        char c = s[i];\n\n        if (mp.find(c) != mp.end()) {\n            maxi = max(maxi, i - mp[c] - 1);\n        } else {\n            mp[c] = i;\n        }\n    }\n    return maxi;\n    }\n};\n```\n**Java**\n```\nclass Solution {\n    public int maxLengthBetweenEqualCharacters(String s) {\n        HashMap<Character, Integer> mp = new HashMap<>();\n        int maxi = -1;\n\n        for (int i = 0; i < s.length(); ++i) {\n            char c = s.charAt(i);\n\n            if (mp.containsKey(c)) {\n                maxi = Math.max(maxi, i - mp.get(c) - 1);\n            } else {\n                mp.put(c, i);\n            }\n        }\n        return maxi;\n    }\n}\n```\n**Python**\n```\nclass Solution(object):\n    def maxLengthBetweenEqualCharacters(self, s):\n        mp = {}\n        maxi = -1\n\n        for i in range(len(s)):\n            c = s[i]\n\n            if c in mp:\n                maxi = max(maxi, i - mp[c] - 1)\n            else:\n                mp[c] = i\n\n        return maxi\n```\n![image](https://assets.leetcode.com/users/images/48a9fd57-cf78-498a-9010-c6e7604618ef_1703981840.4302227.jpeg)\n
['C']