[Java] Map solution.
java\nclass Solution {\n    public int maxLengthBetweenEqualCharacters(String s) {\n        int ans = -1;\n        Map<Character, Integer> map = new HashMap<>()
```java\nclass Solution {\n    public int maxLengthBetweenEqualCharacters(String s) {\n        int ans = -1;\n        Map<Character, Integer> map = new HashMap<>();\n        for (int i = 0; i < s.length(); i++) {\n            char ch  = s.charAt(i);\n            if (map.containsKey(ch)) {\n                ans = Math.max(ans, i - 1 - map.get(ch));\n            }\n            else {\n                map.put(ch, i);\n            }\n        }\n        return ans;\n    }\n}\n```
['Java']