字节面试中遇到的一道代码题

题目:编写递归算法求*长连续重复的单个字符(如果是找*长重复子串的话,可以参考leetcode 1044: *长重复子串),比如”aaabbcc”的*大重复字串为”aaa”,“aab”*大重复字串为”aa”, 而类似”aba”这种则返回a,b均可,并编写测试用例。

class Solution:
def repeatTimes(self, s):
n = len(s)
if n == 0:
return None

c = {}
for i in range(n):
if s[i] not in c.keys():
c[s[i]] = 1
else:
c[s[i]] += 1
cmax = max(c.values())
for k, v in c.items():
if v == cmax:
res = k
break
return res * cmax

tests = [”, ‘a’, ‘aa’, ‘abc’, ‘aab’, ‘aba’, ‘aabb’, ‘aabbb’, ‘abcdc’]
a = Solution()
for test in tests:
b = a.repeatTimes(test)
print(b)