Q:

count substring occurrence in every combination

I have a string phahahahoto and I need to find how many times the String haha appear in the above string. If you look closely it appears 2 times.
My code is below and I get the output 1 instead of 2. 
Code is written in java.
Pattern pattern = Pattern.compile("haha");
Matcher matcher = pattern.matcher("phahahahoto");
int count = 0;
while (matcher.find()) {
    count++;
}
System.out.println(count);

A:

Use lookaheads in-order to do overlapping matches. If you clearly noticed that the string haha was overlapped. If you pass haha as regex, it won't do an overlapping match, since the pattern haha matches the first haha substring which leaves you only the last ha part. Lookarounds won't consume any single character. So it would be able to match only the boundaries.
Pattern pattern = Pattern.compile("(?=haha)");
Matcher matcher = pattern.matcher("phahahahoto");
int count = 0;
while (matcher.find()) {
     count++;
}
System.out.println(count);

DEMO
Here it matches the boundary which exists before each haha . See the above demo link.

A:

You can get the count in one line like this also:
int count = "phahahahoto".split("(?=haha)").length - 1;
//=> 2

