LeetCode in Kotlin

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

@Suppress("NAME_SHADOWING")
class Solution {
    fun getEncryptedString(s: String, k: Int): String {
        var k = k
        val n = s.length
        k %= n
        val str = StringBuilder(s.substring(k, n))
        str.append(s.substring(0, k))
        return str.toString()
    }
}