Untitled
Anonymous
c_cpp
11/16/2024 12:15 PM
528 B
13
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