Leetcode 每日一题
题目链接: 1370. 上升下降字符串
难度: 简单
解题思路: 将字符串转化成字典,并对key关键字进行排序。然后从小到大,从大到小进行拼接。
题解:
class Solution:def sortString(self, s: str) -> str:# 记录每个字符出现的次数support = {
}for example in s:support[example] = support.get(example, 0) + 1words = list(support.keys())words.sort()# 拼接res = ""while words:delete = []for example in words:if support[example] > 0:res += examplesupport[example] -= 1if support[example] == 0:delete.append(example)for item in delete:words.remove(item)# 反向words.reverse()return res