Problem · String

Replace Nth Consonant

Learn this problem
EasyDuolingo logoDuolingoOA

Problem statement

See the Image Source section at the very bottom of the page for the original problem statement 🐈‍⬛ 🐈‍

In a world of letters, there’s a secret mission hidden within a string of text. Your task is to find every nth consonant in the message and transform it into the next consonant in the alphabet, while preserving its original case. The journey of the consonants continues as 'b' becomes 'c', 'x' transforms into 'y', and when you reach 'z', it wraps back around to 'b' (or 'Z' to 'B'). Your goal is to follow this pattern, subtly shifting every nth consonant while leaving the rest of the message untouched, returning the newly crafted string when the job is done.

Function

replaceNthConsonant(message: String, n: int) → String

Examples

Example 1

message = "Codesignal"n = 3return = "Codetignam"

The consonants in "Codesignal" are C-d-s-g-n-l. The third consonant is s, which is replaced by t. The sixth consonant is l, which is replaced by m. Thus, the resulting string is "Codetignam".

More Duolingo problems

drafts saved locally
public String replaceNthConsonant(String message, int n) {
  // write your code here
}
message"Codesignal"
n3
expected"Codetignam"
checking account