LeetCode 2716: Minimize String Length (Unique Characters Count)
LeetCode 2716StringHash SetToday we solve LeetCode 2716 - Minimize String Length.
Source: https://leetcode.com/problems/minimize-string-length/
English
Problem Summary
You may repeatedly delete one character from any pair of equal characters in string s. Return the minimum possible length after all valid deletions.
Key Insight
Each distinct character can be reduced to exactly one remaining copy, but cannot disappear completely. So the final minimum length equals the number of distinct characters in s.
Brute Force and Limitations
A simulation that repeatedly finds duplicate pairs and deletes one character is unnecessary and inefficient. It adds bookkeeping and can degrade toward quadratic behavior.
Optimal Algorithm Steps
1) Create a set (or 26-boolean array).
2) Scan each character in s and mark it as seen.
3) Return the number of seen characters.
Complexity Analysis
Time: O(n).
Space: O(1) (bounded by 26 lowercase letters).
Common Pitfalls
- Misreading operation as deleting both equal characters.
- Trying greedy deletion simulation instead of counting distinct letters.
- Ignoring that one copy of each distinct letter must remain.
Reference Implementations (Java / Go / C++ / Python / JavaScript)
class Solution {
public int minimizedStringLength(String s) {
boolean[] seen = new boolean[26];
int cnt = 0;
for (int i = 0; i < s.length(); i++) {
int idx = s.charAt(i) - 'a';
if (!seen[idx]) {
seen[idx] = true;
cnt++;
}
}
return cnt;
}
}func minimizedStringLength(s string) int {
seen := make([]bool, 26)
cnt := 0
for _, ch := range s {
idx := ch - 'a'
if !seen[idx] {
seen[idx] = true
cnt++
}
}
return cnt
}class Solution {
public:
int minimizedStringLength(string s) {
vector<bool> seen(26, false);
int cnt = 0;
for (char c : s) {
int idx = c - 'a';
if (!seen[idx]) {
seen[idx] = true;
cnt++;
}
}
return cnt;
}
};class Solution:
def minimizedStringLength(self, s: str) -> int:
return len(set(s))/**
* @param {string} s
* @return {number}
*/
var minimizedStringLength = function(s) {
const seen = new Set();
for (const ch of s) seen.add(ch);
return seen.size;
};中文
题目概述
给定字符串 s,你可以对任意一对相同字符执行操作:删除其中一个字符。求最终能得到的最小字符串长度。
核心思路
每种不同字符最多只能被删到剩 1 个,不能全部删光。因此最小长度正好等于字符串中不同字符的个数。
暴力解法与不足
如果按“不断找重复并删除”去模拟,会有很多无意义操作与状态维护,复杂度也更差。
最优算法步骤
1)准备集合(或 26 位布尔数组)。
2)遍历字符串,把出现过的字符标记为已见。
3)返回已见字符数量。
复杂度分析
时间复杂度:O(n)。
空间复杂度:O(1)(小写字母最多 26 种)。
常见陷阱
- 误以为一次操作会删掉两个相同字符。
- 走模拟删除路线,代码复杂还慢。
- 忽略“每种字符最终至少保留一个”的本质约束。
多语言参考实现(Java / Go / C++ / Python / JavaScript)
class Solution {
public int minimizedStringLength(String s) {
boolean[] seen = new boolean[26];
int cnt = 0;
for (int i = 0; i < s.length(); i++) {
int idx = s.charAt(i) - 'a';
if (!seen[idx]) {
seen[idx] = true;
cnt++;
}
}
return cnt;
}
}func minimizedStringLength(s string) int {
seen := make([]bool, 26)
cnt := 0
for _, ch := range s {
idx := ch - 'a'
if !seen[idx] {
seen[idx] = true
cnt++
}
}
return cnt
}class Solution {
public:
int minimizedStringLength(string s) {
vector<bool> seen(26, false);
int cnt = 0;
for (char c : s) {
int idx = c - 'a';
if (!seen[idx]) {
seen[idx] = true;
cnt++;
}
}
return cnt;
}
};class Solution:
def minimizedStringLength(self, s: str) -> int:
return len(set(s))/**
* @param {string} s
* @return {number}
*/
var minimizedStringLength = function(s) {
const seen = new Set();
for (const ch of s) seen.add(ch);
return seen.size;
};
Comments