Count of all substrings which satisfies a property
Anonymous User
271

Given a string S, find all substring which satisfy following property

Count of all digits appearing in this sub-string must be equal to k.
ex- 110101 and k = 2
Consider sub-string [0-1] "11" should be included in our answer.
[1-4] "1010" also should be included.
[2-5] - "0101" also should be included
All other sub-strings don't satisfy this property.

Constraints
length of string < 10^5
k < 10^5

Variation - what if k is not given and we are supposed to find all substrings in which all digits have equal count, if a digit is present in the sub-string.

I am able to find O(2^10 n10) solution.

My approach -

  1. Maintain in a vector - all numbers from 1-1023 - call this vector comb
  2. iterate over strings
  3. pick a number from comb, check which bit position are set, the position which are set , you include those in your sub-string and check if there is such sub-string possible with the property. Each such check will 10 comparisons . I am checking this for 1024 number at each position of string.
Comments (0)