423. Reconstruct Original Digits from English

本文介绍了一种算法,该算法能够将包含英文数字表述的字符串转换为升序排列的数字。输入字符串长度不超过50,000,且仅包含有效的英文小写字母,通过分析特定字符频率来确定每个数字的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given a non-empty string containing an out-of-order English representation of digits 0-9, output the digits in ascending order.

Note:

  1. Input contains only lowercase English letters.
  2. Input is guaranteed to be valid and can be transformed to its original digits. That means invalid inputs such as "abc" or "zerone" are not permitted.
  3. Input length is less than 50,000.

Example 1:

Input: "owoztneoer"

Output: "012"

Example 2:

Input: "fviefuro"

Output: "45"




例如 “two",字符w只在”two"中出现,在其他的数字中都不出现,那么w字符的个数就代表2的个数。

这样可以确定的是0、2、4、6、8,其他字符可以根据第一步得到的数字个数结合字符分布情况推出来

public class Solution {
    public String originalDigits(String s)
	{
		int len=s.length();
		int[] carr=new int[128];
		for(int i=0;i<len;i++)
			carr[s.charAt(i)]++;
		int[] num_count=new int[10];
		num_count[0]=carr['z'];
		num_count[2]=carr['w'];
		num_count[4]=carr['u'];
		num_count[6]=carr['x'];
		num_count[8]=carr['g'];
		num_count[1]=carr['o']-num_count[0]-num_count[2]-num_count[4];
		num_count[3]=carr['h']-num_count[8];
		num_count[5]=carr['f']-num_count[4];
		num_count[7]=carr['s']-num_count[6];
		num_count[9]=(carr['n']-num_count[1]-num_count[7])/2;
		StringBuilder sb=new StringBuilder();
		for(int i=0;i<10;i++)
			for(int j=0;j<num_count[i];j++)
				sb.append(i);
		return sb.toString();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值