Counting length-2 substrings that are common to two strings at the same offset I am working through the CodingBat exercises for Java. I just completed this one, an exercise that requests the comparison of substrings of two strings:


  Given 2 strings, a and b, return the number of the positions where they contain the same length 2 substring. So "xxcaazz" and "xxbaaz" yields 3, since the "xx", "aa", and "az" substrings appear in the same place in both strings. 

stringMatch("xxcaazz", "xxbaaz") → 3  
stringMatch("abc", "abc") → 2  
stringMatch("abc", "axc") → 0



Here is my method (which does work):

public int stringMatch(String a, String b) {
    int count = 0;

    String sub2 = "";

    String arrayA[] = new String[a.length()];
    String arrayB[] = new String[b.length()];

    for (int i = 0; i &lt; a.length()-1; i++) {
        sub2 = a.substring(i, i+2);
        arrayA[i] = sub2;       
    }

    for (int i = 0; i &lt; b.length()-1; i++) {
        sub2 = b.substring(i, i+2);
        arrayB[i] = sub2;       
    }

    for (int j = 0; j &lt; arrayA.length-1; j++) {

        if (j &gt;= b.length())
            break;

        if (arrayA[j].equals(arrayB[j]))
            count++;
    }

    return count;
}


And here is the answer on the site:


public int stringMatch(String a, String b) {

  int len = Math.min(a.length(), b.length());
  int count = 0;

  for (int i=0; i&lt;len-1; i++) {
    String aSub = a.substring(i, i+2);
    String bSub = b.substring(i, i+2);
    if (aSub.equals(bSub)) {  // Use .equals() with strings
      count++;
    }
  }

  return count;
}



I feel that my code is longer and messy and drawn out compared to other people's.  As a beginner programmer, how should I improve it?
If I were you I would try to write more concise solutions. Readability is extremely important. If you write code that is hard to read, it will not be reused as much, it will be hard to change or extend the code, your code will probably perform worse, and it will be hard to find bugs.

Your first approach can be based on your intuition, but then you should look at your code, and try to find ways to simplify it. With time and experience, this will get easier and easier, and your intuitive solution will more closely resemble a concise solution.

Ignoring the complexity of your solution, here are some things that could be better:


Your first two for loops perform exactly the same functionality, and you shouldn't duplicate code. Whenever you copy-paste a piece of code, think about extracting it to a method. This will increase the readability, maintainability, and reusability of your code.
define variables when they are needed, not earlier. The fewer variables a reader has to remember at any point, the easier it will be to understand your code. count for example isn't needed at the beginning of the method, but just in the last for loop, so declare it there. Another example is sub2, which isn't needed at all, and which also doesn't have a very expressive name.
use spaces around - and + to increase readability.
don't declare variables that are only used once, but use those values directly, eg arrayA[i] = a.substring(i, i+2);.
use curly brackets even for one line statements to increase readability and possibly avoid future bugs.
be consistent. Why do you use i as loop variable for the first two loops, but not the third? If there is no reason, use i for all.
Why have if (j &gt;= b.length())? It's not intuitively clear what it does, so you should at least add a comment.