Word Order - Python HackerRank Solution
Hello Friends, How are you? Today I am going to solve the HackerRank Word Order Problem in Python with a very easy explanation. In this article, you will get one or more approaches to solving this problem. So let's start-
{tocify} $title={Table of Contents}
HackerRank Python Word Order Solution - Problem Statement
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 the appearance of the word. See the sample input/output for clarification.
Note: Each input line ends with a "\n" character.
Input Format
The first line contains the integer, n.
The next n lines each contain a word.
Constraints
1 <= n <= 10^5
The sum of the lengths of all the words does not exceed 10^6
All the words are composed of lowercase English letters only.
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 0
4 bcdef abcdefg bcde bcdef {codeBox}
Sample Output 0
3 2 1 1 {codeBox}
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 is "bcdef", "abcdefg" and "bcde" which corresponds to the output.
Python Word Order - Hacker Rank Solution
Approach I: Word Order HackerRank Python Solution
# ========================
# Information
# ========================
# Name: Word Order in Python HackerRank
# Direct Link: https://www.hackerrank.com/challenges/word-order/problem
# Difficulty: Medium
# Max Score: 50
# Language: Pypy 3
# ========================
# Solution Start
# ========================
#Word Order in Python - Hacker Rank Solution
# Enter your code here. Read input from STDIN. Print output to STDOUT
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
# MyEduWaves
Disclaimer: The above Problem ( Python Word Order ) is generated by Hackerrank but the Solution is Provided by MyEduWaves. This tutorial is only for Educational and Learning purposes. Authority if any queries regarding this post or website fill out the contact form.
I hope you have understood the solution to this HackerRank Problem. All these three solutions will pass all the test cases. Now visit Python Word Order Hackerrank Problem and try to solve it again.
All the Best!
Tags:
HackerRank Python