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
Understanding the Visualization
1
Input
Original string s and shift amount k
2
Process
Each character moves k positions forward with wrapping
3
Output
Encrypted string with shifted characters
Key Takeaway
🎯 Key Insight: Use modular arithmetic (i + k) % n to directly calculate target positions without manual counting
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code