Related Articles
Decrypt a string encrypted by repeating i-th character i times
• Last Updated : 12 Feb, 2019

Given an encrypted string str and the encryption algorithm, the task is to decrypt the string. The encryption algorithm is as follows:
The 1st character of the string will be repeated once in the encrypted string, the 2nd character will be repeated twice, …, nth character will be repeated n times. For example, string “abcd” will be encrypted as “abbcccdddd”.

Examples:

Input: str = “geeeeekkkksssss”
Output: geeks

Input: str = “abbcccdddd”
Output: abcd

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Initialize i = 0 and print str[i].
Update i = i + 1 and print str[i] then update i = i + 2 and print str[i] and so on while i < length(str).

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the decrypted string ` `string decryptString(string str, ``int` `n) ` `{ ` ` `  `    ``// Initial jump will be 1 ` `    ``int` `i = 0, jump = 1; ` `    ``string decryptedStr = ``""``; ` ` `  `    ``while` `(i < n) { ` `        ``decryptedStr += str[i]; ` `        ``i += jump; ` ` `  `        ``// Increment jump by 1 with every character ` `        ``jump++; ` `    ``} ` ` `  `    ``return` `decryptedStr; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string str = ``"geeeeekkkksssss"``; ` `    ``int` `n = str.length(); ` `    ``cout << decryptString(str, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `class` `GFG ` `{ ` ` `  `// Function to return the decrypted string ` `static` `String decryptString(String str, ``int` `n) ` `{ ` ` `  `    ``// Initial jump will be 1 ` `    ``int` `i = ``0``, jump = ``1``; ` `    ``String decryptedStr = ``""``; ` ` `  `    ``while` `(i < n)  ` `    ``{ ` `        ``decryptedStr += str.charAt(i); ` `        ``i += jump; ` ` `  `        ``// Increment jump by 1 with every character ` `        ``jump++; ` `    ``} ` ` `  `    ``return` `decryptedStr; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``String str = ``"geeeeekkkksssss"``; ` `    ``int` `n = str.length(); ` `    ``System.out.println(decryptString(str, n)); ` `} ` `} ` ` `  `// This code is contributed by Code_Mech `

## Python3

 `# Python 3 implementation of the approach ` ` `  `# Function to return the decrypted string ` `def` `decryptString(``str``, n): ` `     `  `    ``# Initial jump will be 1 ` `    ``i ``=` `0` `    ``jump ``=` `1` `    ``decryptedStr ``=` `"" ` ` `  `    ``while` `(i < n): ` `        ``decryptedStr ``+``=` `str``[i]; ` `        ``i ``+``=` `jump ` ` `  `        ``# Increment jump by 1 with  ` `        ``# every character ` `        ``jump ``+``=` `1` ` `  `    ``return` `decryptedStr ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``str` `=` `"geeeeekkkksssss"` `    ``n ``=` `len``(``str``) ` `    ``print``(decryptString(``str``, n)) ` ` `  `# This code is contributed by ` `# Surendra_Gangwar `

## C#

 `// C# implementation of the approach ` `using` `System;  ` ` `  `class` `GFG ` `{ ` ` `  `// Function to return the decrypted string ` `static` `string` `decryptString(``string` `str, ``int` `n) ` `{ ` ` `  `    ``// Initial jump will be 1 ` `    ``int` `i = 0, jump = 1; ` `    ``string` `decryptedStr = ``""``; ` ` `  `    ``while` `(i < n)  ` `    ``{ ` `        ``decryptedStr += str[i]; ` `        ``i += jump; ` ` `  `        ``// Increment jump by 1 with every character ` `        ``jump++; ` `    ``} ` ` `  `    ``return` `decryptedStr; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main() ` `{ ` `    ``string` `str = ``"geeeeekkkksssss"``; ` `    ``int` `n = str.Length; ` `    ``Console.Write(decryptString(str, n)); ` `} ` `} ` ` `  `// This code is contributed by ita_c `

## PHP

 ` `

Output:

```geeks
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :