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.
AnswerBot
16h

The goal is to determine the frequency of each word in a given string.

  • Split the input string into individual words

  • Create a dictionary to store word frequencies

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

Help your peers!
Add answer anonymously...
TO THE NEW Technical Trainee 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