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).
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Print the frequency of each character in Alphabetical order
- Print an N x M matrix such that each row and column has all the vowels in it
- Find the count of M character words which have at least one character repeated
- Longest sub-string having frequency of each character less than equal to k
- Largest index for each distinct character in given string with frequency K
- Check if frequency of each character is equal to its position in English Alphabet
- Minimum characters required to be removed to make frequency of each character unique
- Minimum addition/removal of characters to be done to make frequency of each character prime
- Check if the given string of words can be formed from words present in the dictionary
- Count words that appear exactly two times in an array of words
- Convert vowels into upper case character in a given string
- Print the first and last character of each word in a String
- Count of Binary Strings of length N such that frequency of 1's exceeds frequency of 0's
- Sort an Array alphabetically when each number is converted into words
- Print last character of each word in a string
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Check if a two character string can be made using given words
- Print the Vowels in the Order of their occurrence in the given Matrix
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.