BackmediumStringsUber

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.

Example 1
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'.

Example 2
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.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Vowel Shift Cipher — Problem Statement & Solution Guide

StringsMediumMixed
TimeO(n)
|
SpaceO(n)

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

Example 1

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'.

Example 2

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

JavaScript Solution
Time: O(n)
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

Uber

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.