[LeetCode] Find the Encrypted String

3210. Find the Encrypted String

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.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
string getEncryptedString(string s, int k) {
string res = "";
int n = s.length();
for(int i = 0; i < s.length(); i++) {
res.push_back(s[(i+k)%n]);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/07/07/PS/LeetCode/find-the-encrypted-string/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.