Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.3 kB
2
Indexable
int minStrLen(string str){
    vector<int> cnt(4);
    unsigned long len = str.size();
    
    for(int i = 0; i < len; i++){
        switch (str[i]) {
            case 'A':
                cnt[0]++; break;
            case 'S':
                cnt[1]++; break;
            case 'D':
                cnt[2]++; break;
            case 'F':
                cnt[3]++; break;
        }
    }

    unsigned long meanCnt = len / 4;
    for(int i = 0; i < 4; i++){
        if(cnt[i] <= meanCnt)
            cnt[i] = 0;
        else
            cnt[i] -= meanCnt;
    }

    
    vector<int> curCnt(4);
    int minLen = (int)len;
    
    for(int i = 0; i < len; i++){
        for (int j = i; j < len; j++) {
            int curLen = j - i + 1;
            switch (str[j]) {
                case 'A':
                    curCnt[0]++; break;
                case 'S':
                    curCnt[1]++; break;
                case 'D':
                    curCnt[2]++; break;
                case 'F':
                    curCnt[3]++; break;
            }
            if(curCnt == cnt){
                minLen = min(curLen, minLen);
                break;
            }
        }
        fill(curCnt.begin(), curCnt.end(), 0);;
        
    }
    
    return minLen;
}

int main() {
    string s("ADDFASDF");
    cout << minStrLen(s) << endl;
}