|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +/** |
| 4 | + * 1370. Increasing Decreasing String |
| 5 | + * |
| 6 | + * Given a string s. You should re-order the string using the following algorithm: |
| 7 | + * Pick the smallest character from s and append it to the result. |
| 8 | + * Pick the smallest character from s which is greater than the last appended character to the result and append it. |
| 9 | + * Repeat step 2 until you cannot pick more characters. |
| 10 | + * Pick the largest character from s and append it to the result. |
| 11 | + * Pick the largest character from s which is smaller than the last appended character to the result and append it. |
| 12 | + * Repeat step 5 until you cannot pick more characters. |
| 13 | + * Repeat the steps from 1 to 6 until you pick all characters from s. |
| 14 | + * 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. |
| 15 | + * Return the result string after sorting s with this algorithm. |
| 16 | + * |
| 17 | + * Example 1: |
| 18 | + * Input: s = "aaaabbbbcccc" |
| 19 | + * Output: "abccbaabccba" |
| 20 | + * Explanation: After steps 1, 2 and 3 of the first iteration, result = "abc" |
| 21 | + * After steps 4, 5 and 6 of the first iteration, result = "abccba" |
| 22 | + * First iteration is done. Now s = "aabbcc" and we go back to step 1 |
| 23 | + * After steps 1, 2 and 3 of the second iteration, result = "abccbaabc" |
| 24 | + * After steps 4, 5 and 6 of the second iteration, result = "abccbaabccba" |
| 25 | + * |
| 26 | + * Example 2: |
| 27 | + * Input: s = "rat" |
| 28 | + * Output: "art" |
| 29 | + * Explanation: The word "rat" becomes "art" after re-ordering it with the mentioned algorithm. |
| 30 | + * |
| 31 | + * Example 3: |
| 32 | + * Input: s = "leetcode" |
| 33 | + * Output: "cdelotee" |
| 34 | + * |
| 35 | + * Example 4: |
| 36 | + * Input: s = "ggggggg" |
| 37 | + * Output: "ggggggg" |
| 38 | + * |
| 39 | + * Example 5: |
| 40 | + * Input: s = "spo" |
| 41 | + * Output: "ops" |
| 42 | + * |
| 43 | + * Constraints: |
| 44 | + * 1 <= s.length <= 500 |
| 45 | + * s contains only lower-case English letters. |
| 46 | + * */ |
| 47 | +public class _1370 { |
| 48 | + public static class Solution1 { |
| 49 | + public String sortString(String s) { |
| 50 | + int[] count = new int[26]; |
| 51 | + for (char c : s.toCharArray()) { |
| 52 | + count[c - 'a']++; |
| 53 | + } |
| 54 | + StringBuilder sb = new StringBuilder(); |
| 55 | + while (sb.length() < s.length()) { |
| 56 | + for (int i = 0; i < count.length; i++) { |
| 57 | + if (count[i] != 0) { |
| 58 | + char character = (char) (i + 'a'); |
| 59 | + sb.append(character); |
| 60 | + count[i]--; |
| 61 | + } |
| 62 | + } |
| 63 | + for (int i = 25; i>= 0; i--) { |
| 64 | + if (count[i] > 0) { |
| 65 | + char character = (char) (i + 'a'); |
| 66 | + sb.append(character); |
| 67 | + count[i]--; |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + return sb.toString(); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments