A string contains many patterns of the form 1(0+)1 where (0+) represents any non-empty consecutive sequence of 0's. The patterns are allowed to overlap.
For example, consider string "1101001", we can see there are two consecutive sequences "1(0)1" and "1(00)1" which are of the form 1(0+)1.
public class Solution {
static int patternCount(String s){
String[] sArray = s.split("1");
for(String str : sArray) {
if(Pattern.matches("[0]+", str)) {
count++;
}
}
return count;
}
public static void main(String[] args) {
int result = patternCount("1001010001");
System.out.println(result);//3
}
}
Sample Input:
100001abc101, 1001ab010abc01001, 1001010001
Sample Output:
2 2 3
But still something i feel might fail in future could you please help me to optimize my code as per the Requirement