Problem

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.

Examples

Example 1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11

Input: s = "dart", k = 3

Output: "tdar"

Explanation:

  * For `i = 0`, the 3rd character after `'d'` is `'t'`.
  * For `i = 1`, the 3rd character after `'a'` is `'d'`.
  * For `i = 2`, the 3rd character after `'r'` is `'a'`.
  * For `i = 3`, the 3rd character after `'t'` is `'r'`.

Example 2

1
2
3
4
5
6
7
8
9

Input: s = "aaa", k = 1

Output: "aaa"

Explanation:

As all the characters are the same, the encrypted string will also be the
same.

Constraints

  • 1 <= s.length <= 100
  • 1 <= k <= 10^4
  • s consists only of lowercase English letters.

Solution

Method 1 – Cyclic Indexing

Intuition

To encrypt the string, for each character at index i, we replace it with the character at index (i + k) % n, where n is the length of the string. This wraps around the string in a cyclic manner.

Approach

  1. Let n be the length of the string s.
  2. For each index i from 0 to n-1:
    • The encrypted character at position i is s[(i + k) % n].
  3. Build the encrypted string by collecting these characters in order.
  4. Return the encrypted string.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
public:
    string encryptString(string s, int k) {
        int n = s.size();
        string ans(n, ' ');
        for (int i = 0; i < n; ++i) {
            ans[i] = s[(i + k) % n];
        }
        return ans;
    }
};
1
2
3
4
5
6
7
8
func encryptString(s string, k int) string {
    n := len(s)
    ans := make([]byte, n)
    for i := 0; i < n; i++ {
        ans[i] = s[(i+k)%n]
    }
    return string(ans)
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
    public String encryptString(String s, int k) {
        int n = s.length();
        char[] ans = new char[n];
        for (int i = 0; i < n; ++i) {
            ans[i] = s.charAt((i + k) % n);
        }
        return new String(ans);
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
    fun encryptString(s: String, k: Int): String {
        val n = s.length
        val ans = CharArray(n)
        for (i in 0 until n) {
            ans[i] = s[(i + k) % n]
        }
        return String(ans)
    }
}
1
2
3
4
class Solution:
    def encryptString(self, s: str, k: int) -> str:
        n = len(s)
        return ''.join(s[(i + k) % n] for i in range(n))
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
impl Solution {
    pub fn encrypt_string(s: String, k: i32) -> String {
        let n = s.len();
        let s = s.as_bytes();
        let mut ans = vec![0u8; n];
        for i in 0..n {
            ans[i] = s[(i + k as usize) % n];
        }
        String::from_utf8(ans).unwrap()
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
    encryptString(s: string, k: number): string {
        const n = s.length;
        let ans = '';
        for (let i = 0; i < n; ++i) {
            ans += s[(i + k) % n];
        }
        return ans;
    }
}

Complexity

  • ⏰ Time complexity: O(n), where n is the length of the string, as we process each character once.
  • 🧺 Space complexity: O(n), for the output string.