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 c at position i in string s
  • Replace c with the character that is k positions 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
Cyclic Encryption: s="abcd", k=2ai=0bi=1ci=2di=3Each character shifts k=2 positions clockwisePosition Mappinga: (0 + 2) % 4 = 2 → cb: (1 + 2) % 4 = 3 → dc: (2 + 2) % 4 = 0 → ad: (3 + 2) % 4 = 1 → bEncrypted Result:cdab"cdab"
Understanding the Visualization
1
Arrange in Circle
Visualize the string characters arranged in a circle
2
Apply Shift Formula
For each position i, calculate (i + k) % length
3
Build Result
Collect characters from their new positions to form encrypted string
Key Takeaway
🎯 Key Insight: Modular arithmetic `(i + k) % n` directly computes the target position, making the algorithm efficient and eliminating the need for manual iteration through the circular structure.
Asked in
Amazon 15 Microsoft 12 Google 8 Meta 6
23.4K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen