📌  相关文章
📜  将字符串的字符重新排序为数字的有效英文表示形式

📅  最后修改于: 2021-04-17 18:42:55             🧑  作者: Mango

给定长度为N的字符串S ,该字符串由小写字符组成,这些小写字符包含重新排列的数字[0-9]英文表示形式任务是按升序打印这些数字。

例子:

天真的方法:最简单的方法是生成给定字符串的所有排列,对于每个排列,检查是否有可能找到由字符串表示的有效数字。如果发现为真,则按升序打印数字集。

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

高效的方法:这个想法是基于观察到某些字符仅以一个数字出现。

请按照以下步骤解决问题:

  • 初始化一个空字符串,ANS存储所需的结果。
  • 将字符串中每个字符的频率存储在M中
  • 创建一个唯一字符到其对应字符串的映射。
  • 遍历地图,然后执行以下步骤:
    • 将与数字相对应的唯一字符存储在变量x中
    • 获取xM中的出现,并将其存储在变量freq中
    • 将相应的数字,频率次数附加到ans
    • 遍历x对应的单词,并以M的频率减少其字符的频率
  • 打印字符串, ans作为结果。  

下面是上述方法的实现:

Python3
# Python program to implement the above approach
from collections import Counter
 
# Function to construct the original set of digits
# from the string in ascending order
def construct_digits(s):
   
    # Store the unique characters
    # corresponding to word and number
    k = ["z", "w", "u", "x", "g",
         "h", "o", "f", "v", "i"]
 
    l = ["zero", "two", "four", "six", "eight",
         "three", "one", "five", "seven", "nine"]
 
    c = [0, 2, 4, 6, 8, 3, 1, 5, 7, 9]
     
    # Store the required result
    ans = []
     
    # Store the frequency of
    # each character of S
    d = Counter(s)
 
    # Traverse the unique characters
    for i in range(len(k)):
 
        # Store the count of k[i] in S
        x = d.get(k[i], 0)
         
        # Traverse the corresponding word
        for j in range(len(l[i])):
               
            # Decrement the frequency
            # of characters by x
            d[l[i][j]]-= x
             
        # Append the digit x times to ans
        ans.append(str(c[i])*x)
 
    # Sort the digits in ascending order
    ans.sort()
 
    return "".join(ans)
 
# Driver Code
 
# Given string, s
s = "fviefuro"
 
# Function Call
print(construct_digits(s))


C#
// C# program to implement the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to construct the original set of digits
// from the string in ascending order
static string construct_digits(string s)
{
     
    // Store the unique characters
    // corresponding to word and number
    char[] k = { 'z', 'w', 'u', 'x', 'g',
                 'h', 'o', 'f', 'v', 'i' };
 
    string[] l = { "zero", "two", "four", "six", "eight",
                   "three", "one", "five", "seven", "nine" };
 
    int[] c = { 0, 2, 4, 6, 8, 3, 1, 5, 7, 9 };
 
    // Store the required result
    List ans = new List();
 
    // Store the frequency of
    // each character of S
    Dictionary d = new Dictionary();
    for(int i = 0; i < s.Length; i++)
    {
        if (!d.ContainsKey(s[i]))
            d[s[i]] = 0;
             
        d[s[i]] += 1;
    }
 
    // Traverse the unique characters
    for(int i = 0; i < k.Length; i++)
    {
         
        // Store the count of k[i] in S
        int x = 0;
        if (d.ContainsKey(k[i]))
            x = d[k[i]];
 
        // Traverse the corresponding word
        for(int j = 0; j < l[i].Length; j++)
        {
             
            // Decrement the frequency
            // of characters by x
            if (d.ContainsKey(l[i][j]))
                d[l[i][j]] -= x;
        }
         
        // Append the digit x times to ans
        ans.Add(((c[i]) * x).ToString());
    }
     
    // Sort the digits in ascending order
    ans.Sort();
 
    string str = (String.Join("", ans.ToArray()));
    return str.Replace("0", "");
}
 
// Driver Code
public static void Main(string[] args)
{
     
    // Given string, s
    string s = "fviefuro";
 
    // Function Call
    Console.WriteLine(construct_digits(s));
}
}
 
// This code is contributed by ukasp


输出:
45

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