📜  根据给定的加密解密技术,在 Matrix 的帮助下解密编码的字符串

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

根据给定的加密解密技术,在 Matrix 的帮助下解密编码的字符串

给定一个长度为N的编码(或加密)字符串S ,一个整数M 。任务是解密加密的字符串并打印出来。加密和解密技术如下:

例子

方法:首先,找到列数。然后,开始穿越。请按照以下步骤解决问题:

  • 对于每一列,从第 i 行开始直到字符串的长度,每次遍历后将迭代值增加到column+1
  • 因为,这里的遍历是对角进行的,这里的下一个对角字符将在列号加一之后。
  • 在遍历时将字符添加到空字符串中。然后检查字符串末尾是否有空格,如果有,则将其删除。

最后,打印那个字符串,这将是解码后的字符串/desired 字符串。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the desire string
string decodeString(string encodedText, int rows)
{
 
    // Stores the length of the string
    int len = encodedText.size();
 
    // Stores the number of columns
    int cols = len / rows;
 
    // Declaring an empty string
    string res;
    for (int i = 0; i < cols; ++i)
        for (int j = i; j < len; j += cols + 1)
            // Adding the characters
            // into the empty string
            res += encodedText[j];
 
    // If their any space at the end,
    // delete it
    while (res.back() == ' ') {
        res.pop_back();
    }
    return res;
}
 
// Driver Code
int main()
{
 
    string S = "GSRE   E  K   EFGS   KOE";
    int row = 4;
    cout << decodeString(S, row) << endl;
    return 0;
}


Java
// Java program for the above approach
class GFG {
 
  // Function to find the desire string
  static String decodeString(String encodedText, int rows) {
 
    // Stores the length of the string
    int len = encodedText.length();
 
    // Stores the number of columns
    int cols = len / rows;
 
    // Declaring an empty string
    String res = "";
    for (int i = 0; i < cols; ++i)
    {
      for (int j = i; j < len; j += cols + 1)
      {
        // Adding the characters
        // into the empty string
        res += encodedText.charAt(j);
      }
    }
 
    // If their any space at the end,
    // delete it
    while (res.charAt(res.length() - 1) == ' ') {
      res = res.substring(0, res.length() - 2);
    }
    return res;
  }
 
  // Driver Code
  public static void main(String args[]) {
    String S = "GSRE   E  K   EFGS   KOE";
    int row = 4;
 
    System.out.println(decodeString(S, row));
 
  }
}
 
// This code is contributed by gfgking


Python3
# Python code for the above approach
 
# Function to find the desire string
def decodeString(encodedText, rows):
 
    # Stores the length of the string
    _len = len(encodedText)
 
    # Stores the number of columns
    cols = _len // rows
 
    # Declaring an empty string
    res = "";
    for i in range(cols):
        for j in range(i, _len, cols + 1):
         
            # Adding the characters
            # into the empty string
            res += encodedText[j];
 
    # If their any space at the end,
    # delete it
    while (res[len(res) - 1] == ' '):
        res = res[0: len(res) - 1];
    return res;
 
 
# Driver Code
S = "GSRE   E  K   EFGS   KOE";
row = 4;
print(decodeString(S, row))
 
# This code is contributed by Saurabh Jaiswal


C#
// C# program for the above approach
using System;
class GFG
{
 
// Function to find the desire string
static string decodeString(string encodedText, int rows)
{
 
    // Stores the length of the string
    int len = encodedText.Length;
 
    // Stores the number of columns
    int cols = len / rows;
 
    // Declaring an empty string
    string res = "";
    for (int i = 0; i < cols; ++i) {
        for (int j = i; j < len; j += cols + 1) {
            // Adding the characters
            // into the empty string
            res += encodedText[j];
        }
    }
  
    // If their any space at the end,
    // delete it
    while (res[res.Length - 1] == ' ') {
        res = res.Remove(res.Length - 1);
    }
    return res;
}
 
// Driver Code
public static void Main()
{
    string S = "GSRE   E  K   EFGS   KOE";
    int row = 4;
 
    Console.Write(decodeString(S, row));
 
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
GEEKS FOR GEEKS

时间复杂度 O(M*(M/col)),接近 O(字符串长度)
辅助空间 O(N)