Find the Encrypted String

Easy
1011.

Find the Encrypted String

Find the Encrypted String is an easy coding problem on strings and simulation, asked in Amazon, Google and TCS interviews.

Encrypt s by replacing every character with the character k places after it, wrapping around to the start of the string when you run off the end.

Return the encrypted string.

Example 1

Input
s = "codekairo", k = 3
Output
ekairocod
Explanation
Position 0 takes the character at position 3, and so on around the ring.

Example 2

Input
s = "dart", k = 3
Output
tdar

Example 3

Input
s = "aaa", k = 1
Output
aaa
Explanation
Every character is the same, so rotating changes nothing.

Constraints

  • 1 <= s.length <= 100
  • 1 <= k <= 10^4
  • s consists only of lowercase English letters.
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {string} s
 * @param {number} k
 * @return {string}
 */
var getEncryptedString = function(s, k) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Find the Encrypted String — Solution in Python, Java & C++

Find the Encrypted String solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.

Find the Encrypted String is an easy coding problem on strings and simulation, asked in Amazon, Google and TCS interviews.

  • Difficulty: Easy
  • Topics: Strings, Simulation
  • Asked at: Amazon, Google, TCS
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

Encrypt s by replacing every character with the character k places after it, wrapping around to the start of the string when you run off the end.

Return the encrypted string.

Example 1

Input: s = "codekairo", k = 3
Output: ekairocod
Explanation: Position 0 takes the character at position 3, and so on around the ring.

Example 2

Input: s = "dart", k = 3
Output: tdar

Example 3

Input: s = "aaa", k = 1
Output: aaa
Explanation: Every character is the same, so rotating changes nothing.

Constraints

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

Hints for Find the Encrypted String

  1. Hint 1

    The character that lands at position i is the one originally at (i + k) mod n.

  2. Hint 2

    k can exceed the length, which is exactly what the modulo handles.

  3. Hint 3

    This is a left rotation of the string by k mod n.

How to solve Find the Encrypted String

The answer at index i is s[(i + k) mod n] — a left rotation by k mod n. Build the result index by index.

Approach

  1. For each i from 0 to n - 1, append s[(i + k) mod n].
  2. Return the accumulated string.

Why it works

The modulo is doing all the work: k can be a hundred times the string's length, and taking it modulo n collapses every one of those wraps into the single rotation they amount to. Rotating one character at a time in a loop would be O(n · k) and needlessly slow for large k.

Complexity

  • Time — O(n)
  • Space — O(n) for the output

Pitfalls

  • Shifting is on positions, not on the letters themselves — this is not a Caesar cipher.
  • k may be far larger than the string; reduce it with mod n.
  • The direction is forward: position i reads from i + k, not i - k.

Reference solution

Python

def getEncryptedString(s: str, k: int) -> str:
    n = len(s)
    return "".join(s[(i + k) % n] for i in range(n))

Java

public static String getEncryptedString(String s, int k) {
    int n = s.length();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < n; i++) sb.append(s.charAt((i + k) % n));
    return sb.toString();
}

C++

string getEncryptedString(string s, int k) {
    int n = (int) s.size();
    string out;
    out.reserve((size_t) n);
    for (int i = 0; i < n; i++) out += s[(i + k) % n];
    return out;
}

JavaScript

var getEncryptedString = function(s, k) {
    var n = s.length, out = "";
    for (var i = 0; i < n; i++) out += s.charAt((i + k) % n);
    return out;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 515 strings problems · the whole catalogue

Learn the technique: Strings