Distance to Nearest Vowel

Published by Danny May in

Write a function that takes in a string and for each character, returns the distance to the nearest vowel in the string. If the character is a vowel itself, return 0.

Examples

DistanceToNearestVowel("aaaaa") ➞ [0, 0, 0, 0, 0]

DistanceToNearestVowel("babbb") ➞ [1, 0, 1, 2, 3]

DistanceToNearestVowel("abcdabcd") ➞ [0, 1, 2, 1, 0, 1, 2, 3]

DistanceToNearestVowel("shopper") ➞ [2, 1, 0, 1, 1, 0, 1]

Notes

  • All input strings will contain at least one vowel.
  • Strings will be lowercased.
  • Vowels are: a, e, i, o, u.
Watch a quick demo on how Edabit works.