mediumStringsPattern: Mixed
Vowel Shift Cipher Solution
Problem Statement
Given a string `s`, encode it by shifting all vowels to the next vowel in the sequence 'aeiou' (with 'u' wrapping around to 'a') and leaving consonants unchanged, then reverse the entire string to obtain the encoded string.
Examples
Example 1:
Input:hello
Output:ollih
Explanation: Shift vowels in 'hello' to get 'holla', then reverse to get 'ollah'. But given the specific rules, 'e' shifts to 'i', resulting in 'hillo' before reversing to 'ollih'.
Example 2:
Input:world
Output:dlroiw
Explanation: Shift vowels in 'world' to get 'wirld', then reverse to get 'dlriw'. But given the specific rules, 'o' shifts to 'u', resulting in 'wirld' before reversing to 'dlriw'. But since there are no vowels in 'world' to shift, no change occurs, and then it simply reverses to 'dlrow'. Given the shift, 'o' to 'u', we get 'wurld' before reversing to 'dlruw'.
Constraints
- Input string will only contain lowercase English letters.
- Input length will not exceed 100 characters.
Time: O(n) Space: O(n)
The optimal approach is to first create a string of vowels, shift the index of each vowel, and then use the resulting string to replace the vowels in the original string. After that, reverse the entire string to get the encoded message. This approach has a time complexity of O(n).
Run, Test & Submit Code
Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.
Solve on Interactive WorkspaceTested Solutions
No solution code is currently loaded.
Complete this code in the workspace editor.
