Increasing Decreasing String Problem With Solution
💻 coding

Increasing Decreasing String Problem With Solution

2 min read 453 words
2 min read
ShareWhatsAppPost on X
  • 1The algorithm involves repeatedly appending the smallest and largest characters from the string in a specific order until all characters are used.
  • 2The process continues by alternating between picking the smallest and largest characters, ensuring the result is sorted in a unique pattern.
  • 3The time complexity of the solution is O(u*n), where u is the number of unique characters and n is the total number of characters.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"The algorithm involves repeatedly appending the smallest and largest characters from the string in a specific order until all characters are used."

Increasing Decreasing String Problem With Solution

Given a string s. You should re-order the string using the following algorithm:

Pick the smallest character from s and append it to the result.

Pick the smallest character from s which is greater than the last appended character to the result and append it.

Repeat step 2 until you cannot pick more characters.

Pick the largest character from s and append it to the result.

Pick the largest character from s which is smaller than the last appended character to the result and append it.

Repeat step 5 until you cannot pick more characters.

Repeat the steps from 1 to 6 until you pick all characters from s.

In each step, If the smallest or the largest character appears more than once you can choose any occurrence and append it to the result.

Return the result string after sorting s with 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.

Example 3:

Input: s = "leetcode"

Output: "cdelotee"

Example 4:

Input: s = "ggggggg"

Output: "ggggggg"

Example 5:

Input: s = "spo"

Output: "ops"

Constraints:

1 <= s.length <= 500

s contains only lower-case English letters.

Solution:

using System;
using System.Collections.Generic;
using System.Text;

namespace LeetCode.AskGif.Easy.String
{
 class SortStringSoln
 {
 public void execute()
 {
 var res = SortString("aaaabbbbcccc");
 }

 public string SortString(string s)
 {
 var dict = new Dictionary<char, int>();
 var set = new SortedSet<char>();
 var str = new StringBuilder();
 foreach (var c in s)
 {
 if (dict.ContainsKey(c))
 dict[c]++;
 else
 dict[c] = 1;
 set.Add(c);
 }

 var prevStack = new Stack<char>();
 var currentStack = new Stack<char>();
 foreach (var item in set)
 {
 prevStack.Push(item);
 }

 while (prevStack.Count != 0)
 {
 currentStack.Push(prevStack.Pop());
 }

 while (dict.Count != 0)
 {
 while (currentStack.Count != 0)
 {
 var item = currentStack.Pop();
 if (!dict.ContainsKey(item)) continue;
 str.Append(item);
 if (dict[item] == 1)
 {
 dict.Remove(item);
 }
 else
 {
 dict[item]--;
 }
 prevStack.Push(item);
 }
 currentStack = prevStack;
 prevStack = new Stack<char>();
 }

 return str.ToString();
 }

 }
}

Time Complexity: O(u*n) where u will be unique elements in the string and n will be total number of elements. If we consider those unique elements are very limited in numbers then the time complexity of the solution will be O(n).

Space Complexity: O(n) - for dictionary and HashSet.

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 3 May 2020 · 2 min read · 453 words

Part of AskGif Blog · coding

You might also like