[LeetCode] Minimize String Length

2716. Minimize String Length

Given a 0-indexed string s, repeatedly perform the following operation any number of times:

  • Choose an index i in the string, and let c be the character in position i. Delete the closest occurrence of c to the left of i (if any) and the closest occurrence of c to the right of i (if any).

Your task is to minimize the length of s by performing the above operation any number of times.

Return an integer denoting the length of the minimized string.

1
2
3
4
5
6
7
8
class Solution {
public:
int minimizedStringLength(string s) {
unordered_set<char> us;
for(auto& ch : s) us.insert(ch);
return us.size();
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/06/04/PS/LeetCode/minimize-string-length/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.