count the substring of 1's
The given binary string S and integer K, to find count the number of substrings that contain exactly K ones.
Example-1:
Input : s = "10010" k = 1 Output : 9 Explain : The 9 substrings containing k 1's are, “1”, “1”, “10”, “01”, “10”, “100”, “001”, “010” and “0010”.
Example-2:
Input : s = "1011" k = 2 Output : 3 Explain : The 3 substrings containing k 1's are, “11”, “101”, “011”.
Solution
s = "10010" k = 1 l = [] for i in range(k,len(s)): for j in range(0,len(s)): a = s[j:j+i] if(len(a)==i and a.count("1")==k): l.append(a) print(len(l))
Output
9