instagram

Count number of valid substring in binary string

This algorithm to count number of valid substring in given binary string.

Valid substrings will be 01, 10, 0011, 1100, 000111, 111000, 00001111, 11110000 …. etc

Example :

Input : 0001000
Output : 1
In above example there are only two substrings are possible and they are 01 and 10

Input : 1000001
Output : 2
In above example two substrings are 10 and 01

Input : 1010101
Output : 6
Substrings are : 10, 01, 10, 01, 10, 01 total 6 (six)

Input : 000111000
Output : 4
Substrings are : 01, 10, 000111, 111000 total four.

Algorithm

Share