📜  解密通过重复第 i 个字符i 次加密的字符串

📅  最后修改于: 2022-05-13 01:57:08.208000             🧑  作者: Mango

解密通过重复第 i 个字符i 次加密的字符串

给定一个加密字符串str和加密算法,任务是解密该字符串。加密算法如下:
字符串的一个字符将在加密字符串中重复一次第二个字符将重复两次,……,第n字符将重复n次。例如,字符串“abcd”将被加密为“abbcccdddd”

例子:

方法:初始化i = 0并打印str[i]
更新i = i + 1并打印str[i] ,然后在i < length(str)时更新i = i + 2并打印str[i]等等。

下面是上述方法的实现:

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


Javascript


输出:
geeks