Beat 98% user, Unique approach
Intuition\nPYTHON and PYTHON3:\nli=[]\nli1=[]\nli2=[]\n\n1. Firstly li used to store unique characters that occur more than once than li1 stores their first occ
# Intuition\nPYTHON and PYTHON3:\nli=[]\nli1=[]\nli2=[]\n\n1. Firstly li used to store unique characters that occur more than once than li1 stores their first occurrences or index in string, and li2 stores their last occurrences or index in the string respectively.\nfor example take string s = "abcdba" than li stores that characters which occurs more than once i.e li=[a,b] than li1 stores their first occurence or index in string s i.e li1=[0,1] and than li2 stores their last occurence or index in string s i.e li2=[5,4]\n\nfor i in range(len(s)):\n    if s.count(s[i])>1 and s[i] not in li:\n        li.append(s[i])\n        li1.append(i)\n\n2. This loop iterates through the characters of the input string s.\nIf the character occurs more than once and has not been added to the list li, it is added to li, and its index i is added to li1.\n\nfor j in li:\n    for i in range(len(s)-1,-1,-1):\n        if s[i]==j:\n            li2.append(i)\n            break\n\n3. This loop iterates through the unique characters in li.\nFor each character j, it finds the last occurrence index in the string s and adds it to the list li2 we apply break so it can stuck only on last same character.\n\nwe get this from example\ns = "abcdba"\nthan we get\nli=[a,b]\nli1=[0,1]\nli2=[5,4]\n\nnow we get the li2 and li1 so now we can see which character contain more substring in between them.\n\nli3 = []\n        for i in range(len(li1)):\n            li3.append(li2[i] - li1[i] - 1)\n\n4. So, now we just check the diiference between the first and last occurrences of each character and stores the result in the list li3.\ni.e first a is at index 0 and last a is at index 5 so the elements between them are 4 which are "bcdb" so we can calculate it by 5-0-1=4 we have to substract by 1 also so we can get the between\'s elements similarly we check for b its first occurence is at index 1 and last b index is at 4 so the elements between them are 2 which are "cd" it also calculate as 4-1-1=2 and we just stores both result in li3 list.\nnow we check which give the maximum substring as in above case a give 4 elements as substring whereas b gives 2 elements as substring so a is greater here with 4 elements so we check for maximum element in li3.\n\nif len(li) > 0:\n            return max(li3)\n        else:\n            return -1\n\n5. If there are characters that occur more than once (len(li) > 0), it returns the maximum value from the difference calculated in li3 using max(li3).\nIf there are no such characters, it returns -1.\nhere we just check that li contain any characters or not which occurs more than once so than we further proceed for above opertion otherwise we have to return just -1.\n\nC++:\n\nSimilar approach in C++ also:\n\nFirstly we simply calculate the count of the characters using map and filter the characters which occurs more than one times and stores in the vector explain below:\n\n1. Counting Character Occurrences : The code starts by using a map (m) to count the occurrences of each character in the input string s.\n2. Identifying Repeated Characters : It then creates a vector (v1) to store characters that occur more than once.\n\nNow we simply calculated the first occurence index and last occurence index and stores them in different vectors and calculate the difference between both vectors which contain first and last indexes and stores the difference in a vector and find the maximum value in res vector explain below:\n\n3. Finding First and Last Occurrences : Two vectors (v2 and v3) are used to store the indices of the first and last occurrences of each repeated character.\n4. Calculating Distances : The code calculates the difference between the first and last occurrences of each repeated character and stores the results in a vector (res).\n\n5. Finding Maximum difference : It finds the maximum value in the res vector.\n\n6. In last we just check if size of v1 vector which contain the characters which occurs more than one times is greater than 0 means there are repeated characters in string so we can proceed further and if there are no elements in v1 means the string contains all single occurs character so in this case we have not to proceed further and return -1.\n\nReturning Result : If there are repeated characters, it returns the maximum distance. Otherwise, it returns -1.\n\nThanks All Happy coding!\n\n# Approach\ncount in python and map in c++.\n\n# Complexity\n- Time complexity: O(n*m) where n is the length of the string and m is the occurence of characters more than 1 times.\n\n- Space complexity: O(n)\n\n# Code\n```python []\nclass Solution(object):\n    def maxLengthBetweenEqualCharacters(self, s):\n        """\n        :type s: str\n        :rtype: int\n        """\n        li=[]\n        li1=[]\n        li2=[]\n        for i in range(len(s)):\n            if s.count(s[i])>1 and s[i] not in li:\n                li.append(s[i])\n                li1.append(i)\n        for j in li:\n            for i in range(len(s)-1,-1,-1):\n                if s[i]==j:\n                    li2.append(i)\n                    break\n        li3=[]\n        for i in range(len(li1)):\n            li3.append(li2[i]-li1[i]-1)\n        if len(li)>0:\n            return max(li3)\n        else:\n            return -1\n        \n```\n```python3 []\nclass Solution:\n    def maxLengthBetweenEqualCharacters(self, s: str) -> int:\n        li=[]\n        li1=[]\n        li2=[]\n        for i in range(len(s)):\n            if s.count(s[i])>1 and s[i] not in li:\n                li.append(s[i])\n                li1.append(i)\n        for j in li:\n            for i in range(len(s)-1,-1,-1):\n                if s[i]==j:\n                    li2.append(i)\n                    break\n        li3=[]\n        for i in range(len(li1)):\n            li3.append(li2[i]-li1[i]-1)\n        if len(li)>0:\n            return max(li3)\n        else:\n            return -1\n```\n```C++ []\nclass Solution {\npublic:\n    int maxLengthBetweenEqualCharacters(string s) {\n        map <char,int> m;\n        for(int i=0;i<s.length();i++){\n            m[s[i]]++;\n        }\n        vector<char> v1;\n        for(auto i:m){\n            if(i.second>1){\n                v1.push_back(i.first);\n            }\n        }\n        vector<int> v2;\n        vector<int> v3;\n        for(int i=0;i<v1.size();i++){\n            for(int j=0;j<s.length();j++){\n                if(s[j]==v1[i]){\n                    v2.push_back(j);\n                    break;\n                }\n            }\n            for(int k=s.length()-1;k>=0;k--){\n                if(s[k]==v1[i]){\n                    v3.push_back(k);\n                    break;\n                }\n            }\n        }\n        vector<int> res;\n        for(int i=0;i<v2.size();i++){\n            int a=v3[i]-v2[i]-1;\n            res.push_back(a);\n        }\n        int ans=0;\n        for(int i=0;i<res.size();i++){\n            if(res[i]>ans){\n                ans=res[i];\n            }\n        }\n        if(v1.size()>0){\n            return ans;\n        }\n        else{\n            return -1;\n        }\n    }\n};\n```\n\n
['Python', 'C++', 'Python3']