Given a string str, the task is to find the frequency of each word in a string.
Input: str = “Geeks For Geeks”
For occurs 1 time and Geeks occurs 2 times in the given string str.
Input: str = “learning to code is learning to create and innovate”
The words and, code, create, innovate, is occurs 1 time; and learning, to occurs 2 times in the given string str.
Approach: To solve the problem mentioned above we have to follow the steps given below:
- Use a Map data structure to store the occurrence of each word in the string.
- Traverse the entire string and check whether the current word is present in map or not. If it is present, then update the frequency of the current word else insert the word with frequency 1.
- Traverse in the map and print the frequency of each word.
Below is the implementation of the above approach:
For - 1 Geeks - 2
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Find frequency of each word in a string in Python
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- String containing first letter of every word in a given string with spaces
- Most frequent word in first String which is not present in second String
- Length Of Last Word in a String
- Print last character of each word in a string
- Count occurrences of a word in string
- Capitalize the first and last character of each word in a string
- Print the first and last character of each word in a String
- Find the first repeated word in a string
- Reverse every word of the string except the first and the last character
- Remove the first and last character of each word in a string
- First substring whose reverse is a word in the string
- Find the first maximum length even word from a string
- Get the first letter of each word in a string using regex in Java
- Frequency of a substring in a string
- Reverse all the word in a String represented as a Linked List
- Program to find Smallest and Largest Word in a String
- Frequency of a substring in a string using pthread
- Sort a string according to the frequency of characters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : SHUBHAMSINGH10