作者:
Furina (芙寧娜)
2025-01-14 03:05:403223. Minimum Length of String After Operations
思路:
如果字串長度小於3直接回傳原字串長度
否則遍歷字串,只要有個數大於3的字母就將個數減2直到小於3,再將全部字母個數加總輸
出
C
int minimumLength(char* s) {
if(strlen(s) < 3) return strlen(s);
int count[200] = {0};
int length = 0;
for(int i = 0; s[i] != '\0'; i++){
count[s[i]]++;
}
for(int i = 95; i <= 122; i++){
while(count[i] >= 3){
count[i] -= 2;
}
length += count[i];
}
return length;
}