Guys it's Hackerrank problem say 'Word Order' my code passing 5 test cases but for remaining 3 tests it's showing timeout | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Guys it's Hackerrank problem say 'Word Order' my code passing 5 test cases but for remaining 3 tests it's showing timeout

https://code.sololearn.com/ccE29LQ1Tei7/?ref=app

21st Apr 2020, 4:11 PM
Mohd Aadil
Mohd Aadil - avatar
2 Answers
+ 2
Thanks Zuke
21st Apr 2020, 5:47 PM
Mohd Aadil
Mohd Aadil - avatar
+ 1
Does this help? from collections import Counter n = int(input()) words = [] for i in range(n): words.append(input()) counter = Counter(words) print(len(counter)) print(*counter.values()) Also, no need to list() a set, you can just do len(set(...)) The reason your solution times out is because you're doing l.count(i), for each i in l; that means it's gonna go through n elements of l, then for each again traversing through l to get their count. That's n² operations. You're better off going through the list once holding the result in a dictionary. counter = dict.fromkeys(l, 0) for key in l: counter[key] += 1
21st Apr 2020, 5:44 PM
Zuke
Zuke - avatar