📌  相关文章
📜  将每个字母数字字符的最后一次出现重复到它们在字符系列时间中的位置

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

将每个字母数字字符的最后一次出现重复到它们在字符系列时间中的位置

给定一个大小为N的字符串str[] ,任务是以这样一种方式对其进行编码,即每个字符的最后一次出现与它在其家族中的位置一样长。由于 'a' 是其家族的第一个字符(小写字母),所以它将保持为 'a',但 'b' 变为 'bb','D' 变为 'DDDD' 等等。在数字字符的情况下,字符出现的次数与其值一样多。因为'1'仍然是'1','2'变成'22','0'变成''等等。但是除了最后一次出现的字符之外,其余的都必须保持原样。 (az)、(AZ) 和 (0-9) 以外的字符不受此类编码的影响。

例子:

方法:遍历字符串并使用散列跟踪每个字符的最新出现,然后对最后出现的字符进行编码。
请按照以下步骤解决问题:

  • 将变量字符串res初始化为空字符串以存储结果。
  • 0初始化大小为26small大写的数组和大小为10num ,以存储字符串str[]中任何字符的最后一次出现。
  • 使用变量i迭代范围[0, N]并执行以下任务:
    • 如果str[i]大于等于'0'且小于等于'9' ,则将num[str[i] – '0']的值设置为i。
    • Else 如果str[i]大于等于'a'且小于等于'z' ,则将small[str[i] – 'a']的值设置为i。
    • Else 如果str[i]大于等于'A'且小于等于'Z' ,则将capital[str[i] – 'A']的值设置为i。
  • 使用变量i迭代范围[0, N]并执行以下任务:
    • 如果str[i]大于等于'0'且小于等于'9'并且num[str[i]-'0']等于i ,则初始化变量为str [i]-' 0'并在结果字符串res中附加str[i]出现次数。
    • 否则,如果str[i]大于等于'a'且小于等于'z'并且small[str[i]-'a']等于i ,则初始化变量为str [i]- 'a'并在结果字符串res中附加str[i]出现次数。
    • 否则,如果str[i]大于等于'A'且小于等于'Z'大写[str[i]-'0']等于i ,则将变量初始化str[i]- 'A'并在结果字符串res中附加str[i]出现次数。
    • 否则,在结果字符串res中附加str[i]
  • 执行上述步骤后,打印字符串res作为答案。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to encode the given string
void encodeString(string str)
{
 
    // Variable string to store the result
    string res = "";
 
    // Arrays to store the last occuring index
    // of every character in the string
    int small[26] = { 0 }, capital[26] = { 0 },
        num[10] = { 0 };
 
    // Length of the string
    int n = str.size();
 
    // Iterate over the range
    for (int i = 0; i < n; i++) {
 
        // If str[i] is between 0 and 9
        if (str[i] >= '0' && str[i] <= '9') {
            num[str[i] - 48] = i;
        }
 
        // If str[i] is between a and z
        else if (str[i] >= 'a' && str[i] <= 'z') {
            small[str[i] - 97] = i;
        }
 
        // If str[i] is between A and Z
        else if (str[i] >= 'A' && str[i] <= 'Z') {
            capital[str[i] - 65] = i;
        }
    }
 
    // Iterate over the range
    for (int i = 0; i < n; i++) {
 
        // If str[i] is between a and z and i
        // is the last occurence in str
        if ((str[i] >= 'a' && str[i] <= 'z')
            && small[str[i] - 97] == i) {
 
            int occ = str[i] - 96;
            while (occ--) {
                res += str[i];
            }
        }
 
        // If str[i] is between A and Z and i
        // is the last occurence in str
        else if ((str[i] >= 'A' && str[i] <= 'Z')
                 && capital[str[i] - 65] == i) {
 
            int occ = str[i] - 64;
            while (occ--) {
                res += str[i];
            }
        }
 
        // If str[i] is between 0 and 9 and i
        // is the last occurence in str
        else if ((str[i] >= '0' && str[i] <= '9')
                 && num[str[i] - 48] == i) {
 
            int occ = str[i] - 48;
            while (occ--) {
                res += str[i];
            }
        }
        else {
            res += str[i];
        }
    }
 
    // Print the result
    cout << res;
}
 
// Driver Code
int main()
{
    string str = "Ea2, 0, E";
 
    encodeString(str);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
class GFG
{
   
// Function to encode the given string
static void encodeString(String str)
{
    // Variable string to store the result
    String res = "";
   
    // Arrays to store the last occuring index
    // of every character in the string
    int small[] = new int[26];
    int capital[] = new int[26];
    int num[] = new int[10];
        for(int i = 0; i < 26; i++)
        {
            small[i] = 0;
            capital[i] = 0;
        }
        for(int i = 0; i < 10; i++)
        {
            num[i] = 0;
        }
   
    // Length of the string
    int n = str.length();
   
    // Iterate over the range
    for (int i = 0; i < n; i++)
    {
   
        // If str[i] is between 0 and 9
        if (str.charAt(i)>= '0' && str.charAt(i) <= '9')
        {
            num[str.charAt(i) - 48] = i;
        }
   
        // If str[i] is between a and z
        else if (str.charAt(i) >= 'a' && str.charAt(i)<= 'z') {
            small[str.charAt(i)- 97] = i;
        }
   
        // If str[i] is between A and Z
        else if (str.charAt(i)>= 'A' && str.charAt(i) <= 'Z') {
            capital[str.charAt(i)- 65] = i;
        }
    }
   
    // Iterate over the range
    for (int i = 0; i < n; i++) {
   
        // If str[i] is between a and z and i
        // is the last occurence in str
        if ((str.charAt(i)>= 'a' && str.charAt(i)<= 'z')
            && small[str.charAt(i)- 97] == i) {
   
            int occ = str.charAt(i) - 96;
            while (occ-- >0)
            {
                res += str.charAt(i);
            }
        }
   
        // If str[i] is between A and Z and i
        // is the last occurence in str
        else if ((str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') && capital[str.charAt(i)- 65] == i)
        {
   
            int occ = str.charAt(i) - 64;
            while (occ-- >0) {
                res = res+str.charAt(i);
            }
        }
   
        // If str[i] is between 0 and 9 and i
        // is the last occurence in str
        else if ((str.charAt(i)>= '0' && str.charAt(i) <= '9')
                 && num[str.charAt(i) - 48] == i) {
   
            int occ = str.charAt(i) - 48;
            while (occ-- >0) {
                res = res+str.charAt(i);
            }
        }
        else {
            res = res+str.charAt(i);
        }
    }
   
    // Print the result
    System.out.print(res);
}
 
    // Driver Code
    public static void main(String[] args)
    {
            String str = "Ea2, 0, E";
   
              encodeString(str);
    }
}
 
// This code is contributed by dwivediyash


Python3
# Python 3 program for the above approach
 
# Function to encode the given string
def encodeString(str):
   
    # Variable string to store the result
    res = ""
 
    # Arrays to store the last occuring index
    # of every character in the string
    small = [0 for i in range(26)]
    capital = [0 for i in range(26)]
    num = [0 for i in range(10)]
 
    # Length of the string
    n = len(str)
 
    # Iterate over the range
    for i in range(n):
        # If str[i] is between 0 and 9
        if (str[i] >= '0' and str[i] <= '9'):
            num[ord(str[i]) - 48] = i
 
        # If str[i] is between a and z
        elif(str[i] >= 'a' and str[i] <= 'z'):
            small[ord(str[i]) - 97] = i
 
        # If str[i] is between A and Z
        elif(str[i] >= 'A' and str[i] <= 'Z'):
            capital[ord(str[i]) - 65] = i
 
    # Iterate over the range
    for i in range(n):
       
        # If str[i] is between a and z and i
        # is the last occurence in str
        if ((str[i] >= 'a' and str[i] <= 'z') and small[ord(str[i]) - 97] == i):
            occ = ord(str[i]) - 96
            while(occ>0):
                res += str[i]
                occ -= 1
 
        # If str[i] is between A and Z and i
        # is the last occurence in str
        elif((str[i] >= 'A' and str[i] <= 'Z') and capital[ord(str[i]) - 65] == i):
            occ = ord(str[i]) - 64
            while (occ>0):
                res += str[i]
                occ -= 1
 
        # If str[i] is between 0 and 9 and i
        # is the last occurence in str
        elif((str[i] >= '0' and str[i] <= '9') and num[ord(str[i]) - 48] == i):
            occ = ord(str[i]) - 48
            while (occ>0):
                res += str[i]
                occ -= 1
        else:
            res += str[i]
 
    # Print the result
    print(res)
 
# Driver Code
if __name__ == '__main__':
    str = "Ea2, 0, E"
    encodeString(str)
     
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for the above approach
using System;
 
public class GFG
{
 
  // Function to encode the given string
  static void encodeString(String str)
  {
 
    // Variable string to store the result
    String res = "";
 
    // Arrays to store the last occuring index
    // of every character in the string
    int []small = new int[26];
    int []capital = new int[26];
    int []num = new int[10];
    for(int i = 0; i < 26; i++)
    {
      small[i] = 0;
      capital[i] = 0;
    }
    for(int i = 0; i < 10; i++)
    {
      num[i] = 0;
    }
 
    // Length of the string
    int n = str.Length;
 
    // Iterate over the range
    for (int i = 0; i < n; i++)
    {
 
      // If str[i] is between 0 and 9
      if (str[i]>= '0' && str[i] <= '9')
      {
        num[str[i] - 48] = i;
      }
 
      // If str[i] is between a and z
      else if (str[i] >= 'a' && str[i]<= 'z') {
        small[str[i]- 97] = i;
      }
 
      // If str[i] is between A and Z
      else if (str[i]>= 'A' && str[i] <= 'Z') {
        capital[str[i]- 65] = i;
      }
    }
 
    // Iterate over the range
    for (int i = 0; i < n; i++) {
 
      // If str[i] is between a and z and i
      // is the last occurence in str
      if ((str[i]>= 'a' && str[i]<= 'z')
          && small[str[i]- 97] == i) {
 
        int occ = str[i] - 96;
        while (occ-- >0)
        {
          res += str[i];
        }
      }
 
      // If str[i] is between A and Z and i
      // is the last occurence in str
      else if ((str[i] >= 'A' && str[i] <= 'Z') && capital[str[i]- 65] == i)
      {
 
        int occ = str[i] - 64;
        while (occ-- >0) {
          res = res+str[i];
        }
      }
 
      // If str[i] is between 0 and 9 and i
      // is the last occurence in str
      else if ((str[i]>= '0' && str[i] <= '9')
               && num[str[i] - 48] == i) {
 
        int occ = str[i] - 48;
        while (occ-- >0) {
          res = res+str[i];
        }
      }
      else {
        res = res+str[i];
      }
    }
 
    // Print the result
    Console.Write(res);
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    String str = "Ea2, 0, E";
 
    encodeString(str);
  }
}
 
// This code is contributed by 29AjayKumar


Javascript



输出:
Ea22, , EEEEE

时间复杂度: O(N)
辅助空间: O(M)(结果字符串的大小)