0

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;
}
2
  • I would suggest you use a regexp such as \d+(?=x). Loop over the string with matcher.find. Commented Jan 23, 2015 at 8:52
  • @user902383 You might elaborate your ominous hints. Commented Jan 23, 2015 at 9:16

3 Answers 3

2

One error is

if (check<low) low=check;

which should be

if (check > low) low = check;

as you are looking for a maximum.

Considerably simpler is:

public static String gooD(String... arr) {
    int max = 0;
    String best = "";
    for( String s: arr ){
        String t = s.replaceAll( "\\dx", "" );
        int d = s.length() - t.length();
        if( d > max ){
            max = d;
            best = s;
        }
    }
    return best;
}
Sign up to request clarification or add additional context in comments.

Comments

0

First you have to reverse your test :

    if (check<low) low=check;

should be :

    if (check>low) low=check;

Your code was looking for the minimum instead of the maximum.

Next you are not returning the input array, but only the digits that were found immetiately followed by an x.

Keeping your method, a possible implementation could be :

public String gooD(String arr[]) {
    String digits = "0123456789";
    int low = 0;
    int ilow = 0; // first string by default
    int check = 0;
    String s;
    String d = "";
    for (int i = 0; i < arr.length; i++) {
        s = arr[i];
        check = 0;
        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")) {
                    check += 1;
                }
            }
        }
        if (check > low) { // only is strictly longer
            low = check;
            ilow = i; // keep the index of first longer string
        }
    }
    return arr[ilow]; // return the string
}

Comments

0

What @laune said. Anyway, this would be far more efficient

public String gooD(String arr[]) {
    Pattern pattern = Pattern.compile("\\dx");

    String candidate = "";
    int max = 0;
    for (String s : arr){
        Matcher matcher = pattern.matcher(s);
        int current = 0;
        while (matcher.find()){
            current++;
        }
        if (current > max){
            max = current;
            candidate = s;
        }
    }
    return candidate;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.