Count Consonants In A String

Easy
0/40
Average time to solve is 15m
profile
Contributed by
10 upvotes
Asked in companies
OptumFarEye

Problem statement

Given a string ‘STR’ which consists of uppercase and lowercase characters and spaces. Count the number of consonants in the string.

A consonant is an English alphabet character that is not vowel (a, e, i, o, and u). Examples of constants are b, c, d, f, etc.

Example :

Given string 'STR' : ‘Coding Ninjas’ there are 8 consonants i.e ‘C’,’d’,’n’,’g’,’N’,’n’,’j’,’s’, because these characters do not belong to set above mentioned set of vowels.
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘T’ lines represent the ‘T’ test cases.

The only line of each test case consists of a string ‘STR’
Output Format :
For each test case, print a single integer denoting the number of consonants in the string.
Note :
You don't need to print anything, it has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 50
1 <= Length of 'STR' <= 10^4

The string consists of uppercase and lowercase characters and spaces.

Time Limit: 1 sec
Sample Input 1 :
2
Coding Ninjas
MacBook Pro   
Sample Output 1 :
8
6
Explanation For Sample Input 1 :
Test Case 1 :

example-1

Consider the above figure:
The Characters highlighted in red are consonants we can clearly see that there are 8 consonants in the given string. Therefore we print 8.

Test Case 2 :

example-2

The Characters highlighted in red are consonants we can clearly see that there are 6 consonants in the given string. Therefore we print 6.
Sample Input 2 :
2
BANANA
mango
Sample Output 2 :
3
3    

Explanation For Sample Input 2 :

We can clearly see that banana and mango each have 3 consonants,  therefore we return 3 for both of them 
Hint

Find all consonants recursively

Approaches (2)
Recursive Approach

If we can find if the current character is a consonant or not we can create a recursive to count all the vowels.

 

  1. Let ‘i’ be the index of the current character we are at and ‘N’ be the length of the string.
  2. Then for every character ‘i’ we check if it is a consonant or not(a character is a consonant if it is not A, E, I, O, U), If it is we add 1 and call the function recursively to find the answer for the next ‘N - i’ characters.
  3. In the base case, when we reach the last character we return 1 if it is a consonant else we return 0
  4. Finally, we return the answer once we have checked all the characters.
Time Complexity

O(N), where ‘N’ denotes the size of the string.

 

In each recursive call, we just check if it is a consonant or not which is a constant time operation, and we do this for all characters of the string. Hence, the overall time complexity will be O(N).

Space Complexity

O(N), where ‘N’ denotes the size of the string.

 

The space complexity is the order of ‘N’ because in the case of recursion the space complexity is proportional to the depth of the recursion tree, which in this case is proportional to the size of the string. Hence, the overall space complexity will be O(N).

Code Solution
(100% EXP penalty)
Count Consonants In A String
Full screen
Console