# Restore original String from given Encrypted String by the given operations

Given a string str and a positive integer N, the task is to reverse N characters and skip N characters until the end of the string to generate the encrypted message.

Examples:

Input: str = “ihTs suohld ebeas!y”, K = 3
Output: This should be easy!
Explanation:
Reverse “ihT” -> “Thi”
“s” remains the same
“uoh” -> “hou”, and so on.

Input: str = “!ysae eb dluohs sihT”, K = 30
Output: This should be easy!
Explanation:
Since 30 is larger than the length of the given string(= 20), just print the reverse of the string.

Approach: Follow the steps below to solve the problem:

• Traverse the given string.
• Increment the iterator by 2 * N.
• Reverse N characters step by step.

Below is the implementation of the above approach:

## C++

 `// C++ Program to implement` `// the above approach` `#include ` `using` `namespace` `std;`   `// Function to decrypt and print the` `// original strings` `int` `decryptString(string s, unsigned ``int` `N)` `{`   `    ``for` `(unsigned ``int` `i = 0; i < s.size();` `         ``i += 2 * N) {` `        ``auto` `end = s.begin() + i + N;` `    `    `        ``// If length is exceeded` `        ``if` `(i + N > s.size())` `            ``end = s.end();`   `        ``// Reverse the string` `        ``reverse(s.begin() + i, end);` `          `  `      `  `    ``}`   `    ``cout << s << endl;` `}`   `// Driver Code` `int` `main()` `{` `    ``string s = ``"ihTs suohld  ebeas!y"``;` `    ``unsigned ``int` `N = 3;` `    ``decryptString(s, N);`   `    ``return` `0;` `}`

## Java

 `// Java Program to implement` `// the above approach` `import` `java.io.*;`   `class` `GFG {`   `  ``// utility function to reverse the string from start to end -1 index` `  ``static` `String reverse_(String s ,``int` `start ,``int` `end)` `  ``{` `    ``String str=``""``;` `    ``for``(``int` `i = ``0``;i=start;i--)` `      ``str += s.charAt(i);` `    ``for``(``int` `i = end; i s.length())` `        ``end = s.length();`   `      ``// Reverse the string` `      ``s = reverse_(s, i, end);` `    ``}`   `    ``System.out.println(s);` `  ``}`   `  ``public` `static` `void` `main(String[] args)` `  ``{` `    ``String s = ``"ihTs suohld  ebeas!y"``;` `    ``int` `N = ``3``;` `    ``decryptString(s, N);` `  ``}` `}`   `// This code is contributed by AKSHAY TRIPATHI`

## C#

 `using` `System;`   `class` `GFG` `{` `    ``// utility function to reverse the string from start to end -1 index` `    ``static` `string` `Reverse_(``string` `s, ``int` `start, ``int` `end)` `    ``{` `        ``string` `str = ``""``;` `        ``for` `(``int` `i = 0; i < start; i++)` `            ``str += s[i];` `        ``for` `(``int` `i = end - 1; i >= start; i--)` `            ``str += s[i];` `        ``for` `(``int` `i = end; i < s.Length; i++)` `            ``str += s[i];` `        ``return` `str;` `    ``}` `    ``// Function to decrypt and print the` `    ``// original strings` `    ``static` `void` `DecryptString(``string` `s, ``int` `N)` `    ``{` `        ``for` `(``int` `i = 0; i < s.Length; i += 2 * N)` `        ``{` `            ``int` `end = i + N;`   `            ``// If length is exceeded` `            ``if` `(i + N > s.Length)` `                ``end = s.Length;`   `            ``// Reverse the string` `            ``s = Reverse_(s, i, end);` `        ``}`   `        ``Console.WriteLine(s);` `    ``}`   `    ``public` `static` `void` `Main(``string``[] args)` `    ``{` `        ``string` `s = ``"ihTs suohld  ebeas!y"``;` `        ``int` `N = 3;` `        ``DecryptString(s, N);` `    ``}` `}`

## Python3

 `# Python3 program to implement` `# the above approach`   `# Function to decrypt and print the` `# original strings` `def` `decryptString(s, N):` `    `  `    ``for` `i ``in` `range``(``0``, ``len``(s), ``2` `*` `N):` `        ``if` `(i ``+` `N < ``len``(s)):` `            ``end ``=` `s[i ``+` `N]` `            `  `        ``# If length is exceeded` `        ``if` `(i ``+` `N > ``len``(s)):` `            ``end ``=` `s[``-``1``]`   `        ``# Reverse the string` `        ``if` `(i ``=``=` `0``):` `            ``s ``=` `s[i ``+` `N ``-` `1``::``-``1``] ``+` `s[i ``+` `N:]` `        ``else``:` `            ``s ``=` `s[:i] ``+` `s[i ``+` `N ``-` `1``:i ``-` `1``:``-``1``] ``+` `s[i ``+` `N:]`   `    ``print``(s)`   `# Driver Code` `if` `__name__ ``=``=` `"__main__"``:`   `    ``s ``=` `"ihTs suohld  ebeas!y"` `    ``N ``=` `3` `    `  `    ``decryptString(s, N)`   `# This code is contributed by ukasp`

## Javascript

 `// JavascriptProgram to implement` `// the above approach`   `//utility function to reverse the string from start to end -1 index` `function` `reverse_(s , start , end)` `{` `    ``end -= 1;` `    ``while``(start s.length)` `            ``end = s.length;`   `        ``// Reverse the string` `        ``s = reverse_(s, i, end);` `          `  `    ``}`   `    ``console.log(s);` `}`   `// Driver Code` `    ``var` `s = ``"ihTs suohld  ebeas!y"``;` `    ``var` `N = 3;` `    ``decryptString(s, N);` ` `  ` ``// This code is contributed by Abhijeet Kumar(abhijeet19403)`

Output:

`This should be easy!`

Time Complexity: O(N)
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next