Skip to content

Latest commit

 

History

History
29 lines (27 loc) · 710 Bytes

1370.md

File metadata and controls

29 lines (27 loc) · 710 Bytes

1370. Increasing Decreasing String

Solution 1 (time O(n), space O(1))

class Solution(object):
    def sortString(self, s):
        """
        :type s: str
        :rtype: str
        """
        n = len(s)
        cnt = [0] * 26
        for c in s:
            cnt[ord(c) - ord('a')] += 1
        ans = ""
        while n > 0:
            for i in range(26):
                if cnt[i] > 0:
                    ans += chr(ord('a') + i)
                    cnt[i] -= 1
                    n -= 1
            for i in range(25, -1, -1):
                if cnt[i] > 0:
                    ans += chr(ord('a') + i)
                    cnt[i] -= 1
                    n -= 1
        return ans