- Reconstruct Original Digits from English
Given a string s containing an out-of-order English representation of digits 0-9, return the digits in ascending order.
I tried the code in Java but it somehow is throwing an error for Index out of Bound
It works for other inputs but throws the error for this input
public static String originalDigits(String s){
StringBuilder st = new StringBuilder(s);
StringBuilder num = new StringBuilder();
while(st.length()!=0){
if (st.indexOf("z")!=-1){
num.append("0");
st.deleteCharAt(st.indexOf("z"));
st.deleteCharAt(st.indexOf("e"));
st.deleteCharAt(st.indexOf("r"));
st.deleteCharAt(st.indexOf("o"));
}
if (st.indexOf("o")!=-1 && st.indexOf("n")!=-1){
num.append("1");
st.deleteCharAt(st.indexOf("o"));
st.deleteCharAt(st.indexOf("n"));
st.deleteCharAt(st.indexOf("e"));
}
if (st.indexOf("w")!=-1){
num.append("2");
st.deleteCharAt(st.indexOf("t"));
st.deleteCharAt(st.indexOf("w"));
st.deleteCharAt(st.indexOf("o"));
}
if (st.indexOf("h")!=-1 && st.indexOf("r")!=-1){
num.append("3");
st.deleteCharAt(st.indexOf("t"));
st.deleteCharAt(st.indexOf("h"));
st.deleteCharAt(st.indexOf("r"));
st.deleteCharAt(st.indexOf("e"));
st.deleteCharAt(st.indexOf("e"));
}
if (st.indexOf("u")!=-1){
num.append("4");
st.deleteCharAt(st.indexOf("f"));
st.deleteCharAt(st.indexOf("o"));
st.deleteCharAt(st.indexOf("u"));
st.deleteCharAt(st.indexOf("r"));
}
if (st.indexOf("f")!=-1){
num.append("5");
st.deleteCharAt(st.indexOf("f"));
st.deleteCharAt(st.indexOf("i"));
st.deleteCharAt(st.indexOf("v"));
st.deleteCharAt(st.indexOf("e"));
}
if (st.indexOf("x")!=-1){
num.append("6");
st.deleteCharAt(st.indexOf("s"));
st.deleteCharAt(st.indexOf("i"));
st.deleteCharAt(st.indexOf("x"));
}
if (st.indexOf("v")!=-1){
num.append("7");
st.deleteCharAt(st.indexOf("s"));
st.deleteCharAt(st.indexOf("e"));
st.deleteCharAt(st.indexOf("v"));
st.deleteCharAt(st.indexOf("e"));
st.deleteCharAt(st.indexOf("n"));
}
if (st.indexOf("g")!=-1){
num.append("8");
st.deleteCharAt(st.indexOf("e"));
st.deleteCharAt(st.indexOf("i"));
st.deleteCharAt(st.indexOf("g"));
st.deleteCharAt(st.indexOf("h"));
st.deleteCharAt(st.indexOf("t"));
}
if (st.indexOf("n")!=-1){
num.append("9");
st.deleteCharAt(st.indexOf("n"));
st.deleteCharAt(st.indexOf("i"));
st.deleteCharAt(st.indexOf("n"));
st.deleteCharAt(st.indexOf("e"));
}
System.out.println(num);
}
String num1 = new String(num);
return num1;
}
The error specifically is : Error Showing
You need to change the order of the checks for certain letters. Start with the digits whose names contain a unique letter, i.e.
Of-course continue to remove the relevant letters – as you are already doing.
After that, check for the letter f since you have removed all the letters f, o, u and r (for the digit 4) and therefore any remaining letter f will indicate that the string contains the letters for the digit 5.
After that, check for the letter v (for the digit 7)
Then check for the letter h (for the digit 3)
Then check for i (9)
Finally check for n (1).
This will fix your
StringIndexOutOfBoundsExceptionbut you may fail due to time limit exceeded.Note that many, if not all, leetcode problems have online solutions. For problem 423, I found this answer.
Here is your code with my corrections – as described above. When I submitted this solution, I got time limit exceeded.