Find the Encrypted String - Problem
You are given a string s and an integer k. Your task is to encrypt the string using the following algorithm:
For each character c in s, replace c with the k-th character after c in the string (in a cyclic manner).
Example: If s = "abc" and k = 1, then 'a' is replaced by 'b' (1 position after 'a'), 'b' is replaced by 'c' (1 position after 'b'), and 'c' is replaced by 'a' (1 position after 'c' cyclically).
Return the encrypted string.
Input & Output
Example 1 — Basic Case
$
Input:
s = "abc", k = 1
›
Output:
"bca"
💡 Note:
For each character, replace with the next character: 'a' → 'b' (1 step), 'b' → 'c' (1 step), 'c' → 'a' (1 step, wrapping around)
Example 2 — Larger Step
$
Input:
s = "abcdef", k = 2
›
Output:
"cdefab"
💡 Note:
Each character moves 2 positions forward: 'a'→'c', 'b'→'d', 'c'→'e', 'd'→'f', 'e'→'a', 'f'→'b'
Example 3 — Single Character
$
Input:
s = "x", k = 1
›
Output:
"x"
💡 Note:
With only one character, moving k positions always returns to the same character
Constraints
- 1 ≤ s.length ≤ 100
- 1 ≤ k ≤ 104
- s consists of lowercase English letters
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code