Find the number of Substrings with even number of K
Last Updated :
18 Apr, 2023
Given a string str of length N and an integer K, the task is to count the number of substrings with even numbers of K. It means the count of ‘K’ in each substring will be even and must not be zero.
Examples:
Input: N = 4, str = “2131”, K = 1
Output: 2
Explanation: There are two substrings “2131” and ‘131″ having an even number of K.
Input: N = 4, str = “1121”, K = 2
Output: 0
Explanation: No, substring exists having an even number of K.
Approach: This can be solved with the following idea:
We can precompute an array that counts the occurrence of K. After computing this we can traverse the for loops two times to find the ranges and checking the occurrence between the range is odd or even. For more clarity follow the process.
Below are the steps of implementation:
- Declare a new array oneoccur[] of the same size as the input string.
- Run a for loop to traverse the input string.
- For each index add the value of the previous value of the precomputing array if the element is other than K and if the current element is K add the previous element incremented by 1.
- Now we have all occurrences of K till all indexes.
- Now we have to find all the ranges possible. For that traverse a nested for loop.
- In each range find the number of occurring K in this range.
- Check whether the number is even or not and check if it is not equal to 0.
- If it is even and not equal to zero, then increment the counter variable.
- After traversing all possible ranges Output the cnt.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void oneoccur(string arr, int n, int prefixSum[], char K)
{
if (arr[0] == K) {
prefixSum[0] = 1;
}
else {
prefixSum[0] = 0;
}
for ( int i = 1; i < n; i++)
{
if (arr[i] == K)
prefixSum[i] = prefixSum[i - 1] + 1;
else
prefixSum[i] = prefixSum[i - 1];
}
}
void findevenone(string s, int n, char K)
{
int onencnt[n];
oneoccur(s, n, onencnt, K);
int cnt = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i; j < n; j++) {
int onecnt = 0;
if (i == 0) {
if (onencnt[j] % 2 == 0
&& onencnt[j] != 0) {
cnt++;
}
}
else {
if ((onencnt[j] - onencnt[i - 1]) % 2 == 0
&& (onencnt[j] - onencnt[i - 1]) != 0) {
cnt++;
}
}
}
}
cout << cnt << endl;
return ;
}
int main()
{
string str = "2131" ;
char K = '1' ;
int size = str.size();
findevenone(str, size, K);
return 0;
}
|
Java
import java.util.*;
public class Main {
static void oneoccur(String arr, int n, int prefixSum[], char K) {
if (arr.charAt( 0 ) == K) {
prefixSum[ 0 ] = 1 ;
} else {
prefixSum[ 0 ] = 0 ;
}
for ( int i = 1 ; i < n; i++) {
if (arr.charAt(i) == K) {
prefixSum[i] = prefixSum[i - 1 ] + 1 ;
} else {
prefixSum[i] = prefixSum[i - 1 ];
}
}
}
static void findevenone(String s, int n, char K) {
int onencnt[] = new int [n];
oneoccur(s, n, onencnt, K);
int cnt = 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = i; j < n; j++) {
int onecnt = 0 ;
if (i == 0 ) {
if (onencnt[j] % 2 == 0 && onencnt[j] != 0 ) {
cnt++;
}
} else {
if ((onencnt[j] - onencnt[i - 1 ]) % 2 == 0 && (onencnt[j] - onencnt[i - 1 ]) != 0 ) {
cnt++;
}
}
}
}
System.out.println(cnt);
}
public static void main(String[] args) {
String str = "2131" ;
char K = '1' ;
int size = str.length();
findevenone(str, size, K);
}
}
|
Python3
def oneoccur(arr, n, prefixSum, K):
if arr[ 0 ] = = K:
prefixSum[ 0 ] = 1
else :
prefixSum[ 0 ] = 0
for i in range ( 1 , n):
if arr[i] = = K:
prefixSum[i] = prefixSum[i - 1 ] + 1
else :
prefixSum[i] = prefixSum[i - 1 ]
def findevenone(s, n, K):
onecnt = [ 0 ] * n
oneoccur(s, n, onecnt, K)
cnt = 0
for i in range (n):
for j in range (i, n):
if i = = 0 :
if onecnt[j] % 2 = = 0 and onecnt[j] ! = 0 :
cnt + = 1
else :
if (onecnt[j] - onecnt[i - 1 ]) % 2 = = 0 and (onecnt[j] - onecnt[i - 1 ]) ! = 0 :
cnt + = 1
print (cnt)
if __name__ = = '__main__' :
str = "2131"
K = '1'
size = len ( str )
findevenone( str , size, K)
|
C#
using System;
class Program {
static void OneOccur( string arr, int n, int [] prefixSum,
char K)
{
if (arr[0] == K) {
prefixSum[0] = 1;
}
else {
prefixSum[0] = 0;
}
for ( int i = 1; i < n; i++) {
if (arr[i] == K)
prefixSum[i] = prefixSum[i - 1] + 1;
else
prefixSum[i] = prefixSum[i - 1];
}
}
static void FindEvenOne( string s, int n, char K)
{
int [] oneCnt = new int [n];
OneOccur(s, n, oneCnt, K);
int cnt = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i; j < n; j++) {
int oneCntInRange = 0;
if (i == 0) {
if (oneCnt[j] % 2 == 0
&& oneCnt[j] != 0) {
cnt++;
}
}
else {
if ((oneCnt[j] - oneCnt[i - 1]) % 2 == 0
&& (oneCnt[j] - oneCnt[i - 1])
!= 0) {
cnt++;
}
}
}
}
Console.WriteLine(cnt);
}
static void Main()
{
string str = "2131" ;
char K = '1' ;
int size = str.Length;
FindEvenOne(str, size, K);
}
}
|
Javascript
function oneoccur( arr, n, prefixSum, K)
{
if (arr[0] == K) {
prefixSum[0] = 1;
}
else {
prefixSum[0] = 0;
}
for (let i = 1; i < n; i++)
{
if (arr[i] == K)
prefixSum[i] = prefixSum[i - 1] + 1;
else
prefixSum[i] = prefixSum[i - 1];
}
}
function findevenone( s, n, K)
{
let onencnt= new Array(n);
oneoccur(s, n, onencnt, K);
let cnt = 0;
for (let i = 0; i < n; i++) {
for (let j = i; j < n; j++) {
let onecnt = 0;
if (i == 0) {
if (onencnt[j] % 2 == 0
&& onencnt[j] != 0) {
cnt++;
}
}
else {
if ((onencnt[j] - onencnt[i - 1]) % 2 == 0
&& (onencnt[j] - onencnt[i - 1]) != 0) {
cnt++;
}
}
}
}
console.log(cnt);
return ;
}
let str = "2131" ;
let K = '1' ;
let size = str.length;
findevenone(str, size, K);
|
Time complexity: O(N2)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...