给定长度为N的字符串S ,它是混杂格式的[0 – 9]范围内的任意数字的英语表示。任务是从此表示形式中找到数字。
注意:以任何顺序打印数字
例子
Input: S = “owoftnuoer”
Output: 124
Explanation: The digits here are jumbled form of one, two and four. Therefore, the required output can be 124 or 421 or 214 etc.
Input: S = “zesxrionezoreo”
Output: 0016
方法:可以通过观察一个有趣的事实轻松解决此问题,即所有偶数数字都至少有一个字符不在任何其他字符串,而所有奇数数字都没有:
Following digits have unique letters:
- zero: Only digit with z
- two: Only digit with w
- four: Only digit with u
- six: Only digit with x
- eight: Only digit with g
对于奇数位,每个字母也以其他数字出现。单词中的奇数位是{1、3、5、7、9}。请按照以下步骤解决问题
- 创建一个字符串向量num ,该向量将保存从0到9的英语字母中的数字,以及一个整数向量, count []的大小为10 ,并创建一个答案字符串ans以显示数字。
- 现在,遍历给定的字符串,从i = 0到N-1 。
- 如果s [i] =’z’ ,则将count [0]增加1 。
- 如果s [i] =’w’ ,则将count [2]增加1 。
- 如果s [i] =’g’ ,则将count [8]增加1 。
- 如果s [i] =’x’ ,则将count [6]增加1 。
- 如果s [i] =’v’ ,则将count [5]增加1 。
- 如果s [i] =’o’ ,则将count [1]增加1 。
- 如果s [i] =’s’ ,则将count [7]增加1 。
- 如果s [i] =’f’ ,则将count [4]增加1 。
- 如果s [i] =’h’ ,则将count [3]增加1 。
- 如果s [i] =’i’ ,则将count [9]增加1 。
- 现在,如下所示更新矢量的元素:
- count[7] = count[7] – count[6].
- count[5] = count[5] – count[7].
- count[4] = count[4] – count[5].
- count[1] = count[1] – (count[2] + count[4] + count[0]).
- count[3] = count[3] – count[8].
- count[9] = count[9] – (count[5] + count[6] + count[8]).
- 现在,将向量从0遍历到9 ,并将字符(i +’0’)附加到ans ,计数[i]次。
- 最后,将ans打印为必需的答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to convert the jumbled
// string into digits
string finddigits(string s)
{
// Strings of digits 0-9
string num[]
= { "zero", "one", "two",
"three", "four", "five",
"six", "seven", "eight", "nine" };
// Initialize vector
vector arr(10);
// Initialize answer
string ans = "";
// Size of the string
int n = s.size();
// Traverse the string
for (int i = 0; i < n; i++) {
if (s[i] == 'z')
arr[0]++;
if (s[i] == 'w')
arr[2]++;
if (s[i] == 'g')
arr[8]++;
if (s[i] == 'x')
arr[6]++;
if (s[i] == 'v')
arr[5]++;
if (s[i] == 'o')
arr[1]++;
if (s[i] == 's')
arr[7]++;
if (s[i] == 'f')
arr[4]++;
if (s[i] == 'h')
arr[3]++;
if (s[i] == 'i')
arr[9]++;
}
// Update the elements of the vector
arr[7] -= arr[6];
arr[5] -= arr[7];
arr[4] -= arr[5];
arr[1] -= (arr[2] + arr[4] + arr[0]);
arr[3] -= arr[8];
arr[9] -= (arr[5] + arr[6] + arr[8]);
// Print the digits into their
// original format
for (int i = 0; i < 10; i++) {
for (int j = 0; j < arr[i]; j++) {
ans += (char)(i + '0');
}
}
// Return answer
return ans;
}
// Driver Code
int main()
{
string s = "owoftnuoer";
cout << finddigits(s) << endl;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to convert the jumbled
// string into digits
static String finddigits(String s)
{
// Strings of digits 0-9
String[] num
= { "zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine" };
// Initialize vector
int[] arr = new int[10];
// Initialize answer
String ans = "";
// Size of the string
int n = s.length();
// Traverse the string
for (int i = 0; i < n; i++) {
if (s.charAt(i) == 'z')
arr[0]++;
if (s.charAt(i) == 'w')
arr[2]++;
if (s.charAt(i) == 'g')
arr[8]++;
if (s.charAt(i) == 'x')
arr[6]++;
if (s.charAt(i) == 'v')
arr[5]++;
if (s.charAt(i) == 'o')
arr[1]++;
if (s.charAt(i) == 's')
arr[7]++;
if (s.charAt(i) == 'f')
arr[4]++;
if (s.charAt(i) == 'h')
arr[3]++;
if (s.charAt(i) == 'i')
arr[9]++;
}
// Update the elements of the vector
arr[7] -= arr[6];
arr[5] -= arr[7];
arr[4] -= arr[5];
arr[1] -= (arr[2] + arr[4] + arr[0]);
arr[3] -= arr[8];
arr[9] -= (arr[5] + arr[6] + arr[8]);
// Print the digits into their
// original format
for (int i = 0; i < 10; i++) {
for (int j = 0; j < arr[i]; j++) {
ans += (char)(i + '0');
}
}
// Return answer
return ans;
}
// Driver Code
public static void main(String[] args)
{
String s = "owoftnuoer";
System.out.println(finddigits(s));
}
}
// This code is contributed by Dharanendra L V
Python3
# Python3 program for the above approach
# Function to convert the jumbled
# into digits
def finddigits(s):
# Strings of digits 0-9
num = [ "zero", "one", "two", "three",
"four", "five", "six", "seven",
"eight", "nine"]
# Initialize vector
arr = [0] * (10)
# Initialize answer
ans = ""
# Size of the string
n = len(s)
# Traverse the string
for i in range(n):
if (s[i] == 'z'):
arr[0] += 1
if (s[i] == 'w'):
arr[2] += 1
if (s[i] == 'g'):
arr[8] += 1
if (s[i] == 'x'):
arr[6] += 1
if (s[i] == 'v'):
arr[5] += 1
if (s[i] == 'o'):
arr[1] += 1
if (s[i] == 's'):
arr[7] += 1
if (s[i] == 'f'):
arr[4] += 1
if (s[i] == 'h'):
arr[3] += 1
if (s[i] == 'i'):
arr[9] += 1
# Update the elements of the vector
arr[7] -= arr[6]
arr[5] -= arr[7]
arr[4] -= arr[5]
arr[1] -= (arr[2] + arr[4] + arr[0])
arr[3] -= arr[8]
arr[9] -= (arr[5] + arr[6] + arr[8])
# Print the digits into their
# original format
for i in range(10):
for j in range(arr[i]):
ans += chr((i) + ord('0'))
# Return answer
return ans
# Driver Code
if __name__ == '__main__':
s = "owoftnuoer"
print(finddigits(s))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to convert the jumbled
// string into digits
static string finddigits(string s)
{
// Initialize vector
int[] arr = new int[10];
// Initialize answer
string ans = "";
// Size of the string
int n = s.Length;
// Traverse the string
for (int i = 0; i < n; i++) {
if (s[i] == 'z')
arr[0]++;
if (s[i] == 'w')
arr[2]++;
if (s[i] == 'g')
arr[8]++;
if (s[i] == 'x')
arr[6]++;
if (s[i] == 'v')
arr[5]++;
if (s[i] == 'o')
arr[1]++;
if (s[i] == 's')
arr[7]++;
if (s[i] == 'f')
arr[4]++;
if (s[i] == 'h')
arr[3]++;
if (s[i] == 'i')
arr[9]++;
}
// Update the elements of the vector
arr[7] -= arr[6];
arr[5] -= arr[7];
arr[4] -= arr[5];
arr[1] -= (arr[2] + arr[4] + arr[0]);
arr[3] -= arr[8];
arr[9] -= (arr[5] + arr[6] + arr[8]);
// Print the digits into their
// original format
for (int i = 0; i < 10; i++) {
for (int j = 0; j < arr[i]; j++) {
ans += (char)(i + '0');
}
}
// Return answer
return ans;
}
// Driver Code
static public void Main()
{
string s = "owoftnuoer";
Console.WriteLine(finddigits(s));
}
}
// This code is contributed by Dharanendra L V
输出:
124
时间复杂度: O(N),其中N是字符串的长度
辅助空间: O(N)