LeetCode-in-Java

3210. Find the Encrypted String

Easy

You are given a string s and an integer k. Encrypt the string using the following algorithm:

Return the encrypted string.

Example 1:

Input: s = “dart”, k = 3

Output: “tdar”

Explanation:

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:

Solution

public class Solution {
    public String getEncryptedString(String s, int k) {
        int n = s.length();
        int localK = k % n;
        return s.substring(localK, n) + s.substring(0, localK);
    }
}