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
2mo

Count the frequency of each unique word in a given string, handling multiple spaces and ignoring leading/trailing spaces.

  • Input Handling: Trim the string to remove leading/trailing spaces and split it ...read more

Help your peers!
Select
Add answer anonymously...

Compass Group Support Services Software Engineer interview questions & answers

A Software Engineer was asked Q. Sum Of Zeroes Coverage Calculation You are provided with a binary matrix contain...read more
A Software Engineer was asked Q. Nth Fibonacci Number Problem Statement Calculate the Nth term in the Fibonacci s...read more
A Software Engineer was asked Q. Word Occurrence Counting Given a string 'S' of words, the goal is to determine t...read more
Compass Group Support Services Software Engineer 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