✅Beats 100% - Explained - Elegant Solutions - Brute to Optimized - HashMap
\n\n# YouTube Video Explanation:\n\n <a href="https://youtu.be/6L_25hyF14s">https://youtu.be/6L_25hyF14s</a> \n **If you want a video for this question please w
![Screenshot 2023-12-31 063257.png](https://assets.leetcode.com/users/images/441279e2-1659-40c8-8090-9ffcf2006e35_1703984828.179443.png)\n\n# YouTube Video Explanation:\n\n<!-- [https://youtu.be/6L_25hyF14s](https://youtu.be/6L_25hyF14s) -->\n<!-- **If you want a video for this question please write in the comments** -->\n\n**\uD83D\uDD25 Please like, share, and subscribe to support our channel\'s mission of making complex concepts easy to understand.**\n\nSubscribe Link: https://www.youtube.com/@leetlogics/?sub_confirmation=1\n\n*Subscribe Goal: 1100 Subscribers*\n*Current Subscribers: 1053*\n\n---\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem asks for finding the length of the longest substring between two equal characters, excluding those two characters. One way to approach this is to iterate through all possible substrings and check if they are between equal characters.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. We initialize a variable `current` to `s.length() - 1` as the maximum possible length of the substring.\n2. We iterate through all possible substrings, starting with the longest ones. For each substring, we check if it is between two equal characters.\n3. If we find a substring between equal characters, we return its length (`i - 1` as we exclude the two characters).\n4. If no such substring is found, we decrease the `current` length and continue the search.\n5. If we reach the end of the loop without finding a valid substring, we return -1.\n\n# Complexity\n- Time complexity: The time complexity of this solution is `O(n^3)`, where n is the length of the input string `s`. This is because we have nested loops, iterating over all possible substrings.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: The space complexity is `O(1)`, as we only use a constant amount of extra space.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n\n## Brute Approach\n\n```java []\nclass Solution {\n    public int maxLengthBetweenEqualCharacters(String s) {\n        int current = s.length()-1;\n        for (int i=s.length()-1; i>0;i--){\n            for (int j=0; j+i<s.length(); j++){\n                if (s.charAt(j) == s.charAt(j+i)) return i-1;\n            }\n            current--;\n        }\n        return -1;\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    int maxLengthBetweenEqualCharacters(std::string s) {\n        int current = s.length() - 1;\n        for (int i = s.length() - 1; i > 0; i--) {\n            for (int j = 0; j + i < s.length(); j++) {\n                if (s[j] == s[j + i]) {\n                    return i - 1;\n                }\n            }\n            current--;\n        }\n        return -1;\n    }\n};\n```\n```Python []\nclass Solution(object):\n    def maxLengthBetweenEqualCharacters(self, s):\n        current = len(s) - 1\n        for i in range(len(s) - 1, 0, -1):\n            for j in range(len(s) - i):\n                if s[j] == s[j + i]:\n                    return i - 1\n        return -1\n        \n```\n```JavaScript []\nvar maxLengthBetweenEqualCharacters = function(s) {\n    let current = s.length - 1;\n        for (let i = s.length - 1; i > 0; i--) {\n            for (let j = 0; j + i < s.length; j++) {\n                if (s[j] === s[j + i]) {\n                    return i - 1;\n                }\n            }\n            current--;\n        }\n        return -1;\n};\n```\n## Optimized\n```java []\nclass Solution {\n    public int maxLengthBetweenEqualCharacters(String s) {\n        Map<Character, Integer> lastOccurrence = new HashMap<>();\n        int maxLength = -1;\n\n        for (int i = 0; i < s.length(); i++) {\n            char c = s.charAt(i);\n            if (lastOccurrence.containsKey(c)) {\n                maxLength = Math.max(maxLength, i - lastOccurrence.get(c) - 1);\n            } else {\n                lastOccurrence.put(c, i);\n            }\n        }\n\n        return maxLength;\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    int maxLengthBetweenEqualCharacters(std::string s) {\n        unordered_map<char, int> lastOccurrence;\n        int maxLength = -1;\n\n        for (int i = 0; i < s.length(); i++) {\n            char c = s[i];\n            if (lastOccurrence.count(c)) {\n                maxLength = max(maxLength, i - lastOccurrence[c] - 1);\n            } else {\n                lastOccurrence[c] = i;\n            }\n        }\n\n        return maxLength;\n    }\n};\n```\n```Python []\nclass Solution(object):\n    def maxLengthBetweenEqualCharacters(self, s):\n        last_occurrence = {}\n        max_length = -1\n\n        for i, char in enumerate(s):\n            if char in last_occurrence:\n                max_length = max(max_length, i - last_occurrence[char] - 1)\n            else:\n                last_occurrence[char] = i\n\n        return max_length\n        \n```\n```JavaScript []\n/**\n * @param {string} s\n * @return {number}\n */\nvar maxLengthBetweenEqualCharacters = function(s) {\n    const lastOccurrence = {};\n    let maxLength = -1;\n\n    for (let i = 0; i < s.length; i++) {\n        const char = s[i];\n        if (char in lastOccurrence) {\n            maxLength = Math.max(maxLength, i - lastOccurrence[char] - 1);\n        } else {\n            lastOccurrence[char] = i;\n        }\n    }\n\n    return maxLength;\n};\n```\n\n![upvote.png](https://assets.leetcode.com/users/images/11d7afeb-4a0f-4cb5-803d-506b4f67cff7_1703984802.5942829.png)\n
['Hash Table', 'String', 'Python', 'C++', 'Java', 'JavaScript']