Increasing Decreasing String - Problem

You are given a string s containing lowercase English letters. Your task is to reorder the characters using a specific zigzag pattern algorithm that creates an interesting wave-like arrangement.

The algorithm works as follows:

  1. Ascending Phase: Start by picking the smallest available character, then keep picking the next smallest character that's larger than the previously picked one, continuing until no such character exists.
  2. Descending Phase: Switch direction and pick the largest available character, then keep picking the next largest character that's smaller than the previously picked one, continuing until no such character exists.
  3. Repeat: Continue alternating between ascending and descending phases until all characters are used.

For example, with s = "aaaabbbbcccc", the algorithm creates: "abccbaabccba" - notice the beautiful zigzag pattern!

Goal: Return the resulting string after applying this reordering algorithm.

Input & Output

example_1.py โ€” Basic Mixed Characters
$ Input: s = "aaaabbbbcccc"
โ€บ Output: "abccbaabccba"
๐Ÿ’ก Note: First round: ascending gives 'abc', descending gives 'cba'. Second round: ascending gives 'abc', descending gives 'cba'. Result: 'abc' + 'cba' + 'abc' + 'cba' = 'abccbaabccba'
example_2.py โ€” Single Character Type
$ Input: s = "rat"
โ€บ Output: "art"
๐Ÿ’ก Note: All characters are different. First round ascending: 'a' (smallest), 'r' (next), 't' (next). No descending phase needed since all are used. Result: 'art'
example_3.py โ€” Edge Case Single Character
$ Input: s = "leetcode"
โ€บ Output: "cdelotee"
๐Ÿ’ก Note: Round 1: ascending 'cde', descending 'o'. Round 2: ascending 'e', descending 'e'. Round 3: ascending 'lt'. Result combines all phases: 'cdelotee'

Visualization

Tap to expand
๐Ÿ”๏ธ Zigzag Mountain Trail FormationInput: "aaaabbbbcccc"aร—4bร—4cร—4abccbaRound 1: abc (up) + cba (down)abccbaRound 2: abc (up) + cba (down)Final Result"abccbaabccba"Perfect zigzag:๐Ÿ“ˆ Up: aโ†’bโ†’c๐Ÿ“‰ Down: cโ†’bโ†’a๐Ÿ”„ Repeat pattern
Understanding the Visualization
1
Count Skills
Count how many hikers of each skill level (a=2, b=2, c=2)
2
Ascending Trail
Pick lowest to highest available: aโ†’bโ†’c
3
Descending Trail
Pick highest to lowest available: cโ†’bโ†’a
4
Repeat Pattern
Continue zigzag until all hikers are arranged
Key Takeaway
๐ŸŽฏ Key Insight: Use frequency counters + bidirectional alphabet traversal to efficiently create the zigzag pattern without repeatedly searching for characters.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

One pass to count frequencies O(n), then O(n) to process all characters with O(1) alphabet traversals

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only need frequency array of size 26 (constant space for lowercase letters)

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค s.length โ‰ค 500
  • s consists of lowercase English letters only
  • Characters can repeat multiple times
Asked in
Google 25 Amazon 15 Microsoft 12 Meta 8
32.4K Views
Medium Frequency
~15 min Avg. Time
985 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen