Occurrence of Each Word: Problem Statement

You are given a string S consisting of several words. Your task is to count the number of times each word appears in string S. A word is defined as a sequence of one or more non-space characters. Note that the string can have multiple spaces between words and may also have leading or trailing spaces.

Example:

Input:
 "what we think we become"
Output:
"what" 1
"we" 2
"think" 1
"become" 1
Explanation:

The words "what", "think", and "become" each occur once, while the word "we" occurs twice in the given string.

Constraints:

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

Input:

The input consists of a single line containing string S.

Output:

Output each unique word with its occurrence count on a new line. The word and count should be separated by a space.

Note:

You can produce the output in any order.
AnswerBot
4mo

Count the occurrence of each word in a given string.

  • Split the string into words using spaces as delimiters.

  • Use a hashmap to store the count of each word.

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

Anonymous
5mo
package com.practice.newstart; import java.util.HashMap; import java.util.Map; import java.util.StringTokenizer; public class PrintUniqueWords { public static void main(String[] args) { printWords("wh...read more
Help your peers!
Select
Add answer anonymously...

Standard Chartered Software Developer interview questions & answers

A Software Developer was asked 11mo agoQ. How do you call a REST API?
A Software Developer was asked 11mo agoQ. Explain Encapsulation.
A Software Developer was asked Q. Why are you leaving your current job?

Popular interview questions of Software Developer

A Software Developer was asked 11mo agoQ1. How do you call a REST API?
A Software Developer was asked 11mo agoQ2. Explain Encapsulation.
A Software Developer was asked Q3. Why are you leaving your current job?
Standard Chartered Software 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