Skip to content
CodeKruPro

Count vowels, consonants, digits, spaces and other characters

Coding problemEasyAsked in: Capgemini, TCS, Wipro

Given a string s, count five kinds of character and return the five counts in this order: vowels, consonants, digits, spaces, and all other characters.

Vowels are a, e, i, o and u in upper or lower case. Consonants are the other English letters (y is a consonant). Digits are 0 to 9. Spaces are the space character. Everything else, such as punctuation, counts as other.

Examples

Example 1
Input: s = "Hello World 123!"
Output: [3,7,3,2,1]
Example 2
Input: s = "AEIOU y"
Output: [5,1,0,1,0]
Example 3
Input: s = ""
Output: [0,0,0,0,0]

Constraints

  • The length of s is between 0 and 100,000
  • s holds printable ASCII characters only

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in