String ManipulationSherlock and the Valid String

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the isValid function below.
def isValid(s):
    zd={}
    for c in s:
        if c not in zd.keys():
            zd[c]=1
        else:
            zd[c]+=1 

    a=list(zd.values())
    if len(set(a))==1: #所有字母的次数相同
        result='YES'
    elif len(set(a))==2 and (a.count(max(a))==1) and (max(a)-min(a)==1):
        #存在一个字母出现的次数比其他的多1
        result='YES'
    elif len(set(a))==2 and (min(a)==1) and (a.count(min(a))==1):
        #其他字母的次数相同(>=2),有一个字母只出现1次
        result='YES'
    else:
        result='NO'
    return result



if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    s = input()

    result = isValid(s)

    fptr.write(result + '\n')

    fptr.close()
发布了163 篇原创文章 · 获赞 90 · 访问量 6288

猜你喜欢

转载自blog.csdn.net/weixin_45405128/article/details/104204767