我收到了混乱的字符串: "OTNWEHRE"
现在我必须提取单词" ONE" " TWO" "三"并以数字形式 1 2 3
所以,我的方法是存储所有单词" ZERO,ONE,TWO,THREE,...."首先在字符数组中,然后通过字符串输入运行循环,并将字符串元素与字符数组(O' of string i/p with characters of array ['Z','E','R'..]
)进行比较。如果字符匹配('O' of string input will match at 'O' of ONE
),我将通过char数组(after O matches loop through 'N' then 'E'then ','
)中的字符递增,并检查字符串输入中是否存在这些元素。如果字符匹配到下一个','
我将打印该号码。
但是我的算法存在问题,四位数字,五位数或六位数字以及七位数字以相同的字符开头,但后跟不同的字符。
这里是我的代码
public static void main(String args[])
{
int i,j,a;
String wrd="";
checknum o = new checknum();
Scanner sc = new Scanner(System.in);
String s =sc.nextLine();
String z= "zero,one,two,three,four,five,six,seven,eight,nine,";
char[] c = z.toCharArray();
for(i=0;i<s.length();i++)
{
wrd ="";
for(j=0;j<c.length;j++)
{
if(s.charAt(i)==c[j])
{
wrd = wrd+c[j];
while(c[j] != ',')
{
j++;
if(s.indexOf(c[j])>=0)
wrd = wrd+c[j];
else{
wrd="";
break;
}
}
if(wrd!=null){
a=o.checknumber(wrd);
System.out.println(a);
}
}
}
}
}
PS
如果我的方法完全错误,并且我还有其他任何方式可以解决这个问题,请告诉我。
答案 0 :(得分:0)
要解决的一种方法是 -
Pseudo Code
String [ ] str ={"ZERO","ONE"..};
char[ ] input = {'O','T','N','W','H','E','R','E'};
char [ 26 ] temparray;
For i in input
temparray[i]++;
//Consider 'a'=index 0, 'a' =index 1 ... And so on
For i in str
char[ ] temparray2 = str[i].toCharArray();
For j in temparray2
temparray2 [j]++;
//Consider 'a'=index 0, 'a' =index 1 ... And so on
For j in temparray2
For k in temparray
temparray[k]==temparray2 [j]
// If they are equal, print. If not come out of the loop
注意:我刚给你提示为它写一个逻辑。你试着编码它。还有很多其他方法可以优化它。
答案 1 :(得分:0)
如果我理解正确,数字可以共享字符。 (例如,您的示例中只有一个T
,但同时包含TWO
和THREE
。)
从 HashMap 中的 String 中收集所有字符的数量:
String s = ...;
HashMap<Character, Integer> charCount = new HashMap<>();
for (char c : s.toCharArray()) {
Integer count = charCount.get(c);
if (count == null) count = 0;
map.put(c, ++count);
}
这样你只需要遍历 String 一次。
然后,您可以将每个数字存储为具有字符/整数对的 HashMap ,例如THREE
存储为{{T,1},{H,1},{R,1},{E,2}}
。
然后,您可以遍历每个号码地图并检查charCount
是否包含当前号中的每个字符(键)如果 count(value)大于或等于。
或为数字字符创建某种树结构,如下所示:
_____
| O,1 |
|_____|
/ \
_____/ \_____
| E,1 | | W,1 |
|_____| |_____|
/ \ \
_____/ \_____ \_____
| N,1 | | R,1 | | T,1 |
|_____| |_____| |_____|
v | v
"ONE" __|__ "TWO"
| Z,1 |
|_____|
v
"ZERO"
然后遍历它,你到达的每个叶都是一个包含的数字:
// pseudocode
recursiveCheck(node) {
if (charCount.containsKey(node.character) && charCount.get(node.character) > node.count) {
if (node.hasChildren()) {
for (childnode in node) {
recursiveCheck(childnode);
}
}
else {
// node is leaf, node.number is contained
}
}
}
答案 2 :(得分:0)
public class Solution{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String query = sc.nextLine();
String ans = solve(query);
System.out.println(ans);
}
private String solve(String query){
int fmap[] = new int[27];
for(char x : query.toCharArray()){
fmap[x - 'a']++;
}
StringBuilder sb = new StringBuilder("");
while(fmap['w' - 'a']-- != 0){
fmap['t'-'a']--;
fmap['o'-'a']--;
sb.append('2');
}
while(fmap['g' - 'a']-- != 0){
fmap['e'-'a']--;
fmap['i'-'a']--;
fmap['h'-'a']--;
fmap['t'-'a']--;
sb.append('8');
}
while(fmap['z' - 'a']-- != 0){
fmap['e'-'a']--;
fmap['r'-'a']--;
fmap['o'-'a']--;
sb.append('0');
}
while(fmap['x' - 'a']-- != 0){
fmap['s'-'a']--;
fmap['i'-'a']--;
sb.append('6');
}
while(fmap['u' - 'a']-- != 0){
fmap['f'-'a']--;
fmap['o'-'a']--;
fmap['r'-'a']--;
sb.append('4');
}
while(fmap['u' - 'a']-- != 0){
fmap['f'-'a']--;
fmap['o'-'a']--;
fmap['r'-'a']--;
sb.append('4');
}
while(fmap['o' - 'a']-- != 0){
fmap['e'-'a']--;
fmap['n'-'a']--;
sb.append('1');
}
while(fmap['t' - 'a']-- != 0){
fmap['e'-'a']-=2;
fmap['h'-'a']--;
fmap['r'-'a']--;
sb.append('3');
}
while(fmap['f' - 'a']-- != 0){
fmap['e'-'a']--;
fmap['i'-'a']--;
fmap['v'-'a']--;
sb.append('5');
}
while(fmap['s' - 'a']-- != 0){
fmap['e'-'a']-=2;
fmap['n'-'a']--;
fmap['v'-'a']--;
sb.append('7');
}
while(fmap['i' - 'a']-- != 0){
fmap['n'-'a']-=2;
fmap['e'-'a']--;
sb.append('9');
}
}
}
/*
if 'w' -> there is two
if 'g' -> there is eight
if 'z' -> there is zero
if 'x' -> there is six
if 'u' -> there is four
after removing these one, three, five, seven, nine remain (if present)
if 'o' -> there is one. left: {three, five, seven, nine}
if 't' -> there is three. left: {five, seven, nine}
if 'f' -> there is five. left: {seven, nine}
if 's' -> there is seven. left: {nine}
if 'i' -> there is nine. left: {}
*/
答案 3 :(得分:0)
我没有使用任何算法,而是以单词格式在每个数字中使用了唯一性,对于我遇到的许多测试用例,它就像一个魅力。
这个想法是0-零是唯一的,因为它具有字符z,而其他任何数字都不包含z 2-有w 4-有你 6-有x
下面是代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.*;
public class Yash {
/**
* Iterate through each line of input.
*/
public static void main(String[] args) throws IOException {
InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
BufferedReader in = new BufferedReader(reader);
String line;
String number0 = "";
String number1 = "";
String number2 = "";
String number3 = "";
String number4 = "";
String number5 = "";
String number6 = "";
String number7 = "";
String number8 = "";
String number9 = "";
while ((line = in.readLine()) != null) {
Map<Character, Integer> chracterMap = new HashMap<>();
for (int i = 0; i < line.length(); i++) {
if (chracterMap.get(line.charAt(i)) == null) {
chracterMap.put(line.charAt(i), 1);
} else {
chracterMap.put(line.charAt(i), chracterMap.get(line.charAt(i)) + 1);
}
}
if (chracterMap.get('z') > 0) {
for (int i = 0; i < chracterMap.get('z') ;i++) {
number0 = number0 + Integer.toString(0);
}
chracterMap.put('e', chracterMap.get('e') - chracterMap.get('z'));
chracterMap.put('r', chracterMap.get('r') - chracterMap.get('z'));
chracterMap.put('o', chracterMap.get('o') - chracterMap.get('z'));
}
if (chracterMap.get('w') > 0) {
for (int i = 0; i < chracterMap.get('w') ;i++) {
number2 = number2 + Integer.toString(2);
}
chracterMap.put('t', chracterMap.get('t') - chracterMap.get('w'));
chracterMap.put('o', chracterMap.get('o') - chracterMap.get('w'));
}
if (chracterMap.get('u') > 0) {
for (int i = 0; i < chracterMap.get('u') ;i++) {
number4 = number4 + Integer.toString(4);
}
chracterMap.put('f', chracterMap.get('f') - chracterMap.get('u'));
chracterMap.put('o', chracterMap.get('o') - chracterMap.get('u'));
chracterMap.put('r', chracterMap.get('r') - chracterMap.get('u'));
}
if (chracterMap.get('x') > 0) {
for (int i = 0; i < chracterMap.get('x') ;i++) {
number6 = number6 + Integer.toString(6);
}
chracterMap.put('s', chracterMap.get('s') - chracterMap.get('x'));
chracterMap.put('i', chracterMap.get('i') - chracterMap.get('x'));
}
if (chracterMap.get('g') > 0) {
for (int i = 0; i < chracterMap.get('g') ;i++) {
number8 = number8 + Integer.toString(8);
}
chracterMap.put('e', chracterMap.get('e') - chracterMap.get('g'));
chracterMap.put('i', chracterMap.get('i') - chracterMap.get('g'));
chracterMap.put('h', chracterMap.get('h') - chracterMap.get('g'));
chracterMap.put('t', chracterMap.get('t') - chracterMap.get('g'));
}
if (chracterMap.get('f') > 0) {
for (int i = 0; i < chracterMap.get('f') ;i++) {
number5 = number5 + Integer.toString(5);
}
chracterMap.put('v', chracterMap.get('v') - chracterMap.get('f'));
chracterMap.put('i', chracterMap.get('i') - chracterMap.get('f'));
chracterMap.put('e', chracterMap.get('e') - chracterMap.get('f'));
}
if (chracterMap.get('r') > 0) {
for (int i = 0; i < chracterMap.get('r') ;i++) {
number3 = number3 + Integer.toString(3);
}
chracterMap.put('t', chracterMap.get('t') - chracterMap.get('r'));
chracterMap.put('h', chracterMap.get('h') - chracterMap.get('r'));
chracterMap.put('e', chracterMap.get('e') - chracterMap.get('r') - chracterMap.get('r'));
}
if (chracterMap.get('i') > 0) {
for (int i = 0; i < chracterMap.get('i') ;i++) {
number9 = number9 + Integer.toString(9);
}
}
if(chracterMap.get('s') > 0) {
for (int i = 0; i < chracterMap.get('s') ;i++) {
number9 = number9 + Integer.toString(7);
}
}
if(chracterMap.get('o') > 0) {
for (int i = 0; i < chracterMap.get('o') ;i++) {
number1 = number1 + Integer.toString(1);
}
}
System.out.println(number0 + number1+ number2 + number3 + number4+ number5 + number6 + number7 + number8+ number9);
}
}
}