Re: [闲聊] 每日leetcode

楼主: Furina (芙宁娜)   2025-01-14 03:05:40
3223. 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;
}

Links booklink

Contact Us: admin [ a t ] ucptt.com