Word Frequency Counter

Easy
string frequency sorting
Problem Description

Count the frequency of each word in a given text and return the top k most frequent words in descending order of frequency.

Input Format
First line contains integer k. Second line contains the text string.
Output Format
Print top k words with their frequencies, one per line in format "word:frequency"
Constraints
1 ≤ k ≤ 100, text length ≤ 1000 characters
Sample Input/Output
Input:
2
the quick brown fox jumps over the lazy dog the fox
Output:
the:3
fox:2
Explanation

Count word frequencies and return top 2 most frequent words

Solution Hints
  • Read carefully: Understand input/output format and constraints
  • Start simple: Think of brute force solution first, then optimize
  • Edge cases: Consider empty inputs, single elements, max constraints
  • Data structures: Choose arrays, hash maps, sets based on problem needs
  • Time complexity: Ensure your solution fits within time limits
  • Test examples: Verify logic with provided sample inputs
120
Points
2000ms
Time Limit
256MB
Memory Limit
Code Editor
Register to Submit
Register to Access Code Editor

Create a free account to solve coding problems and track your progress.

Register Now
Feedback