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!
Select
Add answer anonymously...
TO THE NEW Technical Trainee Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits