<jupyter_start><jupyter_text>说明：    给定一个字符串S，检查是否能重新排布其中的字母，使得两相邻的字符不同。    若可行，输出任意可行的结果。若不可行，返回空字符串。Example 1:    Input: S = "aab"    Output: "aba"Example 2:    Input: S = "aaab"    Output: ""Note:    1、S will consist of lowercase letters and have length in range [1, 500].<jupyter_code>from collections import Counter

class Solution:
    def reorganizeString(self, S: str) -> str:
        if len(S) == 1: return S
        s_count = Counter(S)
        s_count = sorted(s_count.items(), key=lambda x: x[1])
        print(s_count)
        return
        res = []
        while s_count:
            idx = len(s_count) - 1
            while res and idx > 0 and s_count[idx][0] != res[-1]:
                idx -= 1
            key, val = s_count[idx]
            if idx == 0 and s_count[idx][0] == res[-1]:
                break
            res.append(key)
            if val == 0:
                s_count.pop(idx)
            s_count = sorted(s_count, key=lambda x: x[1])
            print(res)
        print(res)
import heapq
from collections import Counter

class Solution:
    def reorganizeString(self, S: str) -> str:
        if len(S) == 1: return S
        N = len(S)
        s_count = Counter(S)
        pq = [(-count, char) for char, count in s_count.items()]
        heapq.heapify(pq)
        data = heapq.nlargest(1, pq, key = lambda  item)
        max_occur = -1 * pq[0][0] # 出现最多的元素的数量
        if max_occur > (N + 1) // 2:
            return ""
        
        string = [''] * N
        i = 0
        while pq:
            count, char = heapq.heappop(pq)
            count *= -1
            for j in range(count):
                string[i] = char
                i += 2
                if i >= N:
                    i = 1
        return ''.join(string)
solution = Solution()
solution.reorganizeString('eeeeeeeeeaaaaabcccdddd')
import heapq
a = [('b', 1), ('a', 2)]
heapq.heapify(a) 
data = heapq.nlargest(1, a, key=lambda item: item[1])
print(data)
print(a)<jupyter_output>[('a', 2)]
[('a', 2), ('b', 1)]
{'fraction_non_alphanumeric': 0.13584531482399603, 'fraction_numerical': 0.01834407535944472, 'mean_word_length': 1.814504881450488, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '91724', 'license': 'Apache-2.0', 'n_tokens_mistral': 771, 'n_tokens_neox': 740, 'n_words': 187, 'path': 'String/1014/767. Reorganize String.ipynb', 'repo_name': 'YuHe0108/Leetcode', 'size': '2017'}