Description
You are given a string s and an integer k. Encrypt the string using the following algorithm:
- For each character
cins, replacecwith thekthcharacter aftercin the string (in a cyclic manner).
Return the encrypted string.
Β
Example 1:
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:
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 <= 1001 <= k <= 104sconsists only of lowercase English letters.
Solution
Python3
class Solution:
def getEncryptedString(self, s: str, k: int) -> str:
N = len(s)
res = []
for i, x in enumerate(s):
index = (i + k) % N
res.append(s[index])
return "".join(res)