leetcode-767. 重构字符串

题目

给定一个字符串S,检查是否能重新排布其中的字母,使得两相邻的字符不同。

若可行,输出任意可行的结果。若不可行,返回空字符串。

示例 1:

输入: S = "aab"
输出: "aba"

示例 2:

输入: S = "aaab"
输出: ""

注意:

S 只包含小写字母并且长度在[1, 500]区间内。

解题思路

Heap

不可行的前提条件,字符串中某个字符的出现次数大于字符串长度的一半了。

可行后,每次排字符时,先用出现次数最大的字符和出现次数第二大的字符交错排列,排完后把剩下的字符放到堆里,继续取出来次数最大的和第二大的交错排。

Time complexity: o ( n log ⁡ n ) o(n\log n) o(nlogn)
Space complexity: o ( n ) o(n) o(n)

Tricky

Use the most frequent letter to fill as many even indexes as it could, and then use other elements to fill the rest of even indexes. By making sure that the most frequent elements are separate because of even indexes, then other elements can’t be adjacent because we put them on even indexes first, then fill the slots.

Time complexity: o ( n ) o(n) o(n)
Space complexity: o ( n ) o(n) o(n)

代码

Heap

class Solution:
    def reorganizeString(self, s: str) -> str:
        letter_cnt = {}
        for i in s:
            letter_cnt[i] = letter_cnt.get(i, 0) + 1
        if max(letter_cnt.values()) > (len(s) + 1) // 2:
            return ''
        res = ''
        heap = list((-cnt, letter) for letter, cnt in letter_cnt.items())
        heapq.heapify(heap)
        while len(heap) >= 2:
            x1, x2 = heapq.heappop(heap), heapq.heappop(heap)
            res += x1[1] + x2[1]
            if x1[0] + 1 != 0:
                heapq.heappush(heap, (x1[0] + 1, x1[1]))
            if x2[0] + 1 != 0:
                heapq.heappush(heap, (x2[0] + 1, x2[1]))
        if len(heap) > 1:
            return ""
        if len(heap) == 1:
            res += heap[0][1]
        return res

Tricky

class Solution:
    def reorganizeString(self, s: str) -> str:
        letter_cnt = {}
        for i in s:
            letter_cnt[i] = letter_cnt.get(i, 0) + 1
        max_l, max_c = '', 0
        for letter, cnter in letter_cnt.items():
            if cnter > max_c:
                max_c = cnter
                max_l = letter
        if max_c > (len(s) + 1) // 2:
            return ''
        res = [''] * len(s)
        put_i = 0
        while max_c > 0 and put_i < len(s):
            res[put_i] = max_l
            max_c -= 1
            put_i += 2
        letter_cnt[max_l] = 0
        possible_keys = list(letter_cnt.keys())
        key_index = 0
        if put_i >= len(res):
            put_i = 1
        while key_index < len(possible_keys):
            while letter_cnt[possible_keys[key_index]] != 0:
                res[put_i] = possible_keys[key_index]
                letter_cnt[possible_keys[key_index]] -= 1
                put_i += 2
                if put_i >= len(res):
                    put_i = 1
            key_index += 1
        return ''.join(res)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值