How to find the minimum number of operation(s) to make the string balanced?

后端 未结 5 1617
夕颜
夕颜 2020-12-29 13:27

From Codechef:

A string is considered balanced if and only if all the characters occur in it equal number of times.

You are given a strin

5条回答
  •  时光说笑
    2020-12-29 14:01

    Towards solving this problem ,I think it is also useful to find out all over extra presence( sum of number of elements which present more than one times) of different elements in string

    for ex: in aabbc ,number of elements we have to remove to make presence of each element one is equal to 2 (this is called good string)

    `x=input()
    char=26
    total=0
    lis=[0]*char
    #print(lis)
    
    for i in range(len(x)):
        lis[ord(x[i])-ord('a')]+=1
    #print(lis) 
    
    for i in range(26):
        if(lis[i]>1):
            total=total+(lis[i]-1)
    print(total)        
    

    `

提交回复
热议问题