Word Order in python - HackerRank Solution

Word Order in python - HackerRank Solution
Word Order in python - HackerRank Solution



Problem :


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.



Solution :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# Word Order in python - Hacker Rank Solution
# Python 3
# Enter your code here. Read input from STDIN. Print output to STDOUT
# Word Order in python - Hacker Rank Solution START
import collections;

N = int(input())
d = collections.OrderedDict()

for i in range(N):
    word = input()
    if word in d:
        d[word] +=1
    else:
        d[word] = 1

print(len(d));

for k,v in d.items():
    print(v,end = " ");
# Word Order in python - Hacker Rank Solution END





Disclaimer :-
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.

Next Post Previous Post
4 Comments
  • Unknown
    Unknown Sunday, December 13, 2020

    This is not showing correct in nptel programming exam y

  • Unknown
    Unknown Friday, December 03, 2021

    n = int(raw_input().strip())
    counter = {}
    words = []
    for i in range(n):
    word = raw_input().strip()
    if word in counter:
    counter[word] += 1
    else:
    counter[word] = 1
    words.append(word)

    print len(words)
    print ' '.join([str(counter[word]) for word in words])

  • Anonymous
    Anonymous Tuesday, January 18, 2022

    from collections import Counter

    # The first line contains the integer n
    n = int(input())

    # The next n lines each contain a word
    ip = []
    for i in range(n):
    ip.append(input())

    # Counter returns dict contains (key as each word) and (value as total no of occurrence of a particular word)
    ip_counter = Counter(ip)

    # The number of distinct words from the input
    print(len(ip_counter))

    # The number of occurrences for each distinct word according to their appearance in the input.
    for i in ip_counter.values():
    print(i, end=' ')

  • Anonymous
    Anonymous Friday, July 01, 2022

    from collections import Counter
    n=int(input())
    h=set()
    r=[]
    for i in range(n):
    p=input()
    r.append(p)
    h.add(p)
    a=Counter(r)
    print(len(h))
    print(" ".join(list(str(i) for i in a.values())))

Add Comment
comment url