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.

AnswerBot
4mo

Count the frequency of each word in a given string.

  • Split the string by spaces to get individual words.

  • Use a hashmap to store each word and its frequency.

  • Iterate through the words and update the hashma...read more

Help your peers!
Select
Add answer anonymously...

CitiusTech Java Developer interview questions & answers

A Java Developer was asked 5mo agoQ. What is the Context API in React?
A Java Developer was asked 5mo agoQ. What is Redux in React?
A Java Developer was asked 10mo agoQ. How do you count duplicates from an integer array?

Popular interview questions of Java Developer

A Java Developer was asked 5mo agoQ1. What is the Context API in React?
A Java Developer was asked 10mo agoQ2. How do you count duplicates from an integer array?
A Java Developer was asked Q3. What are the advantages of multithreading in Java?
CitiusTech Java Developer 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