Though my code compiles, it returns all 0's, and I don't understand why.
The problem I have to answer is :
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.
Example:
stringMatch("xxcaazz", "xxbaaz") → 3
stringMatch("abc", "abc") → 2
stringMatch("abc", "axc") → 0
My code for the method is:
public int stringMatch(String a, String b)
{
int match = 0;
int x = 0;
int y = 2;
while(y < a.length() && y < b.length())
{
if (a.substring(x,y) == b.substring(x,y))
{
match++;
x++;
y++;
}
else
{
x++;
y++;
}
}
return match;
}