Word Frequency Counter

Given a string 'S' consisting of words, your task is to calculate how many times each word appears in the string. A word is a series of one or more non-space characters. In 'S', words can be separated by spaces, and you may have leading or trailing spaces.

Input:

"what we think we become"

Output:

what 1
we 2
think 1
become 1

Example:

Consider the string: "what we think we become". Here, the word "what", "think", and "become" appear once, while "we" appears twice.

Constraints:

  • 0 <= |S| <= 10^5
  • Time Limit: 1 second

Note:

The output can be in any order.

Be the first one to answer
Add answer anonymously...
CitiusTech Java Developer 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