First ➡️ Last |  TC O(n) | SC O(1)
Intuition\n Describe your first thoughts on how to solve this problem. \nWe have to return maximum distance between two equal characters. It will always be from
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n**We have to return maximum distance between two equal characters. It will always be from first occurence of that character to last occurence of that character.**\nWe will keep track of that distance for all distinct chracters \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe will store the indices of characters in a `idx` vector of size 26 and initialize it with -1.\nAlso initialize your `ans` with -1.\n\n**Now start iterating the string.**\n- calculate index of character in `idx` vector by `s[i]-\'a\'`\n- Now check if there is -1 (at the time of initialization) at that index, if yes it means we encountered this character first time,\nnow simply put the index of character in this `idx` vector and this index is first occurence of character in this string.\n- Now lets say we encountered another character that has already been found before.\n    - check the curr length by taking difference:-\n   ` current length = current idx - first occurence of char-1`\n   \n```\n        curr = i-idx[x-\'a\']-1; // curr len , idx vector will give index of first character.\n        //maximize ans:\n        ans = max(curr,ans);\n```\n\n# Complexity\n- Time complexity: **O(n)**\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: **O(1)**\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int maxLengthBetweenEqualCharacters(string s) {\n        vector<int> idx(26,-1);\n        int curr = 0 ;\n        int ans = -1;\n        for(int i =0;i<s.size();i++){\n            char x = s[i];\n            if (idx[x-\'a\']==-1){\n                idx[x-\'a\'] = i;\n            }\n            else{\n                curr = i-idx[x-\'a\']-1;\n                ans = max(curr,ans);\n            }\n        }\n        return ans;\n    }\n}; \n```\n\n---\n\n**If you find this solution helpful, consider giving it an upvote! Your support is appreciated. Keep coding, stay healthy, and may your programming endeavors be both rewarding and enjoyable!**\n\n# Join our WhatsApp Channel\nCheck out the link in comments\n\n\uD83D\uDE80 Elevate your coding skills to new heights! \uD83C\uDF1F Join our exclusive WhatsApp community channel dedicated to daily LeetCode challenges, interview problem-solving, and cutting-edge contest solutions.\n\n\uD83D\uDD0D Explore the world of Algorithms and unravel the secrets of Data Structures with us. \uD83E\uDDE0\uD83D\uDCBB\n\n\uD83C\uDFAF What\'s in for you?\n\nDaily LeetCode problems to sharpen your coding prowess.\nInterview problem discussions to ace your tech interviews.\nContest solutions and optimized approaches for a deeper understanding.\nShare new approaches, optimized solutions, and DSA tricks.\nBuild a strong logic foundation at no cost!\n\uD83E\uDD1D Be part of our coding journey, where we learn and grow together. \uD83D\uDCA1\uD83D\uDCBB
['String', 'C++']