✅☑[JAVA / C++] || Simple Easy Solution || Step By Step Explanation 🔥🔥
Approach - 1\n\nIntuition\n---------\n\nThe solution utilizes a HashMap to keep track of the indices of characters as they are encountered in the string. Whenev
# Approach - 1\n\nIntuition\n---------\n\nThe solution utilizes a HashMap to keep track of the indices of characters as they are encountered in the string. Whenever a character is encountered again, the length of the substring between the current index and the previous index of the same character is calculated and compared with the maximum length (`mx`).\n\nApproach\n--------\n\n1.  Initialize `mx` to -1 and create a HashMap (`map`) to store the indices of characters.\n2.  Iterate through the string `s`.\n3.  For each character at index `i`:\n    *   If the character is already in the HashMap (`map`), calculate the length of the substring between the current index and the previous index of the character. Update `mx` if the calculated length is greater.\n    *   If the character is not in the HashMap, add it to the HashMap with its current index.\n4.  Return the final value of `mx`.\n\nComplexity\n---------\n*   Time complexity: O(n), where n is the length of the input string. The algorithm iterates through the string once.\n*   Space complexity: O(n), where n is the length of the input string. The space is used to store indices in the HashMap.\n\n# Code\n``` JAVA []\nclass Solution {\n    public int maxLengthBetweenEqualCharacters(String s) {\n        int mx=-1;\n        HashMap<Character,Integer>map = new HashMap<>();\n\n        for(int i=0;i<s.length();i++){\n            if(map.containsKey(s.charAt(i))){\n                mx=Math.max(mx,(i-map.get(s.charAt(i))));\n            }else{\n                map.put(s.charAt(i),i);\n            }\n        }\n        return mx;\n    }\n}\n```\n``` C++ []\nclass Solution {\npublic:\n    int maxLengthBetweenEqualCharacters(std::string s) {\n        int mx = -1;\n        std::unordered_map<char, int> map;\n\n        for (int i = 0; i < s.length(); i++) {\n            if (map.find(s[i]) != map.end()) {\n                mx = std::max(mx, i - map[s[i]]-1);\n            } else {\n                map[s[i]] = i;\n            }\n        }\n        return mx;\n    }\n};\n```\n# Approach - 2\n### Intuition\n\nThe intuition behind this solution is to iterate through the input string, maintaining two arrays (`firstIndex` and `lastIndex`) to store the indices where each character is first and last encountered. By doing so, we can calculate the length of the substring between these two indices for each character and find the maximum length.\n\n### Approach\n\n1.  Initialize two arrays, `firstIndex` and `lastIndex`, each of size 26 to represent the lowercase English letters. These arrays will store the indices where each character is first and last encountered.\n2.  Initialize a variable `maxLength` to -1, which will be used to keep track of the maximum length of substrings between equal characters.\n3.  Iterate through the input string `s` from left to right.\n4.  For each character at index `i`, calculate the corresponding index in the arrays using `currentChar - \'a\'`.\n5.  If `firstIndex[index]` is 0, it means this is the first occurrence of the character. Set `firstIndex[index]` to `i + 1`.\n6.  If `firstIndex[index]` is not 0, it means we have seen this character before. Set `lastIndex[index]` to `i + 1` and update `maxLength` with the maximum length between equal characters encountered so far (`lastIndex[index] - firstIndex[index] - 1`).\n7.  After iterating through the entire string, return the calculated `maxLength`.\n\n### Complexity\n\n*   Time complexity: O(n)O(n)O(n), where nnn is the length of the input string `s`. We iterate through the string once.\n*   Space complexity: O(1)O(1)O(1) since the size of the arrays (`firstIndex` and `lastIndex`) is constant (26 lowercase English letters).\n# Code\n``` JAVA []\nclass Solution {\n    public int maxLengthBetweenEqualCharacters(String s) {\n        int[] firstIndex = new int[26];\n        int[] lastIndex = new int[26];\n        int maxLength = -1;\n\n        for (int i = 0; i < s.length(); i++) {\n            char currentChar = s.charAt(i);\n            int index = currentChar - \'a\';\n\n            if (firstIndex[index] == 0) {\n                firstIndex[index] = i + 1;\n            } else {\n                lastIndex[index] = i + 1;\n                maxLength = Math.max(maxLength, lastIndex[index] - firstIndex[index] - 1);\n            }\n        }\n\n        return maxLength;\n    }\n}\n```\n``` C++ []\nclass Solution {\npublic:\n    int maxLengthBetweenEqualCharacters(std::string s) {\n        std::vector<int> firstIndex(26, 0);\n        std::vector<int> lastIndex(26, 0);\n        int maxLength = -1;\n\n        for (int i = 0; i < s.length(); i++) {\n            char currentChar = s[i];\n            int index = currentChar - \'a\';\n\n            if (firstIndex[index] == 0) {\n                firstIndex[index] = i + 1;\n            } else {\n                lastIndex[index] = i + 1;\n                maxLength = std::max(maxLength, lastIndex[index] - firstIndex[index] - 1);\n            }\n        }\n\n        return maxLength;\n    }\n};\n```\n![upvote.jpg](https://assets.leetcode.com/users/images/183f06e2-1797-4f73-9059-7a7f2d31c4c2_1703331865.6575596.webp)
['Hash Table', 'Two Pointers', 'String', 'C++', 'Java']