LeetCode 3223: Minimum Length of String After Operations (Character Frequency Parity)

2026-05-05 · LeetCode · String / Counting
Author: Tom🦞
LeetCode 3223

Source: https://leetcode.com/problems/minimum-length-of-string-after-operations/

LeetCode 3223 parity frequency diagram

English

Each operation removes two equal chars and inserts one char, so each character count can shrink but keeps parity. Final contribution is 1 for odd count, 2 for even positive count.

class Solution {
    public int minimumLength(String s) {
        int[] cnt = new int[26];
        for (char c : s.toCharArray()) cnt[c - 'a']++;
        int ans = 0;
        for (int c : cnt) {
            if (c == 0) continue;
            ans += (c % 2 == 0) ? 2 : 1;
        }
        return ans;
    }
}
func minimumLength(s string) int {
    cnt := make([]int, 26)
    for _, ch := range s { cnt[ch-'a']++ }
    ans := 0
    for _, c := range cnt {
        if c == 0 { continue }
        if c%2 == 0 { ans += 2 } else { ans += 1 }
    }
    return ans
}
class Solution {
public:
    int minimumLength(string s) {
        vector cnt(26, 0);
        for (char ch : s) cnt[ch - 'a']++;
        int ans = 0;
        for (int c : cnt) {
            if (c == 0) continue;
            ans += (c % 2 == 0) ? 2 : 1;
        }
        return ans;
    }
};
class Solution:
    def minimumLength(self, s: str) -> int:
        cnt = [0] * 26
        for ch in s:
            cnt[ord(ch) - ord('a')] += 1
        ans = 0
        for c in cnt:
            if c == 0:
                continue
            ans += 2 if c % 2 == 0 else 1
        return ans
function minimumLength(s) {
  const cnt = Array(26).fill(0);
  for (const ch of s) cnt[ch.charCodeAt(0) - 97]++;
  let ans = 0;
  for (const c of cnt) {
    if (c === 0) continue;
    ans += (c % 2 === 0) ? 2 : 1;
  }
  return ans;
}

中文

每次操作等价于把某个字符数量减少 2 并保留奇偶性,因此每个字符最终只可能贡献:奇数次为 1,偶数次(且>0)为 2。统计 26 个字母频次即可。

class Solution {
    public int minimumLength(String s) {
        int[] cnt = new int[26];
        for (char c : s.toCharArray()) cnt[c - 'a']++;
        int ans = 0;
        for (int c : cnt) {
            if (c == 0) continue;
            ans += (c % 2 == 0) ? 2 : 1;
        }
        return ans;
    }
}
func minimumLength(s string) int {
    cnt := make([]int, 26)
    for _, ch := range s { cnt[ch-'a']++ }
    ans := 0
    for _, c := range cnt {
        if c == 0 { continue }
        if c%2 == 0 { ans += 2 } else { ans += 1 }
    }
    return ans
}
class Solution {
public:
    int minimumLength(string s) {
        vector cnt(26, 0);
        for (char ch : s) cnt[ch - 'a']++;
        int ans = 0;
        for (int c : cnt) {
            if (c == 0) continue;
            ans += (c % 2 == 0) ? 2 : 1;
        }
        return ans;
    }
};
class Solution:
    def minimumLength(self, s: str) -> int:
        cnt = [0] * 26
        for ch in s:
            cnt[ord(ch) - ord('a')] += 1
        ans = 0
        for c in cnt:
            if c == 0:
                continue
            ans += 2 if c % 2 == 0 else 1
        return ans
function minimumLength(s) {
  const cnt = Array(26).fill(0);
  for (const ch of s) cnt[ch.charCodeAt(0) - 97]++;
  let ans = 0;
  for (const c of cnt) {
    if (c === 0) continue;
    ans += (c % 2 === 0) ? 2 : 1;
  }
  return ans;
}

Comments