Skip to the code
cf.
+50 XP

#20 One Removal to Even Out Counts

hard

A word game shows a string of lowercase letters s. The player must remove exactly one character, and they win if every letter left in the string then appears the same number of times.

Write canEvenOut(s) that returns true if some single removal makes every remaining letter's count equal, and false otherwise. Letters that no longer appear don't count.

Example 1

Input: s = "aabbccc"
Output: true

Remove one c to get aabbcc: every letter appears twice.

Example 2

Input: s = "aabbcc"
Output: false

Any removal leaves one letter with 1 copy and two letters with 2.

Constraints

  • 2 <= s.length <= 10^5
  • s has only lowercase letters.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).