767. Reorganize String

Information

  • Diffculty: Medium

  • Created: 2019-09-14 11:56:26

  • Last Motified: 2019-09-14 11:56:26

Solution

class Solution:
    def reorganizeString(self, S: str) -> str:
        reorg = [None] * len(S)
        letters = [[S.count(c), c] for c in set(S)]
        letters.sort(reverse=True)

        i = 0
        for count, c in letters:
            while count:
                reorg[i] = c
                i += 2
                if i >= len(S):
                    i = 1
                count -= 1
        for i in range(len(reorg) - 1):
            if reorg[i] == reorg[i + 1]:
                return ""
        return "".join(reorg)