Posted on

Description

Submission

class Solution {
public:
    int uniqueLetterString(string s) {
        int n = s.length();
        vector<vector<int>> pos(26);

        int ret = 0;
        for(int i = 0; i < n; ++i) {
            pos[s[i] - 'A'].push_back(i);
            
            for(int j = 0; j < 26; ++j) {
                int k = pos[j].size();
                if(k == 1) {
                    ret += pos[j][0] + 1;
                } else if(k > 1) {
                    ret += pos[j][k-1] - pos[j][k-2];
                }
            }
        }

        return ret;
    }
};

// L: 0
// E: 1 2 7
// T: 3
// C: 4
// O: 5
// D: 6


// X X X X X X X X X A [X X X X A] X X X X X i

Leave a Reply

Your email address will not be published. Required fields are marked *