3223: Minimum-Length-of-String-After-Operations
Medium
table of contents
This question looks a lot more complicated than it actually is.
The trick is that, in string s
, if a character
appears:
(you can easily demonstrate this using an example)
Since there are only 26 letters in the alphabet, we
can keep track of this data in an integer array
length 26
while we iterate through the string s
.
code
class Solution {
public:
int minimumLength(string s) {
int alphabet[26] = {0};
for (char c : s) {
if (alphabet[c-'a'] == 0) {
++alphabet[c-'a'];
} else {
[c-'a'] ^= 3;
alphabet};
}
int ans = 0;
for (int i : alphabet) {
+= i;
ans }
return ans;
}
};