Skip to main content

Increasing Decreasing String - Solution & Explanation

EasyHash TableStringCounting18 min readAsked at: Amazon, Google, Akuna Capital
Practice this problem

Problem Statement

You are given a string s. Reorder the string using the following algorithm:

  1. Remove the smallest character from s and append it to the result.
  2. Remove the smallest character from s that is greater than the last appended character, and append it to the result.
  3. Repeat step 2 until no more characters can be removed.
  4. Remove the largest character from s and append it to the result.
  5. Remove the largest character from s that is smaller than the last appended character, and append it to the result.
  6. Repeat step 5 until no more characters can be removed.
  7. Repeat steps 1 through 6 until all characters from s have been removed.

If the smallest or largest character appears more than once, you may choose any occurrence to append to the result.

Return the resulting string after reordering s using this algorithm.

 

Example 1:

Input: s = "aaaabbbbcccc"
Output: "abccbaabccba"
Explanation: After steps 1, 2 and 3 of the first iteration, result = "abc"
After steps 4, 5 and 6 of the first iteration, result = "abccba"
First iteration is done. Now s = "aabbcc" and we go back to step 1
After steps 1, 2 and 3 of the second iteration, result = "abccbaabc"
After steps 4, 5 and 6 of the second iteration, result = "abccbaabccba"

Example 2:

Input: s = "rat"
Output: "art"
Explanation: The word "rat" becomes "art" after re-ordering it with the mentioned algorithm.

 

Constraints:

  • 1 <= s.length <= 500
  • s consists of only lowercase English letters.

Approach Overview

Problem Overview: Given a string s, repeatedly build a result string by selecting characters in increasing alphabetical order, then decreasing order, and repeat until all characters are used. Each step picks the smallest available character greater than the last chosen one, then reverses direction.

Approach 1: Two Pointers and Sorting (O(n log n) time, O(n) space)

Sort the characters of the string first. Once sorted, simulate the increasing and decreasing passes using two pointers or directional iteration. During the increasing phase, iterate left to right and pick the next unused character that is strictly larger than the previous pick. During the decreasing phase, iterate right to left and apply the same rule. Mark characters as used or remove them from the structure. Sorting dominates the runtime with O(n log n) complexity, while the extra storage for tracking usage gives O(n) space.

This approach is straightforward because sorting already groups characters in alphabetical order. The logic mirrors the problem description closely, making it useful for quick implementation or when explaining the idea during interviews.

Approach 2: Counting Sort / Frequency Array (O(n) time, O(1) space)

Instead of sorting the entire string, count the frequency of each lowercase letter using a fixed array of size 26. This leverages the limited alphabet constraint. First iterate from 'a' to 'z', appending a character if its frequency is still positive and decrementing the count. Then iterate from 'z' back to 'a' and repeat the process. Continue alternating these passes until the result length matches the input length.

The key insight is that the alphabet size is constant. Each pass checks at most 26 characters, so the algorithm runs in linear time relative to the input size. The frequency array requires constant extra memory, resulting in O(1) auxiliary space.

This technique is essentially a specialized counting strategy combined with repeated ordered traversal. It avoids the overhead of full sorting and works particularly well for problems involving limited character sets. The implementation relies heavily on simple array indexing and character arithmetic.

Recommended for interviews: The counting-based approach is the solution most interviewers expect. It shows you recognize the constrained alphabet and apply a frequency array instead of sorting. Mentioning the sorting simulation first demonstrates problem understanding, while implementing the frequency counting strategy highlights algorithmic optimization for string problems.

Approach 1: Counting Sort Approach

This approach uses frequency counting of characters to build the result string. We maintain a count of each character using an array of size 26 (for each letter in the English alphabet). We repeatedly iterate over this count array to construct the result as per the given algorithm.

In this C implementation, we use an array of size 26 to count the occurrences of each character in the string. We perform two loops: one (to append characters from smallest to largest) and another (from largest to smallest). We repeat these steps until the resultant string is constructed.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n) - We make a constant 52 iterations for each character.
Space Complexity: O(1) - Constant space is used for the count array.

Try this approach in the editor →

Approach 2: Two Pointers and Sorting Approach

In this approach, we first sort the string. We then utilize two pointers: one starting from the beginning and the other starting from the end. These two pointers help us alternate between choosing the smallest and largest characters.

This approach involves sorting the string first. We manage two pointers and alter between collecting elements from start and end. This way, we maintain an order that meets the requirements while traversing the characters.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n log n) - due to sorting the string initially.
Space Complexity: O(n) - Extra space for result storage.

Try this approach in the editor →

Approach 3: Counting + Simulation

First, we use a hash table or an array cnt of length 26 to count the number of occurrences of each character in the string s.

Then, we enumerate the letters [a,...,z]. For the current enumerated letter c, if cnt[c] > 0, we append the letter c to the end of the answer string and decrease cnt[c] by one. We repeat this step until cnt[c] = 0. Then we enumerate the letters [z,...,a] in reverse order and perform similar operations. If the length of the answer string equals the length of s, then we have completed all the concatenation operations.

The time complexity is O(n times |\Sigma|), and the space complexity is O(|\Sigma|). Where n is the length of the string s, and \Sigma is the character set. In this problem, the character set is all lowercase letters, so |\Sigma| = 26.

Code

Python

Java

C++

Go

TypeScript

JavaScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Counting Sort Approach

Time Complexity: O(n) - We make a constant 52 iterations for each character.
Space Complexity: O(1) - Constant space is used for the count array.

Two Pointers and Sorting Approach

Time Complexity: O(n log n) - due to sorting the string initially.
Space Complexity: O(n) - Extra space for result storage.

Counting + Simulation—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Two Pointers + SortingO(n log n)O(n)Simple simulation when sorting is acceptable and implementation clarity matters
Counting Sort / Frequency ArrayO(n)O(1)Best choice when characters are limited (26 lowercase letters) and optimal performance is required

Video Solution

LeetCode 1370 | Increasing Decreasing String | Algorithm Explained (Java + Whiteboard) • Xavier Elon • 3,600 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Increasing Decreasing String easy or hard?
Increasing Decreasing String is classified as an Easy problem on LeetCode with an acceptance rate above 75%. The challenge mainly involves recognizing that counting frequencies is more efficient than sorting for a fixed alphabet.
How to solve Increasing Decreasing String in O(n)?
Use a counting array to store the frequency of each lowercase character. Repeatedly iterate from 'a' to 'z' adding available characters, then iterate from 'z' to 'a'. Continue alternating until the output length equals the input string length. Since the alphabet size is constant, the total runtime becomes linear in the size of the string.
Increasing Decreasing String Python or Java solution?
Both Python and Java implementations typically use an integer array of size 26 to count character frequencies. The algorithm repeatedly scans forward and backward through the alphabet while appending characters to a result builder until all counts reach zero.
What is the best approach for Increasing Decreasing String?
The counting sort approach using a frequency array of size 26 is the most efficient solution. It repeatedly scans characters from 'a' to 'z' and then 'z' to 'a', appending available letters. Because the alphabet size is fixed, the algorithm runs in O(n) time with O(1) extra space.
What data structure is used in Increasing Decreasing String?
The optimal solution uses a frequency array (a simple counting hash structure) of size 26 to track occurrences of each character. This structure enables constant-time updates and ordered traversal of characters during the increasing and decreasing passes.
What is the time complexity of Increasing Decreasing String?
The optimal solution runs in O(n) time using a frequency array for the 26 lowercase letters. Each character is processed a constant number of times during forward and backward passes. Space complexity is O(1) because the frequency array size is fixed.
Is Increasing Decreasing String asked at Google, Amazon, or Meta?
Problems of this pattern frequently appear in interviews at large tech companies because they test string manipulation and counting techniques. Variants of frequency-based string reconstruction have appeared in interviews at companies like Amazon and Google.

Ready to solve this problem?

Practice Increasing Decreasing String with our built-in code editor and test cases.

Practice on FleetCode