Problem · String

Shift Every K-th Consonant

Learn this problem
EasyTiktokNEW GRADINTERNOA
See Tiktok hiring insights

Problem statement

You are implementing a simple substitution cipher that affects only consonant characters in a string memo.

Read the consonants in memo from left to right. Every k-th consonant is shifted to the following consonant in the alphabet, while vowels and all other characters remain unchanged. In other words, consonants numbered k, 2 * k, 3 * k, and so on are shifted.

  • The consonants are b, c, d, f, g, h, j, k, l, m, n, p, q, r, s, t, v, w, x, y, and z.
  • When shifting from z, wrap around to b.
  • Preserve the case of every shifted character.

Return the transformed memo.

A solution with time complexity no worse than O(memo.length^2) will fit within the execution time limit.

Function

solution(memo: String, k: int) → String

Examples

Example 1

memo = "CodeSignal"k = 3return = "CodeTignam"

The consonants are C, d, S, g, n, and l. The third consonant S shifts to T, and the sixth consonant l shifts to m.

Example 2

memo = "Quiz, Citizenship, puZZle"k = 5return = "Quiz, Citibenship, quZZle"

The fifth consonant is the lowercase z in Citizenship, so it wraps to b. The tenth consonant is the first p in puZZle, so it shifts to q.

More Tiktok problems

drafts saved locally
public String solution(String memo, int k) {
    // write your code here
}
memo"CodeSignal"
k3
expected"CodeTignam"
checking account