Skip to main content
Back to problems
#3210
Easy Algorithms

Find the encrypted string

String
68.1% acceptance
Feb 25, 2026
111
10
You are given a string s and an integer k. Encrypt the string using the following algorithm: For each character c in s, replace c with the kth character after c in the string (in a cyclic manner). Return the encrypted string.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn get_encrypted_string(s: String, k: i32) -> String {
    let bytes: Vec<u8> = s.bytes().collect();
    let n = bytes.len();
    let shift = (k as usize) % n;
    (0..n).map(|i| bytes[(i + shift) % n] as char).collect()
  }
}