Be the first user to complete this post

  • 0
Add to List
Medium

296. Replace all vowels with next consonant in a given string

Objective: Given a string, write an algorithm to replace all the vowels with next consonant, and if last alphabet is vowel, remove it.

Example:

String input: “abcdefg”
Output: “bbcdffg”

String input: “aaaabccceee”
Output: “bbbbbccc”

String input: “aaa”
Output: “”

Approach:

  • Traverse the string right to left one character at a time.
  • Maintain a variable , last_visited_non_vowel
  • During traversal , when vowel is encountered, replace it with , last_visited_non_vowel and add to result.
  • During traversal , when consonant(non vowel) is encountered, update the last_visited_non_vowel and add to result.

Output:

Input: abcdefg, Output: bbcdffg
Input: aaaabccceee, Output: bbbbbccc
Input: aaaa, Output: