简体   繁体   中英

Python: Given a string s and an integer C I have to find the summation of difference of number of each alphabet and C

If number of occurence is less than C it should be ignored ie sum of max(number of occurrence - C,0). for example if the string is aabbaacdddd and C is 2 then output should be 4. There are 4 a's, 4 d's, 2 b's so sum of(4-2,4-2,2-2) = 4. There is 1 c but since C > 1 the difference is taken to be zero.

Below is my code.

T = int(input())
for _ in range(0,T):
    N,Q = map(int,input().split())
    s = input()
    #print(ord("a"))
    #print(ord("z"))
    for q in range(0,Q):
        C = int(input())
        m = [0] * 26
        for i in s:
            m[ord(i)-97] = m[ord(i)-97] + 1
        #print(m)
        ans = []
        m.sort(reverse=True)
        for i in m:
            if i < C:
                break
            ans.append(i-C)
        #print(ans)
        print(sum(ans))

I am getting a time limit exceeded in this. What would be a faster way to do this?

I would prefer a solution that does not use built-ins or a dictionary

The constraints are- All characters in s are lowercase alphabets, T,N,Q < 10^5, C <= 10^9,

This will work under the given constraints. Assuming the string only consists of lower and uppercase alphabets.

for _ in range(int(input().strip())):
    N, Q = map(int, input().strip().split())
    s = input().strip()

    frequencies = [0] * 26
    [frequencies.__setitem__(ord(k) - 97, frequencies[ord(k) - 97] + 1) for k in list(s)]
    # To get something like [4,4,1,2,0,0,0,0,0,....]
    counts = list(sorted(filter(lambda x: x > 0, frequencies)))
    # counts = [1,2,4,4]

    for __ in range(N):
        C = int(input().strip())

        # Find the index of the value just greater in counts.
        for i, c in enumerate(counts):
            if c > C:
                break

        if i >= 0 and i < len(counts):  # If i is within range. Print the sum from thereon.
            print(max(sum(counts[i:]) - C * len(counts[i:]), 0))  # Subtract C from the individual counts
        else:
            print(0)

And to answer why your code exceeds time limit. You are iterating over the entire string s . Inside the for loop of the queries.

So if len(s)=10^5 and len(N)=10^5 you will make 10^10 iterations. Or O(n^2)

Ok. First of all sorting is a no-no because that will push your complexity to O(n log(n)).

Make an array for taking the count of the 26 alphabets.

Then, count the alphabets in the string using a single loop ie O(n).

Go through your count array and apply the formula you stated above.

Overall complexity would be O(n), which I think what they require.

If you want to leverage all the Python power, then,

from collections import Counter

a = Counter(T) # it will return a dict with counts of all unique characters

Iterate through the returned dict and you will get your answer.

But, I suggest you use the O(n) approach above. Your code will pass then.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM