Number of alternating substrings from a given Binary String
Last Updated :
14 Oct, 2023
Given a binary string of size N, the task is to count the number of alternating substrings that are present in the string S.
Examples:
Input: S = "0010"
Output: 7
Explanation:
All the substring of the string S are: {"0", "00", "001", "0010", "0", "01", "010", "1", "10", "0"}
Strings that are alternating: {"0", "0", "01", "010", "1", "10", "0"}.
Hence, the answer is 7.
Input: S = "010"
Output: 6
Naive Approach: The simplest approach to solve this problem is to first, find all the substrings of the string S, then check for every string if it is alternating or not.
Time Complexity: O(N3)
Auxiliary Space: O(N2)
Efficient Approach: This problem has Overlapping Subproblems property and Optimal Substructure property. So this problem can be solved using Dynamic Programming. Follow the steps below to solve this problem:
- Declare a dp array, where dp[i][j] stores the number of alternating strings that start with char i and are in the range [j, N-1].
- Iterate in the range [N-1, 0] using the variable i and perform the following steps:
- If i is equal to N-1, then if current character is '1', then assign 1 to dp[1][i], otherwise assign 1 to dp[0][i].
- Else, if current character is '0', then update dp[0][i] to 1+dp[1][i+1], otherwise, update dp[1][i] to 1+dp[0][i+1].
- Initialize a variable say ans as 0 to store the number of substrings that are alternating.
- Iterate in the range [0, N-1] using the variable i and perform the following steps:
- Update ans as max of dp[0][i] and dp[1][i].
- Finally, print the value obtained in ans as the answer.
Below is the implementation of the above approach:
C++
// c++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to count number of alternating
// substrings from a given binary string
int countAlternatingSubstrings(string S, int N)
{
// Initialize dp array, where dp[i][j] stores
// the number of alternating strings starts
// with i and having j elements.
vector<vector<int> > dp(2, vector<int>(N, 0));
// Traverse the string from the end
for (int i = N - 1; i >= 0; i--) {
// If i is equal to N - 1
if (i == N - 1) {
if (S[i] == '1')
dp[1][i] = 1;
else
dp[0][i] = 1;
}
// Otherwise,
else {
// Increment count of
// substrings starting at i
// and has 0 in the beginning
if (S[i] == '0')
dp[0][i] = 1 + dp[1][i + 1];
// Increment count of
// substrings starting at i
// and has 1 in the beginning
else
dp[1][i] = 1 + dp[0][i + 1];
}
}
// Stores the result
int ans = 0;
// Iterate in the range [0, N-1]
for (int i = 0; i < N; i++) {
// Update ans
ans += max(dp[0][i], dp[1][i]);
}
// Return the ans
return ans;
}
// Driver code
int main()
{
// Given Input
string S = "0010";
int N = S.length();
// Function call
cout << countAlternatingSubstrings(S, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to count number of alternating
// substrings from a given binary string
static int countAlternatingSubstrings(String S, int N)
{
// Initialize dp array, where dp[i][j] stores
// the number of alternating strings starts
// with i and having j elements.
int[][] dp = new int[2][N];
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < N; j++)
{
dp[i][j] = 0;
}
}
// Traverse the string from the end
for(int i = N - 1; i >= 0; i--)
{
// If i is equal to N - 1
if (i == N - 1)
{
if (S.charAt(i) == '1')
dp[1][i] = 1;
else
dp[0][i] = 1;
}
// Otherwise,
else
{
// Increment count of
// substrings starting at i
// and has 0 in the beginning
if (S.charAt(i) == '0')
dp[0][i] = 1 + dp[1][i + 1];
// Increment count of
// substrings starting at i
// and has 1 in the beginning
else
dp[1][i] = 1 + dp[0][i + 1];
}
}
// Stores the result
int ans = 0;
// Iterate in the range [0, N-1]
for(int i = 0; i < N; i++)
{
// Update ans
ans += Math.max(dp[0][i], dp[1][i]);
}
// Return the ans
return ans;
}
// Driver Code
public static void main(String[] args)
{
// Given Input
String S = "0010";
int N = S.length();
// Function call
System.out.print(countAlternatingSubstrings(S, N));
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 program for the above approach
# Function to count number of alternating
# substrings from a given binary string
def countAlternatingSubstrings(S, N):
# Initialize dp array, where dp[i][j] stores
# the number of alternating strings starts
# with i and having j elements.
dp = [[0 for i in range(N)]
for i in range(2)]
# Traverse the string from the end
for i in range(N - 1, -1, -1):
# If i is equal to N - 1
if (i == N - 1):
if (S[i] == '1'):
dp[1][i] = 1
else:
dp[0][i] = 1
# Otherwise,
else:
# Increment count of
# substrings starting at i
# and has 0 in the beginning
if (S[i] == '0'):
dp[0][i] = 1 + dp[1][i + 1]
# Increment count of
# substrings starting at i
# and has 1 in the beginning
else:
dp[1][i] = 1 + dp[0][i + 1]
# Stores the result
ans = 0
# Iterate in the range [0, N-1]
for i in range(N):
# Update ans
ans += max(dp[0][i], dp[1][i])
# Return the ans
return ans
# Driver code
if __name__ == '__main__':
# Given Input
S = "0010"
N = len(S)
# Function call
print (countAlternatingSubstrings(S, N))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Function to count number of alternating
// substrings from a given binary string
static int countAlternatingSubstrings(string S, int N)
{
// Initialize dp array, where dp[i][j] stores
// the number of alternating strings starts
// with i and having j elements.
int[,] dp = new int[2, N];
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < N; j++)
{
dp[i, j] = 0;
}
}
// Traverse the string from the end
for(int i = N - 1; i >= 0; i--)
{
// If i is equal to N - 1
if (i == N - 1)
{
if (S[i] == '1')
dp[1, i] = 1;
else
dp[0, i] = 1;
}
// Otherwise,
else
{
// Increment count of
// substrings starting at i
// and has 0 in the beginning
if (S[i] == '0')
dp[0, i] = 1 + dp[1, i + 1];
// Increment count of
// substrings starting at i
// and has 1 in the beginning
else
dp[1, i] = 1 + dp[0, i + 1];
}
}
// Stores the result
int ans = 0;
// Iterate in the range [0, N-1]
for(int i = 0; i < N; i++)
{
// Update ans
ans += Math.Max(dp[0, i], dp[1, i]);
}
// Return the ans
return ans;
}
// Driver Code
public static void Main()
{
// Given Input
string S = "0010";
int N = S.Length;
// Function call
Console.Write(countAlternatingSubstrings(S, N));
}
}
// This code is contributed by target_2.
JavaScript
<script>
// Javascript program for the above approach
// Function to count number of alternating
// substrings from a given binary string
function countAlternatingSubstrings(S, N)
{
// Initialize dp array, where dp[i][j] stores
// the number of alternating strings starts
// with i and having j elements.
var dp = new Array(2);
dp[0] = Array(N).fill(0);
dp[1] = Array(N).fill(0);
var i;
// Traverse the string from the end
for(i = N - 1; i >= 0; i--)
{
// If i is equal to N - 1
if (i == N - 1)
{
if (S[i] == '1')
dp[1][i] = 1;
else
dp[0][i] = 1;
}
// Otherwise,
else
{
// Increment count of
// substrings starting at i
// and has 0 in the beginning
if (S[i] == '0')
dp[0][i] = 1 + dp[1][i + 1];
// Increment count of
// substrings starting at i
// and has 1 in the beginning
else
dp[1][i] = 1 + dp[0][i + 1];
}
}
// Stores the result
var ans = 0;
// Iterate in the range [0, N-1]
for(i = 0; i < N; i++)
{
// Update ans
ans += Math.max(dp[0][i], dp[1][i]);
}
// Return the ans
return ans;
}
// Driver code
// Given Input
var S = "0010";
var N = S.length;
// Function call
document.write(countAlternatingSubstrings(S, N));
// This code is contributed by SURENDRA_GANGWAR
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Efficient approach: Space optimization O(1)
In previous approach we the current value dp[i] is only depend upon the previous 2 values i.e. dp[0][i-1], dp[1][i-1] . So to optimize the space we can keep track of previous and current values by the help of variables prev0, prev1 and curr0 ,curr1 which will reduce the space complexity from O(N) to O(1).
Implementation Steps:
- Create 2 variables prev0 and prev1 to keep track of previous values of DP.
- Initialize base case prev0 = prev1 = 0.
- Create variables curr0 and curr1 to store current value.
- Iterate over subproblem using loop and update curr0 , curr1.
- After every iteration update prev0 and prev1 for further iterations.
- At last return ans containing maximum of curr0 and curr1.
C++
// C++ code for above approach
#include <bits/stdc++.h>
using namespace std;
// Function to count the number of alternating substrings
int countAlternatingSubstrings(string S, int N)
{
// Initialize the previous counts for 0s and 1s to 0
int prev0 = 0, prev1 = 0;
// Initialize the current counts for 0s and 1s
int curr0, curr1;
// to store answer
int ans = 0;
// iterate over subproblems to get the current
// value from previous computations
for (int i = N - 1; i >= 0; i--) {
// If the current character is 0
if (S[i] == '0') {
curr0 = 1 + prev1;
curr1 = 0;
}
// If the current character is 1
else {
curr1 = 1 + prev0;
curr0 = 0;
}
// Add the maximum of the current counts
// for 0s and 1s to the answer variable
ans += max(curr0, curr1);
// assigning values for
// further iterations
prev0 = curr0;
prev1 = curr1;
}
// Return the answer
return ans;
}
// Driver Code
int main()
{
string S = "0010";
int N = S.length();
// funcition call
cout << countAlternatingSubstrings(S, N);
return 0;
}
// --- by bhardwajji
Java
// Java code for above approach
import java.util.*;
class Main {
// Function to count the number of alternating substrings
public static int countAlternatingSubstrings(String S, int N) {
// Initialize the previous counts for 0s and 1s to 0
int prev0 = 0, prev1 = 0;
// Initialize the current counts for 0s and 1s
int curr0, curr1;
// to store answer
int ans = 0;
// iterate over subproblems to get the current
// value from previous computations
for (int i = N - 1; i >= 0; i--) {
// If the current character is 0
if (S.charAt(i) == '0') {
curr0 = 1 + prev1;
curr1 = 0;
}
// If the current character is 1
else {
curr1 = 1 + prev0;
curr0 = 0;
}
// Add the maximum of the current counts
// for 0s and 1s to the answer variable
ans += Math.max(curr0, curr1);
// assigning values for
// further iterations
prev0 = curr0;
prev1 = curr1;
}
// Return the answer
return ans;
}
// Driver Code
public static void main(String[] args) {
String S = "0010";
int N = S.length();
// function call
System.out.println(countAlternatingSubstrings(S, N));
}
}
Python
def count_alternating_substrings_optimized(S):
# Initialize variables to keep track of counts and the result
prev0 = prev1 = curr0 = curr1 = ans = 0
# Iterate over the string in reverse
for i in range(len(S) - 1, -1, -1):
# If the current character is '0'
if S[i] == '0':
# Update counts for '0' and '1'
curr0, curr1 = 1 + prev1, 0
else:
# Update counts for '1' and '0'
curr1, curr0 = 1 + prev0, 0
# Add the maximum of the current counts to the result
ans += max(curr0, curr1)
# Update previous counts for the next iteration
prev0, prev1 = curr0, curr1
# Return the final result
return ans
# Driver Code
if __name__ == "__main__":
# Example input
S = "0010"
# Call the function and print the result
print(count_alternating_substrings_optimized(S))
C#
using System;
class MainClass {
// Function to count the number of alternating substrings
public static int CountAlternatingSubstrings(string S, int N) {
// Initialize the previous counts for 0s and 1s to 0
int prev0 = 0, prev1 = 0;
// Initialize the current counts for 0s and 1s
int curr0, curr1;
// to store answer
int ans = 0;
// iterate over subproblems to get the current
// value from previous computations
for (int i = N - 1; i >= 0; i--) {
// If the current character is '0'
if (S[i] == '0') {
curr0 = 1 + prev1;
curr1 = 0;
}
// If the current character is '1'
else {
curr1 = 1 + prev0;
curr0 = 0;
}
// Add the maximum of the current counts
// for 0s and 1s to the answer variable
ans += Math.Max(curr0, curr1);
// assigning values for
// further iterations
prev0 = curr0;
prev1 = curr1;
}
// Return the answer
return ans;
}
// Driver Code
public static void Main(string[] args) {
string S = "0010";
int N = S.Length;
// function call
Console.WriteLine(CountAlternatingSubstrings(S, N));
}
}
JavaScript
// Function to count the number of alternating substrings
function countAlternatingSubstrings(S) {
// Initialize the previous counts for 0s and 1s to 0
let prev0 = 0;
let prev1 = 0;
// Initialize the current counts for 0s and 1s
let curr0, curr1;
// Initialize the answer variable
let ans = 0;
// Iterate over the characters in the string from right to left
for (let i = S.length - 1; i >= 0; i--) {
// If the current character is '0'
if (S[i] === '0') {
curr0 = 1 + prev1;
curr1 = 0;
}
// If the current character is '1'
else {
curr1 = 1 + prev0;
curr0 = 0;
}
// Add the maximum of the current counts for '0's and '1's to the answer
ans += Math.max(curr0, curr1);
// Assign values for further iterations
prev0 = curr0;
prev1 = curr1;
}
// Return the answer
return ans;
}
// Driver Code
const S = "0010";
const result = countAlternatingSubstrings(S);
console.log(result); // Output the result
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
XOR of all substrings of a given Binary String
Given a binary string str of size N, the task is to calculate the bitwise XOR of all substrings of str. Examples: Input: str = "11"Output: 11Explanation: The substrings of "11" are: 1, 1, and 11.Their XOR = 1 â 1 â 11 = 11 Input: str = "110"Output: 111Explanation: The substrings of 110 are: 1, 1, 0,
6 min read
Minimum substring reversals required to make given Binary String alternating
Given a binary string S of length N, the task is to count the minimum number substrings of S that is required to be reversed to make the string S alternating. If it is not possible to make string alternating, then print "-1". Examples: Input: S = "10001110"Output: 2Explanation:In the first operation
7 min read
Number of flips to make binary string alternate | Set 1
Given a binary string, that is it contains only 0s and 1s. We need to make this string a sequence of alternate characters by flipping some of the bits, our goal is to minimize the number of bits to be flipped. Examples : Input : str = â001â Output : 1 Minimum number of flips required = 1 We can flip
8 min read
Number of even substrings in a string of digits
Given a string of digits 0 - 9. The task is to count a number of substrings which when converting into integer form an even number. Examples : Input : str = "1234".Output : 6"2", "4", "12", "34", "234", "1234" are 6 substring which are even.Input : str = "154".Output : 3Input : str = "15".Output : 0
9 min read
Minimum number of flips with rotation to make binary string alternating
Given a binary string S of 0s and 1s. The task is to make the given string a sequence of alternate characters by using the below operations: Remove some prefix from the start and append it to the end.Flip some or every bit in the given string. Print the minimum number of bits to be flipped to make t
11 min read
Count of bitonic substrings from the given string
Given a string str, the task is to count all the bitonic substrings of the given string. A bitonic substring is a substring of the given string in which elements are either strictly increasing or strictly decreasing, or first increasing and then decreasing. Examples: Input: str = "bade"Output: 8Expl
7 min read
Number of substrings with odd decimal value in a binary string
Given a binary string containing only 0's and 1's. Write a program to find number of sub-strings of this string whose decimal representation is odd. Examples : Input : 101 Output : 3 Explanation : Substrings with odd decimal representation are: {1, 1, 101} Input : 1101 Output : 6 Explanation : Subst
6 min read
Minimum number of replacements to make the binary string alternating | Set 2
Given a binary string str, the task is to find the minimum number of characters in the string that have to be replaced in order to make the string alternating (i.e. of the form 01010101... or 10101010...).Examples: Input: str = "1100" Output: 2 Replace 2nd character with '0' and 3rd character with '
5 min read
Minimum swaps required to make a binary string alternating
You are given a binary string of even length (2N) and an equal number of 0's (N) and 1's (N). What is the minimum number of swaps to make the string alternating? A binary string is alternating if no two consecutive elements are equal. Examples: Input : 000111Output : 1Explanation : Swap index 2 and
11 min read
Number of sub-strings in a given binary string divisible by 2
Given binary string str of length N, the task is to find the count of substrings of str which are divisible by 2. Leading zeros in a substring are allowed. Examples: Input: str = "101" Output: 2 "0" and "10" are the only substrings which are divisible by 2. Input: str = "10010" Output: 10 Naive appr
4 min read