1370. Increasing Decreasing String

1370. Increasing Decreasing String
Given a string s. You should re-order the string using the following algorithm:

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

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

Think and method
Today’s problem is listed as a simple problem in Leetcode, but it can be seen that the problem itself is quite complex. Nevertheless, after sorting it out, we can actually summarize it as:

repeatedly traversing forward and backward through a well-arranged array, which can not only achieve the purpose of finding the minimum first and then finding the maximum.

In the previous two weeks, we found that sorting is usually a time-consuming process, but since we only need to deal with the letters in this case, we can easily use the dictionary order to reduce the complexity of sorting. A simple traversal can be done.At the same time, we have also used the method mentioned above, “bucket”, which uses an array of 26 lengths to represent 26 buckets. Each bucket contains one letter, and counts the number of each letter.

In this problem, I also learned to use a new way of string concatenation, which is easy to concatenate using strings.Builder and WriteByte, which greatly reduced the complexity of the actual code in this problem.

Specifically, the algorithm uses the previously set bucket to detect whether the current position of the bucket counts as a value of 0. If not, the current letter is added to the end of the string and the value of counter -1.

Time complexity: O(s) s is the length of string

Space complexity: O(26)

Code:

func sortString(s string) string {
    var result strings.Builder
    var buckets [26]byte

    //An array of length 26 represents 26 buckets
    for _, v := range s{
        buckets[v - 'a'] ++
    }

    length := len(s)
    //every time find buckets[i] > 0 
    //Add the letter corresponding to the bucket to the end of the result string

    for length > 0{
    //from small to big
        for i:= 0; i <= 25;i++{
            if buckets[i] > 0{
                result.WriteByte('a' + byte(i))
                buckets[i] --
                length --
            }
        }
    //from big to small
        for i:= 25; i >= 0;i--{
            if buckets[i] > 0{
                result.WriteByte('a' + byte(i))
                buckets[i] --
                length --
            }
        }
    }
    return result.String()
}

Test:
Example 1:
Input: s = “aaaabbbbcccc”
在这里插入图片描述

Example 2:
Input: s = “rat”
在这里插入图片描述

Example 3:
Input: s = “leetcode”
在这里插入图片描述

Example 4:
Input: s = “ggggggg”
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值