Untitled

 avatar
unknown
c_cpp
5 months ago
396 B
4
Indexable
string lexicographicallySmallestString(string S, int K) {
    unordered_map<char, int> freq;
    for (char c : S) {
        freq[c]++;
    }
    string result = S; 
    for (int i = 0; i < S.size() && K > 0; i++) {
        if (freq[S[i]] > 1) {
            freq[S[i]]--;  
            S.erase(i, 1); 
            i--;           
            K--;    
        }
    }
    return min(result, S);
}

Editor is loading...
Leave a Comment