Simple code || Find starting and ending index
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nFind the starting and ending index of each character from a-z and store i
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nFind the starting and ending index of each character from a-z and store it in a vector of size 26. Then return maximum value of (end - start - 1);\n\n(Please upvote if you found it helpful)\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int maxLengthBetweenEqualCharacters(string s) {\n\n        vector<int>start(26, -1);\n        vector<int>end(26,-1);\n\n        for(int i=0; i<s.length(); ++i)\n            if(start[s[i] - \'a\'] == -1)  // finding start\n                start[s[i] - \'a\'] = i;\n\n\n        for(int i=s.length()-1; i>=0; --i)\n            if(end[s[i] - \'a\'] == -1).     // finding end\n                end[s[i] - \'a\'] = i;\n            \n        int mx = -1;\n\n        for(int i=0; i<=25; ++i)\n            mx = max(mx, end[i] - start[i]);  // calculating max difference\n\n        if(mx == 0)      // if difference is 0 it implies that character only appear once or it doesnt appear at all\n            return -1;      \n        \n        return --mx;  // -- is added because the number of characters in between is end - start - 1.\n    }\n};\n```
['C++']