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.
Anonymous
11d
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!
Add answer anonymously...
Standard Chartered Software 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