|
| 1 | +""" |
| 2 | + Given a string s. You should re-order the string using the following |
| 3 | + algorithm: |
| 4 | + 1. Pick the smallest character from s and append it to the result. |
| 5 | + 2. Pick the smallest character from s which is greater than the last |
| 6 | + appended character to the result and append it. |
| 7 | + 3. Repeat step 2 until you cannot pick more characters. |
| 8 | + 4. Pick the largest character from s and append it to the result. |
| 9 | + 5. Pick the largest character from s which is smaller than the last |
| 10 | + appended character to the result and append it. |
| 11 | + 6. Repeat step 5 until you cannot pick more characters. |
| 12 | + 7. Repeat the steps from 1 to 6 until you pick all characters from s. |
| 13 | +
|
| 14 | + In each step, If the smallest or the largest character appears more than |
| 15 | + once you can choose any occurrence and append it to the result. |
| 16 | +
|
| 17 | + Return the result string after sorting s with this algorithm. |
| 18 | +
|
| 19 | + Example: |
| 20 | + Input: s = "aaaabbbbcccc" |
| 21 | + Output: "abccbaabccba" |
| 22 | + Explanation: After steps 1, 2 and 3 of the first iteration, |
| 23 | + result = "abc" |
| 24 | + After steps 4, 5 and 6 of the first iteration, |
| 25 | + result = "abccba" |
| 26 | + First iteration is done. Now s = "aabbcc" and we go back to |
| 27 | + step 1 |
| 28 | + After steps 1, 2 and 3 of the second iteration, |
| 29 | + result = "abccbaabc" |
| 30 | + After steps 4, 5 and 6 of the second iteration, |
| 31 | + result = "abccbaabccba" |
| 32 | +
|
| 33 | + Example: |
| 34 | + Input: s = "rat" |
| 35 | + Output: "art" |
| 36 | + Explanation: The word "rat" becomes "art" after re-ordering it with the |
| 37 | + mentioned algorithm. |
| 38 | +
|
| 39 | + Example: |
| 40 | + Input: s = "leetcode" |
| 41 | + Output: "cdelotee" |
| 42 | +
|
| 43 | + Example: |
| 44 | + Input: s = "ggggggg" |
| 45 | + Output: "ggggggg" |
| 46 | +
|
| 47 | + Example: |
| 48 | + Input: s = "spo" |
| 49 | + Output: "ops" |
| 50 | +
|
| 51 | + Constraints: |
| 52 | + - 1 <= s.length <= 500 |
| 53 | + - s contains only lower-case English letters. |
| 54 | +""" |
| 55 | +#Difficulty: Easy |
| 56 | +#323 / 323 test cases passed. |
| 57 | +#Runtime: 52 ms |
| 58 | +#Memory Usage: 14 MB |
| 59 | + |
| 60 | +#Runtime: 52 ms, faster than 96.96% of Python3 online submissions for Increasing Decreasing String. |
| 61 | +#Memory Usage: 14 MB, less than 100.00% of Python3 online submissions for Increasing Decreasing String. |
| 62 | + |
| 63 | +class Solution: |
| 64 | + def sortString(self, s: str) -> str: |
| 65 | + result = '' |
| 66 | + letters = [0] * 26 |
| 67 | + chars = set(s) |
| 68 | + for char in chars: |
| 69 | + letters[ord(char) - 97] = s.count(char) |
| 70 | + while True: |
| 71 | + for i in range(26): |
| 72 | + if letters[i]: |
| 73 | + result += chr(i + 97) |
| 74 | + letters[i] -= 1 |
| 75 | + for l in range(25, -1, -1): |
| 76 | + if letters[l]: |
| 77 | + result += chr(l + 97) |
| 78 | + letters[l] -= 1 |
| 79 | + if sum(letters) == 0: |
| 80 | + break |
| 81 | + return result |
0 commit comments