Java - O(n) Time, and O(1) Space
Approach - \n\n1. Store the first occurrence of each character\n2. On every next occurrence of the character, compute the length of the substring and update the
**Approach** - \n\n1. Store the first occurrence of each character\n2. On every next occurrence of the character, compute the length of the substring and update the max.\n\n**Space** - O(1)\n\n**Time** - O(n)\n\n```\nclass Solution {\n    public int maxLengthBetweenEqualCharacters(String s) {\n        int[] first = new int[26];\n        Arrays.fill(first, -1);\n        int max = -1;\n\n        for (int i = 0; i < s.length(); i++) {\n            char ch = s.charAt(i);\n            if (first[ch - \'a\'] != -1)\n                max = Math.max(i - first[ch - \'a\'] - 1, max);\n            else\n                first[ch - \'a\'] = i;\n        }\n\n        return max;\n    }\n}\n```
[]