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).
- 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
- Longest sub-string having frequency of each character less than equal to k
- Check if a two character string can be made using given words
- Check if the frequency of any character is more than half the length of the string
- Character whose frequency is equal to the sum of frequencies of other characters of the given string
- Index of character depending on frequency count in string
- Print an N x M matrix such that each row and column has all the vowels in it
- Print reverse string after removing vowels
- Given a sequence of words, print all anagrams together using STL
- Print all unique words of a String
- Given a sequence of words, print all anagrams together | Set 2
- Print all funny words in a string
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.