Count substring of Binary string such that each character belongs to a palindrome of size greater than 1
Last Updated :
30 Dec, 2021
Given binary string str, the task is to count the number of substrings of the given string str such that each character of the substring belongs to a palindromic substring of length at least 2.
Examples:
Input: S = “00111”
Output: 6
Explanation:
There are 6 such substrings in the given string such that each character belongs to a palindrome of size greater than 1 as {“00”, “0011”, “00111”, “11”, “111”, “11”}
Input: S = “0001011”
Output: 15
Approach: The idea is to count the substrings in which every character doesn’t belong to a palindromic substring and then subtract this count from the total number of possible substrings of the string of size greater than 1. Below is the illustration of the steps:
- It can be observed that if we take the substring a2a3….ak-1 (i.e. without starting and ending character), then each of its characters may belong to some palindrome.Proof:
If ai == ai-1 or ai == ai+1,
Then it belongs to a palindrome of length 2.
Otherwise, If ai != ai-1,
ai != ai+1 and ai+1 == ai-1,
Then, It belongs to a palindrome of size 3.
- Therefore, there are four patterns of substrings in which each character doesn’t belong to the palindrome:
- “0111….11”
- “100…..00”
- “111….110”
- “000….001”
- Finally, subtract this count from the total number of substrings possible of length greater than 1.
Count = (N*(N – 1)/2) – (Count of the substrings in which each character doesn’t belongs to palindrome)
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countSubstrings(string s)
{
int n = s.length();
int answer = (n * (n - 1)) / 2;
int cnt = 1;
vector< int > v;
for ( int i = 1; i < n; i++) {
if (s[i] == s[i - 1])
cnt++;
else {
v.push_back(cnt);
cnt = 1;
}
}
if (cnt > 0)
v.push_back(cnt);
for ( int i = 0;
i < v.size() - 1; i++) {
answer -= (v[i]
+ v[i + 1]
- 1);
}
return answer;
}
int main()
{
string s = "00111" ;
cout << countSubstrings(s);
return 0;
}
|
Java
import java.util.*;
class GFG{
public static int countSubstrings(String s)
{
int n = s.length();
int answer = (n * (n - 1 )) / 2 ;
int cnt = 1 ;
Vector<Integer> v = new Vector<Integer>();
for ( int i = 1 ; i < n; i++)
{
if (s.charAt(i) == s.charAt(i - 1 ))
cnt++;
else
{
v.add(cnt);
cnt = 1 ;
}
}
if (cnt > 0 )
v.add(cnt);
for ( int i = 0 ; i < v.size() - 1 ; i++)
{
answer -= (v.get(i) +
v.get(i + 1 ) - 1 );
}
return answer;
}
public static void main(String[] args)
{
String s = "00111" ;
System.out.print(countSubstrings(s));
}
}
|
Python3
def countSubstrings (s):
n = len (s)
answer = (n * (n - 1 )) / / 2
cnt = 1
v = []
for i in range ( 1 , n):
if (s[i] = = s[i - 1 ]):
cnt + = 1
else :
v.append(cnt)
cnt = 1
if (cnt > 0 ):
v.append(cnt)
for i in range ( len (v) - 1 ):
answer - = (v[i] + v[i + 1 ] - 1 )
return answer
if __name__ = = '__main__' :
s = "00111"
print (countSubstrings(s))
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static int countSubstrings(String s)
{
int n = s.Length;
int answer = (n * (n - 1)) / 2;
int cnt = 1;
List< int > v = new List< int >();
for ( int i = 1; i < n; i++)
{
if (s[i] == s[i-1])
cnt++;
else
{
v.Add(cnt);
cnt = 1;
}
}
if (cnt > 0)
v.Add(cnt);
for ( int i = 0; i < v.Count - 1; i++)
{
answer -= (v[i] +
v[i + 1] - 1);
}
return answer;
}
public static void Main(String[] args)
{
String s = "00111" ;
Console.Write(countSubstrings(s));
}
}
|
Javascript
<script>
function countSubstrings(s)
{
var n = s.length;
var answer = (n * (n - 1)) / 2;
var cnt = 1;
var v =[];
for ( var i = 1; i < n; i++)
{
if (s.charAt(i) == s.charAt(i - 1))
cnt++;
else
{
v.push(cnt);
cnt = 1;
}
}
if (cnt > 0)
v.push(cnt);
for ( var i = 0; i < v.length - 1; i++)
{
answer -= (v[i] +
v[i + 1] - 1);
}
return answer;
}
var s = "00111" ;
document.write(countSubstrings(s));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...