【LeetCode】767. Reorganize String 解题报告(Python)

【LeetCode】767. Reorganize String 解题报告(Python)

标签(空格分隔): LeetCode

作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.me/


题目地址:https://leetcode.com/problems/reorganize-string/description/

题目描述:

Given a string S, check if the letters can be rearranged so that two characters that are adjacent to each other are not the same.

If possible, output any possible result. If not possible, return the empty string.

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].

题目大意

输入是一个字符串,如果这个字符串重新排列之后能组成一个新字符串使得这个字符串相邻的字符都不相同,那么返回这个新字符串;如果做不到,就返回空字符串。

解题方法

这个题我知道要用Counter,而且也知道要做多次操作,可是我想的是递归,没做出来。。看了书影博客,才做出来的。

方法是:

统计这个字符串每个字符的出现频率,在组成新字符串时,在满足和上一个字符不相同的情况下,优先使用最流行的字符串。每用一个字符都对counter进行更新,直至循环结束。注意如果次数为0要删除,否则counter不会为空。

class Solution(object):
    def reorganizeString(self, S):
        """
        :type S: str
        :rtype: str
        """
        counter = collections.Counter(S)
        ans = "#"
        while counter:
            stop = True
            for item, times in counter.most_common():
                if ans[-1] != item:
                    ans += item
                    counter[item] -= 1
                    if not counter[item]:
                        del counter[item]
                    stop = False
                    break
            if stop: break
        return ans[1:] if len(ans) == len(S) + 1 else ""

日期

2018 年 6 月 13 日 ———— 腾讯赛圆满结束!两个月修得正果哈哈~~

猜你喜欢

转载自blog.csdn.net/fuxuemingzhu/article/details/80680454