Count ways to select three indices from Binary String with different adjacent digits
Last Updated :
14 Dec, 2022
Given a binary string S of length N, the task is to find the number of ways to select three indices such that if they are arranged in increasing order of indices no two adjacent indices will have the same value.
Examples:
Input: S = “00110”
Output: 4
Explanation: Below are the possible valid indices:
=> {0, 2, 4} from “00110” forms “010”
=> {0, 3, 4} from “00110” forms “010”
=> {1, 2, 4} from “00110” forms “010”
=> {1, 3, 4} from “00110” forms “010”
No other selection is valid. Thus, there are 4 total ways.
Input: S = “11100”
Output: 0
An approach using the PrefixSum technique.
Keep track of the number of zeros and ones on the left and right at any indices. At any index i, if the ith digit is zero then for this index we can select (number of ones on the left * number of ones on the right). Similarly, if the digit is 1, check for the 0s n left and right.
Keep adding the number of ways for every index in the result and finally return it.
Follow the steps below to implement the above idea:
- Initialize a variable totalZero and totalOne to 0, this will keep track of the total number of zeros and ones in the binary string respectively.
- Initialize a variable currZero and currOne to 0, this will keep track of the number of zeros and ones till ith index.
- Iterate over the string
- If the current digit is zero, increment the totalZero by 1
- Otherwise, increment the totalOne by 1
- Initialize a variable result, to keep track of the answer
- Iterate over the string
- If the current digit is 0
- Add the value of (number of ones to the left * number of ones to the right)
- Increment the count of currZero by 1
- Otherwise,
- Add the value of (number of zeros to the left * number of zeros to the right)
- Increment the count of currOnes by 1
- Finally, return the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long numberOfWays(string s)
{
int n = s.size();
int totalZero = 0, totalOne = 0;
int currZero = 0, currOne = 0;
for ( int i = 0; i < n; i++) {
if (s[i] == '0' )
totalZero++;
else
totalOne++;
}
long long result = 0;
for ( int i = 0; i < n; i++) {
if (s[i] == '0' ) {
result += (currOne * (totalOne - currOne));
currZero++;
}
else {
result += (currZero * (totalZero - currZero));
currOne++;
}
}
return result;
}
int main()
{
string s = "00110" ;
cout << numberOfWays(s) << endl;
s = "11100" ;
cout << numberOfWays(s) << endl;
s = "0000" ;
cout << numberOfWays(s) << endl;
s = "101" ;
cout << numberOfWays(s);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int numberOfWays(String s)
{
int n = s.length();
int totalZero = 0 , totalOne = 0 ;
int currZero = 0 , currOne = 0 ;
for ( int i = 0 ; i < n; i++) {
if (s.charAt(i) == '0' ) {
totalZero++;
}
else {
totalOne++;
}
}
int result = 0 ;
for ( int i = 0 ; i < n; i++) {
if (s.charAt(i) == '0' ) {
result += (currOne * (totalOne - currOne));
currZero++;
}
else {
result
+= (currZero * (totalZero - currZero));
currOne++;
}
}
return result;
}
public static void main(String[] args)
{
String s = "00110" ;
System.out.println(numberOfWays(s));
s = "11100" ;
System.out.println(numberOfWays(s));
s = "0000" ;
System.out.println(numberOfWays(s));
s = "101" ;
System.out.println(numberOfWays(s));
}
}
|
Python3
def numberOfWays(s) :
n = len (s);
totalZero = 0 ; totalOne = 0 ;
currZero = 0 ; currOne = 0 ;
for i in range (n) :
if (s[i] = = '0' ) :
totalZero + = 1 ;
else :
totalOne + = 1 ;
result = 0 ;
for i in range (n) :
if (s[i] = = '0' ) :
result + = (currOne * (totalOne - currOne));
currZero + = 1 ;
else :
result + = (currZero * (totalZero - currZero));
currOne + = 1 ;
return result;
if __name__ = = "__main__" :
s = "00110" ;
print (numberOfWays(s));
s = "11100" ;
print (numberOfWays(s));
s = "0000" ;
print (numberOfWays(s));
s = "101" ;
print (numberOfWays(s));
|
C#
using System;
public class GFG {
static int numberOfWays( string s)
{
int n = s.Length;
int totalZero = 0, totalOne = 0;
int currZero = 0, currOne = 0;
for ( int i = 0; i < n; i++) {
if (s[i] == '0' ) {
totalZero++;
}
else {
totalOne++;
}
}
int result = 0;
for ( int i = 0; i < n; i++) {
if (s[i] == '0' ) {
result += (currOne * (totalOne - currOne));
currZero++;
}
else {
result
+= (currZero * (totalZero - currZero));
currOne++;
}
}
return result;
}
static public void Main()
{
string s = "00110" ;
Console.WriteLine(numberOfWays(s));
s = "11100" ;
Console.WriteLine(numberOfWays(s));
s = "0000" ;
Console.WriteLine(numberOfWays(s));
s = "101" ;
Console.WriteLine(numberOfWays(s));
}
}
|
Javascript
const numberOfWays = (s) => {
let n = s.length;
let totalZero = 0, totalOne = 0;
let currZero = 0, currOne = 0;
for (let i = 0; i < n; i++) {
if (s[i] == '0' )
totalZero++;
else
totalOne++;
}
let result = 0;
for (let i = 0; i < n; i++) {
if (s[i] == '0' ) {
result += (currOne * (totalOne - currOne));
currZero++;
}
else {
result += (currZero * (totalZero - currZero));
currOne++;
}
}
return result;
}
let s = "00110" ;
console.log(`${numberOfWays(s)}<br/>`);
s = "11100" ;
console.log(`${numberOfWays(s)}<br/>`);
s = "0000" ;
console.log(`${numberOfWays(s)}<br/>`);
s = "101" ;
console.log(`${numberOfWays(s)}<br/>`);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Related Articles:
Share your thoughts in the comments
Please Login to comment...