Collections.deque() in python HackerRank Solution

Hello Programmers, In this post, you will know how to solve the Collections.deque() in python HackerRank Solution. This problem is a part of the HackerRank Python Programming Series.

Ezoicreport this adCollections.deque() in python HackerRank Solution
Collections.deque() in python HackerRank Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

Collections.deque() in python HackerRank Solution

Ezoicreport this adProblem

You are given n words. Some words may repeat. For each word, output its number of occurrences. The output order should correspond with the input order of appearance of the word. See the sample input/output for clarification.

Note: Each input line ends with a “\n” character.

Constraints :

  • 1 <= n <= 10^5

The sum of the lengths of all the words do not exceed 10^6
All the words are composed of lowercase English letters only.

Input Format :

The first line contains the integer, n.
The next n lines each contain a word.

Output Format :

Output 2 lines.
On the first line, output the number of distinct words from the input.
On the second line, output the number of occurrences for each distinct word according to their appearance in the input.

Sample Input :

4
bcdef
abcdefg
bcde
bcdef

Sample Output :

3
2 1 1

Explanation :

There are 3 distinct words. Here, “bcdef” appears twice in the input at the first and last positions. The other words appear once each. The order of the first appearances are “bcdef”, “abcdefg” and “bcde” which corresponds to the output.

Collections.deque() in python HackerRank Solutions

# Enter your code here. Read input from STDIN. Print output to STDOUT
from collections import deque
d = deque()
for _ in range(int(input())):
    inp = input().split()
    getattr(d, inp[0])(*[inp[1]] if len(inp) > 1 else [])
print(*[item for item in d])

Disclaimer: The above Problem (Collections.deque() in python) is generated by Hackerrank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: Company Logo in python HackerRank Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad