Best Java Solution in Run Time ✅✅
Intuition\n Describe your first thoughts on how to solve this problem. \nAs the string can contain only lower case alphabets make an Array of length 26 \n\n# Ap
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nAs the string can contain only lower case alphabets make an Array of length 26 \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Run a loop to read char of the string and store index of each alphabet at their first occurance in the Array.\n2. On second occurance of any char subtract it\'s index by first index of the same.\n3. Compare the value with max which has default value -1.\n4. if the value is greator than max update max value with the same.\n5. Return max.\n6. Do Upvote if you got it. Thanks\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1)\n\n# Code\n```\nclass Solution {\n    public int maxLengthBetweenEqualCharacters(String s) {\n\n        int n = s.length();\n\n        int arr[] = new int [26];\n        int max = -1;\n\n        for(int i = 0; i < n; i++){\n            int idx = s.charAt(i) - \'a\';\n            if( arr[idx] > 0){\n                max = Math.max(max, i - arr[idx]);\n            }else{\n                arr[idx] = i + 1;\n            }\n        }\n        return max;\n    }\n}\n```
['Java']