SAY NO TO HASHTABLE || EASIER METHOD|| 3 STEP APP.
Intuition\n Describe your first thoughts on how to solve this problem. \n1. The firstOccurenceOfLetter array efficiently tracks character positions.\n2. The cal
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n1. The firstOccurenceOfLetter array efficiently tracks character positions.\n2. The calculation i - firstOccurenceOfLetter[s.charAt(i) - \'a\'] - 1 accurately measures substring lengths.\n3. The code handles both cases: finding a valid substring or returning -1 if none exists.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. **Track First Occurrences**:\n\nCreate an array firstOccurenceOfLetter to store the first index of each character (using -1 to indicate not seen yet).\nInitialize it with -1 for all characters.\n2. **Iterate and Update**:\n\nIterate through each character in the string s:\nIf its first occurrence hasn\'t been recorded yet, store its index in the firstOccurenceOfLetter array.\nIf it\'s a repeated character, calculate the substring length between its current and first occurrence.\nExclude the two characters themselves, hence i - firstOccurenceOfLetter[s.charAt(i) - \'a\'] - 1.\nUpdate the count variable with the maximum length found so far.\n3. **Return the Result**:\n\nReturn the count variable, which holds the length of the longest substring between equal characters.\nIf no such substring exists, it remains -1.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n O(n) LETS CONNECT ON LINKEDIN , GET THE LINK ON MY PROFILE\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n O(1)\n# Code\n```\nclass Solution {\n    public int maxLengthBetweenEqualCharacters(String s) {\n        // Array to track the first occurrence index of each character (-1 if not seen yet)\n        int[] firstOccurrenceOfLetter = new int[26];\n        Arrays.fill(firstOccurrenceOfLetter, -1);\n\n        // Initialize maximum substring length to -1\n        int maxLength = -1;\n\n        // Iterate through each character in the string\n        for (int i = 0; i < s.length(); i++) {\n            char currentChar = s.charAt(i);\n            int charIndex = currentChar - \'a\';\n\n            // If this is the first occurrence of the character, store its index\n            if (firstOccurrenceOfLetter[charIndex] == -1) {\n                firstOccurrenceOfLetter[charIndex] = i;\n            } else {\n                // If it\'s a repeated character, calculate the substring length between occurrences\n                int substringLength = i - firstOccurrenceOfLetter[charIndex] - 1;\n\n                // Exclude the two characters themselves\n                // Update the maximum length if necessary\n                maxLength = Math.max(maxLength, substringLength);\n            }\n        }\n\n        // Return the maximum length found, or -1 if no valid substring exists\n        return maxLength;\n    }\n}\n```
['String', 'Java']