📌  相关文章
📜  通过删除重复出现来解码给定的字符串

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

通过删除重复出现来解码给定的字符串

给定编码字符串str使得'a'被写入 1 次, 'b'被写入 2 次,依此类推,直到'z'被写入 26 次,任务是解码给定的字符串str

注意:字母可能包含空格和标点符号,所以不要忽略它们。

例子:

方法:给定的问题可以通过迭代给定的字符串str来解决,对于每个字符并将该字符推入输出字符串并将相应的位置向前移动以检查另一个字符。请按照以下步骤解决问题:

  • 初始化一个变量,比如将输出作为存储结果字符串。
  • 定义一个函数findRepetition(char c)并执行以下步骤:
    • 如果c的值在az 的范围内,则返回c-'a'的值。
    • 否则,如果c的值在AZ的范围内,则返回c-'Z'的值。
    • 否则,返回0
  • 使用变量i迭代范围[0, N]并执行以下步骤:
    • 将字符串str第 i字符压入字符串output
    • 调用函数findRepetition(str[i])来计算第i索引必须向前移动的步数。
  • 执行上述步骤后,将字符串输出打印为结果字符串。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the appearances
// of each character
int findRepitition(char a)
{
    // If the character is lower case
    if (a <= 'z' && a >= 'a') {
        return a - 'a';
    }
 
    // If the character is uppercase
    else if (a <= 'Z' && a >= 'A') {
        return a - 'A';
    }
 
    // If the character is a
    // punctuation mark
    return 0;
}
 
// Function to decode the given encoded
// string
void decodeString(string str)
{
    string output = "";
 
    // Iterate the given string str
    for (int i = 0; i < str.length(); i++) {
 
        output.push_back(str[i]);
 
        // Find the index of the next
        // character to be printed
        i += findRepitition(str[i]);
    }
 
    cout << "Decrypted code is {"
         << output << "}" << endl;
}
 
// Driver Code
int main()
{
    string str = "abbbb acccdddd";
    decodeString(str);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to count the appearances
// of each character
static int findRepitition(char a)
{
     
    // If the character is lower case
    if (a <= 'z' && a >= 'a')
    {
        return a - 'a';
    }
 
    // If the character is uppercase
    else if (a <= 'Z' && a >= 'A')
    {
        return a - 'A';
    }
 
    // If the character is a
    // punctuation mark
    return 0;
}
 
// Function to decode the given encoded
// String
static void decodeString(String str)
{
    String output = "";
 
    // Iterate the given String str
    for(int i = 0; i < str.length(); i++)
    {
        output += (str.charAt(i));
 
        // Find the index of the next
        // character to be printed
        i += findRepitition(str.charAt(i));
    }
    System.out.print("Decrypted code is {" +
                     output + "}" + "\n");
}
 
// Driver Code
public static void main(String[] args)
{
    String str = "abbbb acccdddd";
     
    decodeString(str);
}
}
 
// This code is contributed by umadevi9616


Python3
# Python3 program for the above approach
 
# Function to count the appearances
# of each character
def findRepetition(a):
   
  # If the character is lower case
    if a <= 'z' and a >= 'a':
        return ord(a)-97
       
    # If the character is uppercase
    elif a <= 'Z' and a >= 'A':
        return ord(a)-65
    else:
       
     # If the character is a
    # punctuation mark
        return 0
 
# Function to decode the given encoded
# string
def decodeString(str_):
    output = ""
    i = 0
     
    # Iterate the given string str
    while(i < len(str_)):
        output += str_[i]
                 
        # Find the index of the next
        # character to be printed
        i += findRepetition(str_[i]) + 1
    print("Decrypted code is {" + output + "}")
 
# Driver code
str_ = "abbbb acccdddd"
decodeString(str_)
 
# This code is contributed by Parth Manchanda


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to count the appearances
// of each character
static int findRepitition(char a)
{
    // If the character is lower case
    if (a <= 'z' && a >= 'a') {
        return (int)a - 97;
    }
 
    // If the character is uppercase
    else if (a <= 'Z' && a >= 'A') {
        return (int)a - 65;
    }
 
    // If the character is a
    // punctuation mark
    return 0;
}
 
// Function to decode the given encoded
// string
static void decodeString(string str)
{
    string output = "";
 
    // Iterate the given string str
    for (int i = 0; i < str.Length; i++) {
 
        output += str[i];
 
        // Find the index of the next
        // character to be printed
        i += findRepitition(str[i]);
    }
 
    Console.Write("Decrypted code is {" + output + "}");
}
 
// Driver Code
public static void Main()
{
    string str = "abbbb acccdddd";
    decodeString(str);
}
}
 
// This code is contributed by ipg2016107.


Javascript


输出:
Decrypted code is {abb acd}

时间复杂度: O(N)
辅助空间: O(N)