Consonant Shift Cipher

Quick Overview

Encode a lowercase string by moving each consonant forward k places along the 21-letter consonant sequence while vowels stay fixed. Tests index mapping, modular wrap-around and handling a shift as large as one billion.

Consonant Shift Cipher

Company: Hudson River Trading

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a string `s` consisting of lowercase English letters and a non-negative integer `k`, encode the string using a consonant shift cipher. In this cipher, only consonants are shifted forward by `k` positions within the ordered sequence of consonants (`b, c, d, f, g, h, j, k, l, m, n, p, q, r, s, t, v, w, x, y, z`). Vowels (`a, e, i, o, u`) remain unchanged. The shift wraps around, so shifting past `z` returns to `b`. Return the resulting encoded string. ### Function Signature ```python def consonant_shift(s: str, k: int) -> str: ``` ### Rules - The consonant sequence above has 21 letters, and `y` is one of them. - Each consonant moves `k` places forward in that sequence, wrapping from `z` back to `b`. Every character keeps its position in the string. ### Constraints - `1 <= s.length <= 10^5` - `0 <= k <= 10^9` - `s` consists of lowercase English letters only. ### Examples ```text Input: s = "hello", k = 1 Output: "jemmo" ``` `h` moves to `j` (the vowel `i` is not part of the sequence), each `l` moves to `m`, and the vowels `e` and `o` stay. ```text Input: s = "abcz", k = 2 Output: "adfc" ``` `a` stays, `b` becomes `d`, and `c` becomes `f` because `e` is skipped. `z` wraps around: one step takes it to `b` and the second to `c`. ```text Input: s = "xyz", k = 1000000000 Output: "npq" ``` A shift of 21 returns every consonant to itself, and `1000000000 % 21 == 13`, so this is a shift of 13. `x` sits at index 18 of the sequence and moves to index 10 (`n`), `y` moves from 19 to 11 (`p`), and `z` from 20 to 12 (`q`).

Overview: Encode a lowercase string by moving each consonant forward k places along the 21-letter consonant sequence while vowels stay fixed. Tests index mapping, modular wrap-around and handling a shift as large as one billion.

Hudson River Trading logo
Hudson River Trading
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a string s consisting of lowercase English letters and a non-negative integer k, encode the string using a consonant shift cipher.

In this cipher, only consonants are shifted forward by k positions within the ordered sequence of consonants (b, c, d, f, g, h, j, k, l, m, n, p, q, r, s, t, v, w, x, y, z). Vowels (a, e, i, o, u) remain unchanged.

The shift wraps around, so shifting past z returns to b. Return the resulting encoded string.

Function Signature

def consonant_shift(s: str, k: int) -> str:

Rules

  • The consonant sequence above has 21 letters, and y is one of them.
  • Each consonant moves k places forward in that sequence, wrapping from z back to b . Every character keeps its position in the string.

Constraints

  • 1 <= s.length <= 10^5
  • 0 <= k <= 10^9
  • s consists of lowercase English letters only.

Examples

Input: s = "hello", k = 1
Output: "jemmo"

h moves to j (the vowel i is not part of the sequence), each l moves to m, and the vowels e and o stay.

Input: s = "abcz", k = 2
Output: "adfc"

a stays, b becomes d, and c becomes f because e is skipped. z wraps around: one step takes it to b and the second to c.

Input: s = "xyz", k = 1000000000
Output: "npq"

A shift of 21 returns every consonant to itself, and 1000000000 % 21 == 13, so this is a shift of 13. x sits at index 18 of the sequence and moves to index 10 (n), y moves from 19 to 11 (p), and z from 20 to 12 (q).