1 / 4
Apr 2015

I am getting WA. Can someone plzz help me. Here is my Python code.

import collections
t=int(input())
for j in range (1,t+1):
    l=input()
    c=collections.Counter(l).most_common(1)[0][0]
    q=l.split()
    a=[]
    h=len(q)
    print ("Case #%d:"%j)
    for i in range(0,h):
        d=collections.Counter(q[i]).most_common(2)
        if (len(d)>1):
            if (d[0][1]==d[1][1]):
                continue
            elif (d[0][0]==c):
                print(q[i])
        else:
            if (d[0][0]==c):
                print(q[i])
  • created

    Apr '15
  • last reply

    Apr '15
  • 3

    replies

  • 507

    views

  • 3

    users

This is the line where your solution breaks down.

c=collections.Counter(l).most_common(1)[0][0]

Edit: I moved this to the Python forums so someone who actually uses python will see it. There are a couple of AC python solutions, so it should be possible.

I didn't read your code but collections.Counter is slow. Use any alternative data structure.