![]() |
VOOZH | about |
Given a character ch representing an English alphabet, determine whether it is a vowel or not. Return true if ch is a vowel otherwise return false.
Examples:
Input: ch = 'a'
Output: true
Explanation: 'a' is a vowel. So output for this test case is true.Input: ch = 'Z'
Output: false
Explanation: 'Z' is not a vowel. So output for this test case is false.
Table of Content
The Idea is to Directly compare the given character with all vowels (both lowercase and uppercase). If it matches any, return
true; otherwise returnfalse.
Algorithm:
true
Time Complexity: O(1)
Auxiliary Space: O(1)
Both lowercase and uppercase vowels share the same last 5 bits (LSBs) in their ASCII representation. This allows us to normalize any character using (ch & 0x1F) and map it to a small range of values.
We use a precomputed bitmask 0x208222, which is designed such that when it is right-shifted by the normalized value (ch & 0x1F), it produces 1 at the least significant bit for vowel positions (1, 5, 9, 15, 21), and 0 for all other characters.
Thus, by evaluating:
( 0 x 208222 >> (ch & 0 x 1F)) & 1
ASCII Values of Vowels (Lowercase and Uppercase):
Algorithm:
true
Time Complexity: O(1)
Auxiliary Space: O(1)