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 <= 1001 <= k <= 10^4s consists only of lowercase English letters.
Hints for Find the Encrypted String
Hint 1
The character that lands at position
iis the one originally at(i + k) mod n.Hint 2
kcan exceed the length, which is exactly what the modulo handles.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
- For each
ifrom 0 ton - 1, appends[(i + k) mod n]. - 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.
kmay be far larger than the string; reduce it withmod n.- The direction is forward: position
ireads fromi + k, noti - 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