Ctrl K

Letters Between Digits

Easy
The string text consists of lowercase English letters and digits. Rearrange its characters so that letters and digits strictly alternate: no two letters and no two digits stand next to each other. To make the answer unique, follow these rules: - the first character is of the kind that occurs more often; if there are as many letters as digits, a letter goes first; - the letters keep the order they had in text, and so do the digits. If no alternating arrangement exists, return an empty string.
Example 1
text="x7y8" → 'x7y8'

Two letters and two digits, an equal split, so we start with a letter: x7y8.

Example 2
text="5ab90" → '5a9b0'

Three digits and two letters, so digits go first: 5a9b0. The digits keep their order (5, 9, 0) and so do the letters (a, b).

Example 3
text="mn3" → 'm3n'

Two letters and one digit give m3n.

Constraints
  • 1 ≤ len(text) ≤ 500
  • text consists of lowercase English letters and digits
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsValue of a Roman Numeral EasyShared Start of Words EasyBracket Balance EasyWhere the Fragment Starts EasyLength of the Final Word Easy
loading Python…
Input
text="x7y8"
Expected
'x7y8'

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