3210. Find the Encrypted String LeetCode Solution

In this guide, you will get 3210. Find the Encrypted String LeetCode Solution with the best time and space complexity. The solution to Find the Encrypted String problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find the Encrypted String solution in C++
  4. Find the Encrypted String solution in Java
  5. Find the Encrypted String solution in Python
  6. Additional Resources
3210. Find the Encrypted String LeetCode Solution image

Problem Statement of Find the Encrypted String

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.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

3210. Find the Encrypted String LeetCode Solution in C++

class Solution {
 public:
  string getEncryptedString(string s, int k) {
    k %= s.length();
    return s.substr(k) + s.substr(0, k);
  }
};
/* code provided by PROGIEZ */

3210. Find the Encrypted String LeetCode Solution in Java

class Solution {
  public String getEncryptedString(String s, int k) {
    k %= s.length();
    return s.substring(k) + s.substring(0, k);
  }
}
// code provided by PROGIEZ

3210. Find the Encrypted String LeetCode Solution in Python

class Solution:
  def getEncryptedString(self, s: str, k: int) -> str:
    k %= len(s)
    return s[k:] + s[0:k]
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.