Minimum Length of String After Operations (LeetCode 3223): Solution and Explanation

Medium
Hash Table
String
Counting

You may repeatedly pick a character that has a copy of itself on both sides and delete the nearest copy on each side. Return the shortest length the string can reach.

Intuition

Every operation deletes exactly two copies of one letter and leaves all other letters alone. So letters never interact, and only the count of each letter matters, not the positions.

If a letter has 3 or more copies, the second copy from the left has one copy before it and at least one after it, so a move is always possible. Each move takes 2 off the count, so an odd count falls to 1 and an even count falls to 2.

Approach: Count letters, keep 1 for odd and 2 for even

  1. Count how many times each of the 26 letters appears.
  2. Set ans = 0.
  3. For every letter with a count c > 0, add 2 - c % 2: that is 1 when c is odd and 2 when c is even.
  4. Return ans.

Step-by-step example

s = "abaacbcbb"
  1. Counts: a = 3, b = 4, c = 2.
  2. 'a' has 3 copies. Picking the middle one deletes the other two, so 1 'a' is left.
  3. 'b' has 4 copies. One move deletes 2 of them, leaving 2, and two copies cannot be reduced further.
  4. 'c' has 2 copies, too few for a move, so both stay.
  5. 1 + 2 + 2 = 5.

Code: Python, Java, C++

1class Solution:
2    def minimumLength(self, s: str) -> int:
3        cnt = [0] * 26
4        for ch in s:
5            cnt[ord(ch) - ord('a')] += 1
6        ans = 0
7        for c in cnt:
8            if c > 0:
9                ans += 2 - c % 2
10        return ans
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

One pass counts the characters, then a fixed 26-step loop adds up the remainders.

Space

O(1)

Only 26 counters are stored, whatever the length of the string.

Other approaches

Simulate the operations (time O(n^2), space O(n))

Repeatedly find a character with copies on both sides and delete its neighbors. It gives the same answer but costs O(n^2) or worse, and it is easy to get the nearest-copy search wrong.

Hash map counter (time O(n), space O(1))

Use a dictionary or Counter and sum 2 - c % 2 over its values. Same logic with a little more overhead.

Common mistakes

  • Returning 0 or 1 for an even count. Two copies can never be deleted because neither has the same letter on both sides.
  • Adding 2 for letters that never appear. Only letters with a count above 0 contribute.
  • Thinking the order of moves matters. Each letter shrinks independently, so any order reaches the same length.

Frequently asked questions

Why can a letter with 3 copies always be reduced?

Take the middle copy. It has one copy to its left and one to its right, which is exactly what the operation needs.

Why does an even count stop at 2 and not 0?

Each move needs one copy to stay as the pick, so a move can only happen with 3 or more copies. From 4 you reach 2, and with 2 copies there is no copy with a neighbor on both sides.

Does deleting one letter ever unlock a move for another letter?

No. A move only checks for the same letter on both sides, and other letters do not block or enable that.