[C++] Find the First and the Last Index for each Character
Intuition\n Describe your first thoughts on how to solve this problem. \n- Iterate the string s\n- Record the first and the last occurrences for each character 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- Iterate the string `s`\n- Record the first and the last occurrences for each character (`a ~ z`)\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Find the first and the last index for each character\n- Find the longest substring\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    // time/space: O(n)/O(1)\n    int maxLengthBetweenEqualCharacters(string s) {\n        // find the first and the last index for each character\n        vector<int> first(26, INT_MAX);\n        vector<int> last(26, INT_MIN);\n        for (int i = 0; i < s.size(); i++) {\n            int c = s[i] - \'a\';\n            first[c] = min(first[c], i);\n            last[c] = max(last[c], i);\n        }\n\n        // find the longest substring\n        int result = -1;\n        for (int c = 0; c < 26; c++) {\n            if (first[c] == INT_MAX) continue;\n            if (last[c] == INT_MIN) continue;\n            result = max(result, (last[c] - first[c] - 1));\n        }\n        return result;\n    }\n};\n```
['Counting', 'C++']