✅☑Beats 100% Users || [C++/Java/Python/JavaScript] || 3 Approaches || EXPLAINED🔥
PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n\n\n\n\n---\n# Approaches\n(Also explained in the code)\n\n#### Approach 1(Brute Force)\n1. The function initializes a va
# PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n\n![Screenshot 2023-12-31 072215.png](https://assets.leetcode.com/users/images/9bef586b-bc02-449b-956d-a9db734bbadb_1703987555.7337642.png)\n\n\n---\n# Approaches\n(Also explained in the code)\n\n#### ***Approach 1(Brute Force)***\n1. The function initializes a variable `ans` to `-1`. This variable will store the maximum distance found between two equal characters in the string.\n1. It uses two nested loops to iterate through the string characters. The outer loop starts from the first character and moves to the end of the string.\n1. For each character in the string (`s[left]`), the inner loop checks subsequent characters (`s[right]`) to see if any of them match the character at `s[left]`.\n1. If a matching character is found at position `right`, it calculates the distance between the matching characters using (`right - left - 1`) and updates the `ans` variable to store the maximum distance found so far.\n1. The function continues to explore all possible pairs of equal characters, updating `ans` whenever it finds a pair with a greater distance.\n\n# Complexity\n- Time complexity:\n   $$O(n^2)$$\n    \n\n- Space complexity:\n   $$O(1)$$\n    \n\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    int maxLengthBetweenEqualCharacters(string s) {\n        int ans = -1;\n        for (int left = 0; left < s.size(); left++) {\n            for (int right = left + 1; right < s.size(); right++) {\n                if (s[left] == s[right]) {\n                    ans = max(ans, right - left - 1);\n                }\n            }\n        }\n        \n        return ans;\n    }\n};\n\n\n\n\n```\n```Java []\nclass Solution {\n    public int maxLengthBetweenEqualCharacters(String s) {\n        int ans = -1;\n        for (int left = 0; left < s.length(); left++) {\n            for (int right = left + 1; right < s.length(); right++) {\n                if (s.charAt(left) == s.charAt(right)) {\n                    ans = Math.max(ans, right - left - 1);\n                }\n            }\n        }\n        \n        return ans;\n    }\n}\n\n\n```\n```python3 []\n\nclass Solution:\n    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n        ans = -1\n        \n        for left in range(len(s)):\n            for right in range(left + 1, len(s)):\n                if s[left] == s[right]:\n                    ans = max(ans, right - left - 1)\n        \n        return ans\n\n```\n```javascript []\nfunction maxLengthBetweenEqualCharacters(s) {\n    let ans = -1;\n\n    for (let left = 0; left < s.length; left++) {\n        for (let right = left + 1; right < s.length; right++) {\n            if (s[left] === s[right]) {\n                ans = Math.max(ans, right - left - 1);\n            }\n        }\n    }\n\n    return ans;\n}\n\n\n\n```\n---\n#### ***Approach 2(With Maps)***\n\n\n1. The function initializes an unordered map `firstIndex`, where the keys are characters from the string `s`, and the values are the indices (positions) of their first occurrences.\n1. It also initializes `ans` as `-1`, which will store the maximum distance between equal characters found in the string.\n1. The function iterates through each character in the string using a loop.\n1. For each character `s[i]`, it checks if `s[i]` exists in the firstIndex map. If it does:\n    - It calculates the distance between the current index `i` and the index of the first occurrence of `s[i]` stored in `firstIndex[s[i]]`.\n    - It updates `ans` with the maximum of its current value and the calculated distance (`i - firstIndex[s[i]] - 1`).\n1. If the character `s[i]` is not found in the `firstIndex` map, it means it\'s encountered for the first time, so it adds it to the map with its index `i`.\n1. Finally, the function returns the maximum distance (`ans`) found between two equal characters in the input string. If no equal characters are found or there\'s only one occurrence of a character, the function returns `-1`.\n\n# Complexity\n- Time complexity:\n   $$O(n)$$\n    \n\n- Space complexity:\n   $$O(1)$$\n    \n\n\n# Code\n```C++ []\n\nclass Solution {\npublic:\n    int maxLengthBetweenEqualCharacters(string s) {\n        unordered_map<char, int> firstIndex;\n        int ans = -1;\n        \n        for (int i = 0; i < s.size(); i++) {\n            if (firstIndex.find(s[i]) != firstIndex.end()) {\n                ans = max(ans, i - firstIndex[s[i]] - 1);\n            } else {\n                firstIndex[s[i]] = i;\n            }\n        }\n\n        return ans;\n    }\n};\n\n\n\n```\n```Java []\nclass Solution {\n    public int maxLengthBetweenEqualCharacters(String s) {\n        Map<Character, Integer> firstIndex = new HashMap();\n        int ans = -1;\n        \n        for (int i = 0; i < s.length(); i++) {\n            if (firstIndex.containsKey(s.charAt(i))) {\n                ans = Math.max(ans, i - firstIndex.get(s.charAt(i)) - 1);\n            } else {\n                firstIndex.put(s.charAt(i), i);\n            }\n        }\n        \n        return ans;\n    }\n}\n\n\n```\n```python3 []\nclass Solution:\n    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n        first_index = {}\n        ans = -1\n        \n        for i in range(len(s)):\n            if s[i] in first_index:\n                ans = max(ans, i - first_index[s[i]] - 1)\n            else:\n                first_index[s[i]] = i\n\n        return ans\n\n\n```\n```javascript []\nfunction maxLengthBetweenEqualCharacters(s) {\n    const firstIndex = new Map();\n    let ans = -1;\n\n    for (let i = 0; i < s.length; i++) {\n        if (firstIndex.has(s[i])) {\n            ans = Math.max(ans, i - firstIndex.get(s[i]) - 1);\n        } else {\n            firstIndex.set(s[i], i);\n        }\n    }\n\n    return ans;\n}\n\n\n\n```\n---\n\n#### ***Approach 3(Using Array)***\n \n1. It  initializes ans as `-1`, which will store the maximum distance between equal characters found in the string.\n1. The function iterates through each character in the string using a loop.\n1. For each character `s[i]`, it converts the character to an index in the range 0-25 by subtracting `a` from it (`s[i] - \'a\'`). This index is used to access positions in `v1` and `v2` for that specific character.\n1. If the first occurrence of the character is encountered (`v1[temp] == -1`), it records its index `i` in `v1[temp]`.\n1. If a subsequent occurrence of the same character is encountered, it records its index `i` in `v2[temp]` and calculates the distance between the current occurrence and the first occurrence of the character (`v2[temp] - v1[temp] - 1`). It updates `ans` with the maximum of its current value and the calculated distance.\n1. The function continues to iterate through the string, updating the indices in `v1` and `v2` for each character encountered and updating `ans` whenever it finds a pair of equal characters with a greater distance between them.\n\n# Complexity\n- Time complexity:\n   $$O(n)$$\n    \n\n- Space complexity:\n   $$O(1)$$\n    \n\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    int maxLengthBetweenEqualCharacters(string s) {\n        vector<int> v1(26, -1);\n        vector<int> v2(26, -1);\n        int ans = -1;\n\n        for (int i = 0; i < s.size(); ++i) {\n            int temp = s[i] - \'a\';\n\n            if (v1[temp] == -1) {\n                v1[temp] = i;\n            } else {\n                v2[temp] = i;\n                ans = max(ans, v2[temp] - v1[temp] - 1);\n            }\n        }\n\n        return ans;\n    }\n};\n\n\n```\n```Java []\nclass Solution {\n    public int maxLengthBetweenEqualCharacters(String s) {\n        int[] v1 = new int[26];\n        int[] v2 = new int[26];\n        Arrays.fill(v1, -1);\n        Arrays.fill(v2, -1);\n        int ans = -1;\n\n        for (int i = 0; i < s.length(); ++i) {\n            int temp = s.charAt(i) - \'a\';\n\n            if (v1[temp] == -1) {\n                v1[temp] = i;\n            } else {\n                v2[temp] = i;\n                ans = Math.max(ans, v2[temp] - v1[temp] - 1);\n            }\n        }\n\n        return ans;\n    }\n}\n\n\n```\n```python3 []\nclass Solution:\n    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n        v1 = [-1] * 26\n        v2 = [-1] * 26\n        ans = -1\n\n        for i in range(len(s)):\n            temp = ord(s[i]) - ord(\'a\')\n\n            if v1[temp] == -1:\n                v1[temp] = i\n            else:\n                v2[temp] = i\n                ans = max(ans, v2[temp] - v1[temp] - 1)\n\n        return ans\n\n\n\n```\n```javascript []\nfunction maxLengthBetweenEqualCharacters(s) {\n    const v1 = Array(26).fill(-1);\n    const v2 = Array(26).fill(-1);\n    let ans = -1;\n\n    for (let i = 0; i < s.length; ++i) {\n        const temp = s.charCodeAt(i) - \'a\'.charCodeAt(0);\n\n        if (v1[temp] === -1) {\n            v1[temp] = i;\n        } else {\n            v2[temp] = i;\n            ans = Math.max(ans, v2[temp] - v1[temp] - 1);\n        }\n    }\n\n    return ans;\n}\n\n\n```\n---\n\n\n\n# PLEASE UPVOTE IF IT HELPED\n\n---\n---\n\n\n---
['Hash Table', 'String', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']