Find the Encrypted String - Problem
Find the Encrypted String is a cyclic character shifting problem that simulates a basic encryption algorithm.
You are given a string s and an integer k. Your task is to encrypt the string using a cyclic shifting algorithm:
- For each character
cat positioniin strings - Replace
cwith the character that iskpositions ahead in the same string - If we go beyond the end of the string, wrap around to the beginning (cyclic behavior)
Example: If s = "abc" and k = 1, then:
'a'at index 0 → replace with character at index(0 + 1) % 3 = 1→'b''b'at index 1 → replace with character at index(1 + 1) % 3 = 2→'c''c'at index 2 → replace with character at index(2 + 1) % 3 = 0→'a'
Result: "bca"
Return the encrypted string.
Input & Output
example_1.py — Basic Encryption
$
Input:
s = "dart", k = 3
›
Output:
"tdar"
💡 Note:
Each character shifts 3 positions forward: d(0)→t(3), a(1)→d(0), r(2)→a(1), t(3)→r(2). The modulo operation handles the wrapping.
example_2.py — Single Character Wrap
$
Input:
s = "aaa", k = 1
›
Output:
"aaa"
💡 Note:
All characters are the same, so shifting by any amount within the string length produces the same result.
example_3.py — Large Shift Value
$
Input:
s = "abc", k = 4
›
Output:
"bca"
💡 Note:
k=4 is equivalent to k=1 (since 4%3=1) for a string of length 3. So a→b, b→c, c→a.
Constraints
- 1 ≤ s.length ≤ 100
- 0 ≤ k ≤ 105
- s consists only of lowercase English letters
Visualization
Tap to expand