[LeetCode] 1370. Increasing Decreasing String 上升下降字符串


You are given a string s. Reorder the string using the following algorithm:

  1. Remove the smallest character from s and append it to the result.
  2. Remove the smallest character from s that is greater than the last appended character, and append it to the result.
  3. Repeat step 2 until no more characters can be removed.
  4. Remove the largest character from s and append it to the result.
  5. Remove the largest character from s that is smaller than the last appended character, and append it to the result.
  6. Repeat step 5 until no more characters can be removed.
  7. Repeat steps 1 through 6 until all characters from s have been removed.

If the smallest or largest character appears more than once, you may choose any occurrence to append to the result.

Return the resulting string after reordering s using 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.

Constraints:

  • 1 <= s.length <= 500
  • s consists of only lowercase English letters.

这道题说是给了一个字符串s,让采取下面一系列的措施:

  1. 删除s中最小的字符并将其附加到结果中。
  2. 删除比上次附加的字符大的s中最小的字符,并将其附加到结果中。
  3. 重复步骤2,直到不能再删除任何字符。
  4. 删除s中最大的字符并将其附加到结果中。
  5. 删除比上次附加的字符小的s中最大的字符,并将其附加到结果中。
  6. 重复步骤5,直到不能再删除任何字符。
  7. 重复步骤1到6,直到所有字符从s中被删除。

那么实际上就是找每个状态下s中的最大或最小的字符以及其个数,所以就需要统计每个字符出现的次数,而且最好还要能很方便的知道最大最小的字符是什么。一般来说,如果只想统计每个字符出现的个数,会用 HashMap 来做,但是这道题明确说了需要知道最大和最小的字符,则就应该用 TreeMap 或者数组来做。这里用数组来做更方便一点,因为题目中限定了字符串s中只有 26 个小写字母,则用一个大小为 26 的数组来统计每个字母的出现次数即可,而遍历的方向就可以决定取最大或最小的字符。由于总体步骤是要循环执行的,所以最外层要套一个 while 循环,判定条件就是结果 res 的长度不等于原字符串s。

然后从头开始遍历统计字符个数的数组 charCnt,若某个字符个数自减1之后仍大于等于0,则说明该字符存在,并且是当前最大的字符,则将其加入结果 res 中,这样保证了每种字符只会取一个,这样一圈遍历下来步骤1至3就完成了。同理,从末尾往前遍历,若某个字符个数自减1之后仍大于等于0,则说明该字符存在,并且是当前最小的字符,则将其加入结果 res 中,这样保证了每种字符只会取一个,这样一圈遍历下来步骤4至6就完成了。同时最外层的循环保证了步骤1至6可以重复执行,最终循环退出后就得到符合要求的结果 res,参见代码如下:


class Solution {
public:
    string sortString(string s) {
        string res;
        vector<int> charCnt(26);
        for (char c : s) {
            ++charCnt[c - 'a'];
        }
        while (s.size() != res.size()) {
            for (int i = 0; i < 26; ++i) {
                if (--charCnt[i] >= 0) {
                    res += (i + 'a');
                }
            }
            for (int i = 25; i >= 0; --i) {
                if (--charCnt[i] >= 0) {
                    res += (i + 'a');
                }
            }
        }
        return res;
    }
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/1370


参考资料:

https://leetcode.com/problems/increasing-decreasing-string

https://leetcode.com/problems/increasing-decreasing-string/solutions/533002/c-counts/


LeetCode All in One 题目讲解汇总(持续更新中...)

posted @   Grandyang  阅读(37)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2021-01-18 [LeetCode] 1005. Maximize Sum Of Array After K Negations K次取反后最大化的数组和
2021-01-18 [LeetCode] 1004. Max Consecutive Ones III 最大连续1的个数之三
2018-01-18 VMPlayer Ubuntu 16.04 Copy and Paste with Host 主机与宿机之间的复制粘贴
2016-01-18 [LeetCode] Wiggle Sort II 摆动排序之二
2016-01-18 [LeetCode] Odd Even Linked List 奇偶链表
2016-01-18 [LeetCode] Power of Three 判断3的次方数
2016-01-18 [LeetCode] 322. Coin Change 零钱兑换
Fork me on GitHub

喜欢请打赏

扫描二维码打赏

Venmo 打赏

点击右上角即可分享
微信分享提示