![]() |
VOOZH | about |
Given a binary string S i.e. which consists only of 0's and 1's. Calculate the number of substrings of S which are palindromes. String S contains at most two 1's.
Examples:
Input: S = "011"
Output: 4
Explanation: "0", "1", "1" and "11" are the palindromic substrings.Input: S = "0"
Output: 1
Explanation: "0" is the only palindromic substring.
Approach: This can be solved with the following idea:
Using some mathematical observation can find out number of possible palindrome substring of size 2. Rest of all size, we can find out by reducing indexes from left and right side. For more clarification, see steps.
Below are the steps to solve the problem:
Below is the implementation of the code:
10
Time Complexity: O(N)
Auxiliary Space: O(1)