I am assigned a string consisting only of numbers from 0 to 9. I want to calculate how many substrings of them have a power of 2.
For example, for substation 2560616, substring 256 and 16 are 2. I need to calculate how many such substrings are in any given substring.
Note that the substring is very large, so brute force cannot work. Therefore, I basically want to solve two problems.
- How to efficiently count all substrings that have power 2
- How to efficiently calculate if a substring is power 2
I think there might be a DP approach, but I'm not sure about that.
source
share