Vowel Shift Cipher — Problem Statement & Solution Guide
Problem Description
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
Input
hello
Output
hloiu
Explanation: Step-by-step: 'e' shifts to 'i', 'o' shifts to 'u', 'u' shifts to 'a', 'a' shifts to 'e', 'e' shifts to 'i', 'i' shifts to 'o', and 'o' shifts to 'u'. Reversing the string gives 'hloiu'.
Input
world
Output
udro
Explanation: Step-by-step: 'o' shifts to 'u', 'u' shifts to 'a', 'r' is a consonant, 'l' is a consonant, 'd' is a consonant, 'a' shifts to 'e', 'e' shifts to 'i', 'i' shifts to 'o', and 'o' shifts to 'u'. Reversing the string gives 'udro'.
Constraints
- Input string will only contain lowercase English letters.
- Input length will not exceed 100 characters.
Optimal Approach & Strategy
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).
Brute Force Approach
The brute force approach would involve shifting each vowel character in the string individually and then reversing the entire string. However, this would result in a time complexity of O(n^2) due to the nested loops.
Verified Code Solutions
function vowelShiftCipher(s) {
const vowels = 'aeiou';
const shiftedVowels = s.split('').map(c => {
if (vowels.includes(c)) {
const index = vowels.indexOf(c);
const shiftedIndex = (index + 1) % vowels.length;
if (c === 'u' && vowels[shiftedIndex] === 'a') {
return 'a';
}
let temp = vowels[shiftedIndex];
while (s.includes(temp)) {
shiftedIndex = (shiftedIndex + 1) % vowels.length;
temp = vowels[shiftedIndex];
}
return vowels[shiftedIndex];
}
return c;
}).join('');
return shiftedVowels.split('').reverse().join('');
}public class Solution {
public String vowelShiftCipher(String s) {
String vowels = "aeiou";
StringBuilder shiftedS = new StringBuilder();
for (char c : s.toCharArray()) {
if (vowels.indexOf(c) != -1) {
int idx = vowels.indexOf(c);
shiftedS.append(vowels.charAt((idx + 1) % 5));
} else {
shiftedS.append(c);
}
}
return shiftedS.reverse().toString();
}
}def vowel_shift_cipher(s):
vowels = 'aeiou'
shifted_s = ''
for char in s:
if char in vowels:
idx = vowels.index(char)
shifted_s += vowels[(idx + 1) % 5]
else:
shifted_s += char
return shifted_s[::-1]function vowelShiftCipher(s) {
const vowels = 'aeiou';
const shiftedVowels = s.split('').map(c => {
if (vowels.includes(c)) {
const index = vowels.indexOf(c);
const shiftedIndex = (index + 1) % vowels.length;
if (c === 'u' && vowels[shiftedIndex] === 'a') {
return 'a';
}
let temp = vowels[shiftedIndex];
while (s.includes(temp)) {
shiftedIndex = (shiftedIndex + 1) % vowels.length;
temp = vowels[shiftedIndex];
}
return vowels[shiftedIndex];
}
return c;
}).join('');
return shiftedVowels.split('').reverse().join('');
}Asked in Top Tech Interviews
Solve in Interative Editor
Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.