Regular Expression
We'll say that a "triple" in a string is a char appearing three times in a row. Return the number of triples in the given string. The triples may overlap.
countTriple("abcXXXabc") → 1
countTriple("xxxabyyyycd") → 3
countTriple("a") → 0
Solution:
public int countTriple(String str) {
int count = 0;
int i = 0;
int len = str.length();
while (i<len-2) {
if(str.substring(i,i+3).matches("(.)\\1\\1")) {
count++;
}
i++;
}
return count;
}