@@ -0,0 +1,890 @@
+# Time:  O(nloga) = O(n), a is the size of alphabet%0A# Space: O(a) = O(1)%0A%0Aclass Solution(object):%0A    def reorganizeString(self, S):%0A        %22%22%22%0A        :type S: str%0A        :rtype: str%0A        %22%22%22%0A        counts = collections.Counter(S)%0A        if any(v %3E (len(S)+1)/2 for k, v in counts.iteritems()):%0A            return %22%22%0A        %0A        result = %5B%5D%0A        max_heap = %5B%5D%0A        for k, v in counts.iteritems():%0A            heapq.heappush(max_heap, (-v, k))%0A        while len(max_heap) %3E 1:%0A            count1, c1 = heapq.heappop(max_heap)%0A            count2, c2 = heapq.heappop(max_heap)%0A            if not result or c1 != result%5B-1%5D:%0A                result.extend(%5Bc1, c2%5D)%0A                if count1+1: heapq.heappush(max_heap, (count1+1, c1))%0A                if count2+1: heapq.heappush(max_heap, (count2+1, c2))%0A        return %22%22.join(result) + (max_heap%5B0%5D%5B1%5D if max_heap else '')%0A

Create reorganized-string.py