Given a string str with uppercase, lowercase and special characters. The input string is to end with either a space or a dot. The problem is to calculate the number of words, vowels and frequency of each character of the string in a separate line.
Input : How Good GOD Is. Output : Number of words = 4 Number of vowels = 5 Number of upper case characters = 6 Character = Frequency = 3 Character = . Frequency = 1 Character = D Frequency = 1 Character = G Frequency = 2 Character = H Frequency = 1 Character = I Frequency = 1 Character = O Frequency = 1 Character = d Frequency = 1 Character = o Frequency = 3 Character = s Frequency = 1 Character = w Frequency = 1
Approach : We use a TreeMap to store characters and their frequencies. TreeMap is used to get the output in sorted order.
Below is Java implementation of above approach :
Number of words = 3 Number of vowels = 5 Number of upper case characters = 2 Character = Frequency = 2 Character = . Frequency = 1 Character = G Frequency = 2 Character = e Frequency = 4 Character = f Frequency = 1 Character = k Frequency = 2 Character = o Frequency = 1 Character = r Frequency = 1 Character = s Frequency = 2
Time Complexity : O(n), where n is the number of characters in the string.
Auxiliary Space : O(1).
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.
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Print the frequency of each character in Alphabetical order
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Find the count of M character words which have at least one character repeated
- Queries to print the character that occurs the maximum number of times in a given range
- Check if a two character string can be made using given words
- Longest sub-string having frequency of each character less than equal to k
- Index of character depending on frequency count in string
- Largest index for each distinct character in given string with frequency K
- Character whose frequency is equal to the sum of frequencies of other characters of the given string
- Check if the frequency of any character is more than half the length of the string
- Print an N x M matrix such that each row and column has all the vowels in it
- Check if frequency of each character is equal to its position in English Alphabet
- Print reverse string after removing vowels
- Minimum addition/removal of characters to be done to make frequency of each character prime
- Print all funny words in a string
- Print all possible words from phone digits
- Program to print the given digit in words
- Print all the non-repeating words from the two given sentences
- Given a sequence of words, print all anagrams together using STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.