Talentd
FleetCode
Make and follow sheets.Sign in

Find the Encrypted String solution and explanation

EasyString4 min read67.9% acceptanceUpdated 9 Apr 2026
3210 Find the Encrypted String || How to 🤔 in Interview || Cyclic Order (i+k)%nAyush Rao · 4:21 · 430 viewsWatch on YouTube
8 more videos on this problem
  1. 3210 Find the Encrypted String || How to 🤔 in Interview || Cyclic Order (i+k)%nAyush Rao · 4:21 · 430 views
  2. 3210. Find the Encrypted String (Leetcode Easy)Programming Live with Larry · 3:13 · 333 views
  3. leetcode 3210 : encrypt string : python solutionleetcode blind 75 · 4:21 · 212 views
  4. 3210. Find the Encrypted String #java #coding #javainterviewquestions #leetcode #dsa #stringsCodeVia · 1:52 · 132 views
  5. Find the Encrypted String | Leetcode weekly-contest-405 | Java SolutionDeveloper Coder · 4:28 · 122 views
  6. 3210. Find the Encrypted String | Cyclic stringTech Courses · 2:28 · 111 views
  7. Weekly Contest 405 | Leetcode 3210, 3211 and 3212 | First 3 ProblemsAmit Choraria · 23:36 · 43 views
  8. Leetcode easy problem 3210. Find the Encrypted String | devops python practiceADevOpsEngineer · 1:02 · 6 views
  9. leetcode contest 405 3210. Find the Encrypted Stringcomicfans44 · 1:15 · 5 views

The problem

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.

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 <= 100
  • 1 <= k <= 104
  • s consists only of lowercase English letters.

Approach overview

Problem Overview: You receive a string s and an integer k. Each character in the encrypted string comes from index (i + k) % n of the original string, where n is the length of s. The task is to construct the resulting string after this cyclic shift.

Approach 1: Cyclic Reordering with Modulo (Time: O(n), Space: O(n))

The direct solution builds a new string by iterating through every index i from 0 to n-1. For each position, compute the source index using (i + k) % n and append that character to the result. The modulo operation handles wrap‑around when the index exceeds the string length. This approach is straightforward, avoids complex pointer manipulation, and works in linear time because each character is processed exactly once. It relies on basic string traversal and modular arithmetic.

Approach 2: Rotation with In-place Modification (Time: O(n), Space: O(1))

The encryption rule effectively performs a left rotation of the string by k positions. Instead of creating a new string, you can rotate the characters in place using the classic three‑reversal technique. First normalize k using k % n. Reverse the first k characters, reverse the remaining n-k, then reverse the entire sequence. After these operations, the characters appear in the same order as the encrypted result. This method is useful when memory usage matters or when you want to practice in-place manipulation techniques common in array and string rotation problems.

Recommended for interviews: The cyclic modulo approach is usually what interviewers expect first because it directly follows the formula in the problem statement and clearly demonstrates understanding of cyclic indexing. Mentioning the in-place rotation variant shows deeper familiarity with string/array manipulation and space optimization techniques.

Approach 1: Cyclic Reordering with Modulo

This approach involves rearranging the string using the modulo operation to handle the cyclic nature of the problem. For each character, we find its new position by moving k steps forward, accounting for any wrap-arounds using modulo division.

def encrypt_string(s, k):
    len_s = len(s)
    encrypted = [''] * len_s
    for i in range(len_s):
        encrypted[(i + k) % len_s] = s[i]
    return ''.join(encrypted)

s = "dart"
k = 3
print(encrypt_string(s, k))
public class EncryptString {
    public static void main(String[] args) {
        String s = "dart";
        int k = 3;
        System.out.println(encryptString(s, k));
    }

    public static String encryptString(String s, int k) {
        int len = s.length();
        char[] encrypted = new char[len];
        for (int i = 0; i < len; i++) {
            encrypted[(i + k) % len] = s.charAt(i);
        }
        return new String(encrypted);
    }
}
#include <iostream>
#include <string>

std::string encryptString(const std::string& s, int k) {
    int len = s.length();
    std::string encrypted(len, ' ');
    for (int i = 0; i < len; ++i) {
        encrypted[(i + k) % len] = s[i];
    }
    return encrypted;
}

int main() {
    std::string s = "dart";
    int k = 3;
    std::cout << encryptString(s, k);
    return 0;
}
function encryptString(s, k) {
  let len = s.length;
  let encrypted = new Array(len);
  for (let i = 0; i < len; i++) {
    encrypted[(i + k) % len] = s[i];
  }
  return encrypted.join('');
}

let s = "dart";
let k = 3;
console.log(encryptString(s, k));
#include <stdio.h>
#include <string.h>

char* encryptString(char* s, int k) {
    int len = strlen(s);
    char* encrypted = (char*)malloc(len + 1);
    for (int i = 0; i < len; ++i) {
        int newPos = (i + k) % len;
        encrypted[newPos] = s[i];
    }
    encrypted[len] = '\0';
    return encrypted;
}

int main() {
    char s[] = "dart";
    int k = 3;
    char* encrypted = encryptString(s, k);
    printf("%s", encrypted);
    free(encrypted);
    return 0;
}
using System;

class Program {
    static void Main() {
        string s = "dart";
        int k = 3;
        Console.WriteLine(EncryptString(s, k));
    }

    static string EncryptString(string s, int k) {
        int len = s.Length;
        char[] encrypted = new char[len];
        for (int i = 0; i < len; i++) {
            encrypted[(i + k) % len] = s[i];
        }
        return new string(encrypted);
    }
}

The Python solution constructs a list of empty strings (for immutable-like behavior) and populates it through modulo indexing, finally joining the list into a result string.

Complexity

Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(n), for the encrypted string list.

Try this approach in the editor

Approach 2: Rotation with In-place Modification

This approach modifies the original string by rotating segments of it in place. We use slicing to concatenate segments of the string in the correct order, a technique viable due to the small size constraint of the input.

def encrypt_string(s, k):
    k = k % len(s)
    if k == 0:
        return s
    return s[-k:] + s[:-k]

s = "dart"
k = 3
print(encrypt_string(s, k))
function encryptString(s, k) {
  k = k % s.length;
  if (k === 0) return s;
  return s.slice(-k) + s.slice(0, -k);
}

let s = "dart";
let k = 3;
console.log(encryptString(s, k));

In Python, we utilize string slicing to achieve the rotation. We adjust k via modulus to handle cases where k exceeds the string length, returning the desired segments rearranged.

Complexity

Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(n), as a new string is created for the result.

Try this approach in the editor

Approach 3: Simulation

We can use the simulation method. For the i^{th} character of the string, we replace it with the character at position (i + k) bmod n of the string.

The time complexity is O(n), and the space complexity is O(n). Here, n is the length of the string s.

function getEncryptedString(s: string, k: number): string {
    const cs: string[] = [];
    const n = s.length;
    for (let i = 0; i < n; ++i) {
        cs[i] = s[(i + k) % n];
    }
    return cs.join('');
}
func getEncryptedString(s string, k int) string {
	cs := []byte(s)
	for i := range s {
		cs[i] = s[(i+k)%len(s)]
	}
	return string(cs)
}
Try this approach in the editor

Complexity comparison

ApproachComplexity
Cyclic Reordering with Modulo

Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(n), for the encrypted string list.

Rotation with In-place Modification

Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(n), as a new string is created for the result.

Simulation–

Complexity analysis

ApproachTimeSpaceWhen to Use
Cyclic Reordering with ModuloO(n)O(n)Best for clarity and typical interview solutions; simple linear scan with direct indexing
Rotation with In-place ModificationO(n)O(1)When minimizing extra memory or demonstrating in-place rotation techniques

Frequently asked questions

Is Find the Encrypted String easy or hard?

Find the Encrypted String is categorized as an Easy problem on LeetCode. The main challenge is recognizing that the encryption rule is simply a cyclic rotation using modular indexing.

Find the Encrypted String Python/Java solution

In Python or Java, iterate through the string and compute the encrypted character using (i + k) % n. Append characters to a result builder such as a Python list or Java StringBuilder, then convert it to a final string.

How to solve Find the Encrypted String in O(n)?

Iterate through indices from 0 to n-1 and compute the source position using (i + k) % n. Append the character from that position in the original string to the result. This single pass over the string guarantees linear time complexity.

What is the best approach for Find the Encrypted String?

The most practical approach uses cyclic indexing with the formula (i + k) % n while building a new result string. It runs in O(n) time and O(n) space and directly mirrors the problem definition, making it easy to implement and explain during interviews.

Is Find the Encrypted String asked at Google/Amazon/Meta?

The exact problem is relatively new on LeetCode, but the underlying concept—string rotation and cyclic indexing—appears frequently in interviews at companies like Amazon, Google, and Meta. Variants often involve rotating arrays or strings efficiently.

What data structure is used in Find the Encrypted String?

The problem primarily uses basic string manipulation. The key technique is modular arithmetic for cyclic indexing, sometimes combined with array-style operations when performing in-place rotation.

What is the time complexity of Find the Encrypted String?

The optimal solution runs in O(n) time because each character of the string is processed exactly once. Space complexity is O(n) when constructing a new string, or O(1) if the string is rotated in place using reversal operations.

Comments

Now write it yourselfClose this page and solve Find the Encrypted String in the editor, without looking.
Practise Find the Encrypted String