Given an array of Strings, return the String that has the most digits followed immediately by the letter x. If two strings have the same number, return the one with the lowest index.
gooD({"1x","123456789","1y3534ssf","4hsd73s"}) → "1x"
gooD({"1xs3x3412fgxx6","1x+4x=5x","x5x"}) → "1x+4x=5x"
gooD({"3x2y11x3gx5x","","232","2x2xx3x3x"}) → "2x2xx3x3x"
I am completely puzzled why my code is not working. Why so?
public String gooD(String arr[]) {
String digits="0123456789";
int low=0;
int check=0;
String s="";
String d="";
for (int i=0; i<arr.length; i++) {
s=arr[i];
for (int j=0; j<s.length()-1; j++) {
if (digits.indexOf(s.substring(j,j+1))!=-1) {
if (s.substring(j+1,j+2).equals("x")) {
d+=s.substring(j,j+1);
}
}
}
check=d.length();
if (check<low) low=check;
d="";
}
for (int i=0; i<arr.length; i++) {
s=arr[i];
for (int j=0; j<s.length()-1; j++) {
if (digits.indexOf(s.substring(j,j+1))!=-1) {
if (s.substring(j+1,j+2).equals("x")) {
d+=s.substring(j,j+1);
}
}
}
if (d.length()==low) return d;
d="";
}
return d;
}
\d+(?=x). Loop over the string with matcher.find.