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.
Be the first one to answer
Add answer anonymously...
Virtusa Consulting Services Associate Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter