Ctrl K

Longest Run That Shuffles Into a Palindrome

Hard
You get a string digits made of digits. Find the longest non-empty run of consecutive characters whose digits can be rearranged into a palindrome, a string that reads the same forwards and backwards. Return the length of that run.
Example 1
digits="4747123" → 5

The run "47471" can be rearranged into the palindrome "47174": there are two 4s, two 7s and a single 1 that goes in the middle. Nothing longer works: "474712" already has two digits with an odd count.

Example 2
digits="908" → 1

All the digits differ, so only a single-digit run works.

Example 3
digits="1221" → 4

The whole string is already a palindrome.

Constraints
  • 1 ≤ len(digits) ≤ 10⁵
  • digits contains only the digits 0–9
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWords That Fit Each Puzzle HardSwapped Initials Names HardRecurring Gene Windows MediumShuffle Into a Palindrome EasyThe Sneaked-In Letter Easy
loading Python…
Input
digits="4747123"
Expected
5

Run checks the first 3 tests in your browser. Submit checks all 136 tests on the server, hidden ones included.