Find the Encrypted String
EASYDescription
You are given a string s and an integer k. Encrypt the string using the following algorithm:
- For each character
cins, replacecwith thekthcharacter aftercin 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 <= 1001 <= k <= 104sconsists only of lowercase English letters.
Approaches
Checkout 2 different approaches to solve Find the Encrypted String. Click on different approaches to view the approach and algorithm in detail.
String Slicing and Concatenation
This approach recognizes that the described encryption algorithm is equivalent to a left circular shift (or rotation) of the string. The encrypted string can be constructed by splitting the original string at the k-th position (cyclically) and concatenating the two resulting parts in reverse order.
Algorithm
- Let
nbe the length of the input strings. - The rotation is cyclic, so rotating by
kis identical to rotating byk % n. Calculate the effective rotation amount,rot = k % n. - A left rotation by
rotpositions means the substring starting at indexrotbecomes the new prefix of the string. - This new prefix is
s.substring(rot). - The original prefix of the string,
s.substring(0, rot), becomes the new suffix. - Concatenate these two parts:
s.substring(rot) + s.substring(0, rot)to get the final encrypted string.
The core idea is to treat the encryption as a string rotation problem, which can be solved efficiently using substring operations.
-
Algorithm:
- Let
nbe the length of the input strings. - The rotation is cyclic, so rotating by
kis identical to rotating byk % n. Calculate the effective rotation amount,rot = k % n. - A left rotation by
rotpositions means the substring starting at indexrotbecomes the new prefix of the string. - This new prefix is
s.substring(rot). - The original prefix of the string,
s.substring(0, rot), becomes the new suffix. - Concatenate these two parts:
s.substring(rot) + s.substring(0, rot)to get the final encrypted string.
- Let
-
Code Snippet:
class Solution { public String getEncryptedString(String s, int k) { int n = s.length(); int rot = k % n; String rightPart = s.substring(rot); String leftPart = s.substring(0, rot); return rightPart + leftPart; } }
Complexity Analysis
Pros and Cons
- Code is very concise and easy to understand.
- Effectively uses high-level, built-in string manipulation functions.
- Can be slightly less performant than manual construction. It involves creating at least two intermediate string objects before the final result, leading to extra memory allocations and character copying.
Code Solutions
Checking out 3 solutions in different languages for Find the Encrypted String. Click on different languages to view the code.
class Solution {
public
String getEncryptedString(String s, int k) {
char[] cs = s.toCharArray();
int n = cs.length;
for (int i = 0; i < n; ++i) {
cs[i] = s.charAt((i + k) % n);
}
return new String(cs);
}
}
Video Solution
Watch the video walkthrough for Find the Encrypted String
Similar Questions
5 related questions you might find useful
Data Structures:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.