Encrypt a string by repeating i-th character i times

Given string str, the task is to encrypt the string with the given encryption algorithm. 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.
Examples:
Input: str = "zambiatek" Output: geeeeekkkksssss Input: str = "abcd" Output: abbcccdddd
Approach: Initialize cnt = 1 and start traversing the string character by character. Repeat the current character cnt number of times then update cnt = cnt + 1 and get to the next character. Repeat these steps for every character of the string.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;// Function to return the encrypted stringstring encryptString(string str, int n){ int i = 0, cnt = 0; string encryptedStr = ""; while (i < n) { // Number of times the current // character will be repeated cnt = i + 1; // Repeat the current character // in the encrypted string while (cnt--) encryptedStr += str[i]; i++; } return encryptedStr;}// Driver codeint main(){ string str = "zambiatek"; int n = str.length(); cout << encryptString(str, n); return 0;} |
Java
// Java implementation of the approachclass GFG {// Function to return the encrypted Stringstatic String encryptString(String str, int n){ int i = 0, cnt = 0; String encryptedStr = ""; while (i < n) { // Number of times the current // character will be repeated cnt = i + 1; // Repeat the current character // in the encrypted String while (cnt-- >0) encryptedStr += str.charAt(i); i++; } return encryptedStr;}// Driver codepublic static void main(String[] args) { String str = "zambiatek"; int n = str.length(); System.out.println(encryptString(str, n));}}// This code has been contributed by 29AjayKumar |
Python3
# Python3 implementation of the approach # Function to return the encrypted string def encryptString(string, n): i, cnt = 0, 0 encryptedStr = "" while i < n: # Number of times the current # character will be repeated cnt = i + 1 # Repeat the current character # in the encrypted string while cnt > 0: encryptedStr += string[i] cnt -= 1 i += 1 return encryptedStr # Driver code if __name__ == "__main__": string = "zambiatek" n = len(string) print(encryptString(string, n))# This code is contributed # by Rituraj Jain |
C#
// C# implementation of the approachusing System;class GFG{ // Function to return the encrypted Stringstatic String encryptString(String str, int n){ int i = 0, cnt = 0; String encryptedStr = ""; while (i < n) { // Number of times the current // character will be repeated cnt = i + 1; // Repeat the current character // in the encrypted String while (cnt-- >0) encryptedStr += str[i]; i++; } return encryptedStr;}// Driver codestatic public void Main (){ String str = "zambiatek"; int n = str.Length; Console.WriteLine(encryptString(str, n));}}// This code is contributed by ajit |
PHP
<?php// PHP implementation of the approach // Function to return the encrypted string function encryptString($str, $n) { $i = 0 ; $cnt = 0; $encryptedStr = ""; while ($i < $n) { // Number of times the current // character will be repeated $cnt = $i + 1; // Repeat the current character // in the encrypted string while ($cnt--) $encryptedStr .= $str[$i]; $i++; } return $encryptedStr; } // Driver code $str = "zambiatek"; $n = strlen($str);echo encryptString($str, $n); // This code is contributed by Ryuga?> |
Javascript
<script> // Javascript implementation of the approach // Function to return the encrypted String function encryptString(str, n) { let i = 0, cnt = 0; let encryptedStr = ""; while (i < n) { // Number of times the current // character will be repeated cnt = i + 1; // Repeat the current character // in the encrypted String while (cnt-- >0) encryptedStr += str[i]; i++; } return encryptedStr; } let str = "zambiatek"; let n = str.length; document.write(encryptString(str, n)); </script> |
Output:
geeeeekkkksssss
Time Complexity: O(n) // n is the length of the string
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 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



