[LeetCode] 1370. Increasing Decreasing String

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

  1. Pick the smallest character from s and append it to the result.
  2. Pick the smallest character from s which is greater than the last appended character to the result and append it.
  3. Repeat step 2 until you cannot pick more characters.
  4. Pick the largest character from s and append it to the result.
  5. Pick the largest character from s which is smaller than the last appended character to the result and append it.
  6. Repeat step 5 until you cannot pick more characters.
  7. 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.

上升下降字符串。

给你一个字符串 s ,请你根据下面的算法重新构造字符串:

从 s 中选出 最小 的字符,将它 接在 结果字符串的后面。
从 s 剩余字符中选出 最小 的字符,且该字符比上一个添加的字符大,将它 接在 结果字符串后面。
重复步骤 2 ,直到你没法从 s 中选择字符。
从 s 中选出 最大 的字符,将它 接在 结果字符串的后面。
从 s 剩余字符中选出 最大 的字符,且该字符比上一个添加的字符小,将它 接在 结果字符串后面。
重复步骤 5 ,直到你没法从 s 中选择字符。
重复步骤 1 到 6 ,直到 s 中所有字符都已经被选过。
在任何一步中,如果最小或者最大字符不止一个 ,你可以选择其中任意一个,并将其添加到结果字符串。

请你返回将 s 中字符重新排序后的 结果字符串 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/increasing-decreasing-string
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题意还算直观,把input字符串重新排列。排列的规则是按字典序先从小到大,然后再从字典序最大的字母开始再往字典序最小的字母走。思路是用一个freq数组统计一下26个小写字母的出现次数,然后按照规则把字母重新拼接成字符串即可。

时间O(n)

空间O(n)

Java实现

 1 class Solution {
 2     public String sortString(String s) {
 3         int len = s.length();
 4         int[] freq = new int[26];
 5         for (int i = 0; i < len; i++) {
 6             freq[s.charAt(i) - 'a']++;
 7         }
 8         StringBuilder sb = new StringBuilder();
 9         int count = 0;
10         while (count < len) {
11             for (int i = 0; i < 26; i++) {
12                 if (freq[i] > 0) {
13                     sb.append((char) (i + 'a'));
14                     freq[i]--;
15                     count++;
16                 }
17             }
18 
19             for (int i = 25; i >= 0; i--) {
20                 if (freq[i] > 0) {
21                     sb.append((char) (i + 'a'));
22                     freq[i]--;
23                     count++;
24                 }
25             }
26         }
27         return sb.toString();
28     }
29 }

JavaScript实现

 1 /**
 2  * @param {string} s
 3  * @return {string}
 4  */
 5 var sortString = function (s) {
 6     let len = s.length;
 7     let freq = new Array(26).fill(0);
 8     for (let i = 0; i < len; i++) {
 9         freq[s.charCodeAt(i) - 97]++;
10     }
11 
12     let res = [];
13     let count = 0;
14     while (count < len) {
15         for (let i = 0; i < 26; i++) {
16             if (freq[i] > 0) {
17                 res.push(String.fromCharCode(i + 97));
18                 freq[i]--;
19                 count++;
20             }
21         }
22 
23         for (let i = 25; i >= 0; i--) {
24             if (freq[i] > 0) {
25                 res.push(String.fromCharCode(i + 97));
26                 freq[i]--;
27                 count++;
28             }
29         }
30     }
31     return res.join('');
32 };

LeetCode 题目总结

原文地址:https://www.cnblogs.com/cnoodle/p/13772753.html