Hacker News new | ask | show | jobs
by DannyB2 377 days ago
Assume use of 8 bit characters. Declare a constant 256 entry array pre-filled with all False except for the five (or six) vowel characters. This is baked into the code and not initialized at runtime.

Now for each character c in the input string, simply do an array index and see if it is true (a vowel) or not. This avoids either five conditionals, or a loop over the string 'aeiou'. The vowel test is constant time regardless of the character value.

1 comments

In Python this is going to be slow, as you’ll have a ton of Python code in your hot loop.

It’s also going to be even more broken than TFA if any non-ascii character is present in the string.