Word Occurrence Counting
Given a string 'S' of words, the goal is to determine the frequency of each word in the string. Consider a word as a sequence of one or more non-space characters. The string can have multiple spaces between words and may include leading or trailing spaces.
Input:
The input is a single line containing a string 'S'.
Output:
Outputs each unique word and its frequency of occurrence, separated by a space, on separate lines for each word.
Example:
Input: "what we think we become"
Output: "what 1", "we 2", "think 1", "become 1"
Constraints:
- 0 <= |S| <= 105
- Time Limit: 1 second
Note:
The order of the printed words does not matter.

AnswerBot
4mo
The goal is to determine the frequency of each word in a given string.
Split the input string into individual words
Create a dictionary to store word frequencies
Iterate through the words and update the ...read more
Help your peers!
Add answer anonymously...
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

