Beats 92% --> Java Optimal approach O(N)
\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n    - In first ta
![Screenshot 2024-01-03 125227.png](https://assets.leetcode.com/users/images/1b6e272e-deac-4b5f-9bd1-bb592ad533d7_1704266582.8388393.png)\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n    - In first tarverse store the first index of the character.\n    - In second traverse find the last index and update the distance in arr and in boolean brrr.[]()\n\n    - The max value in arr is your max distance.\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(N)\n# Code\n```\nclass Solution {\n\n     public int maxLengthBetweenEqualCharacters(String s) {\n        int[] arr = new int[26];\n        boolean[] brr = new boolean[26];\n        Arrays.fill(arr,-1);\n        for(int i = 0;i<s.length();i++) {\n            if(arr[s.charAt(i)-\'a\'] == -1) {\n                arr[s.charAt(i)-\'a\'] = i;\n            } \n        }\n        for(int j = s.length()-1;j>=0;j--) {\n            if(!brr[s.charAt(j)-\'a\']) {\n                arr[s.charAt(j)-\'a\'] = j - arr[s.charAt(j)-\'a\'] - 1;\n                brr[s.charAt(j)-\'a\'] = !brr[s.charAt(j)-\'a\'];\n            }\n        }\n        int max = -1;\n        for(int x : arr) max = Math.max(x, max);\n        return max;\n    }\n}\n```
['Java']