Ctrl K

Letters Up and Down

Easy
You are given a string word of lowercase English letters. Rearrange its letters as if you were sweeping through the alphabet forward and backward, over and over. - **Upward sweep:** go through the alphabet from a to z, and for every letter you still have left, take exactly one copy into the answer. - **Downward sweep:** the same, but from z down to a. Alternate upward and downward sweeps, starting with an upward one, until every letter of word is in the answer. Return the resulting string.
Example 1
word="bbaacc" → 'abccba'

The first upward pass takes one of each letter: a, b, c. That leaves a, b, c, and the downward pass takes c, b, a. Result: "abccba".

Example 2
word="zebra" → 'aberz'

All letters are distinct, so the very first upward pass takes them all, giving the word in alphabetical order.

Example 3
word="pppq" → 'pqpp'

Up: p, q. Down: p (no q left). Up: p. The result is "pqpp".

Constraints
  • 1 ≤ len(word) ≤ 500
  • word consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsNote From Clipped Letters EasyFirst One-Off Letter EasyWords Heard Only Once EasyWords From Letter Tiles EasyBalloon Signs From Letters Easy
loading Python…
Input
word="bbaacc"
Expected
'abccba'

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