java.lang.Object
g1301_1400.s1370_increasing_decreasing_string.Solution

public class Solution extends Object
1370 - Increasing Decreasing String.<p>Easy</p> <p>You are given a string <code>s</code>. Reorder the string using the following algorithm:</p> <ol> <li>Pick the <strong>smallest</strong> character from <code>s</code> and <strong>append</strong> it to the result.</li> <li>Pick the <strong>smallest</strong> character from <code>s</code> which is greater than the last appended character to the result and <strong>append</strong> it.</li> <li>Repeat step 2 until you cannot pick more characters.</li> <li>Pick the <strong>largest</strong> character from <code>s</code> and <strong>append</strong> it to the result.</li> <li>Pick the <strong>largest</strong> character from <code>s</code> which is smaller than the last appended character to the result and <strong>append</strong> it.</li> <li>Repeat step 5 until you cannot pick more characters.</li> <li>Repeat the steps from 1 to 6 until you pick all characters from <code>s</code>.</li> </ol> <p>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.</p> <p>Return <em>the result string after sorting</em> <code>s</code> <em>with this algorithm</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;aaaabbbbcccc&rdquo;</p> <p><strong>Output:</strong> &ldquo;abccbaabccba&rdquo;</p> <p><strong>Explanation:</strong> After steps 1, 2 and 3 of the first iteration, result = &ldquo;abc&rdquo;</p> <p>After steps 4, 5 and 6 of the first iteration, result = &ldquo;abccba&rdquo;</p> <p>First iteration is done. Now s = &ldquo;aabbcc&rdquo; and we go back to step 1</p> <p>After steps 1, 2 and 3 of the second iteration, result = &ldquo;abccbaabc&rdquo;</p> <p>After steps 4, 5 and 6 of the second iteration, result = &ldquo;abccbaabccba&rdquo;</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;rat&rdquo;</p> <p><strong>Output:</strong> &ldquo;art&rdquo;</p> <p><strong>Explanation:</strong> The word &ldquo;rat&rdquo; becomes &ldquo;art&rdquo; after re-ordering it with the mentioned algorithm.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= s.length <= 500</code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details