Count of strings that does not contain Arc intersection
Given an array arr[] consisting of N binary strings, the task is to count the number of strings that does not contain any Arc Intersection.
Connecting consecutive pairs of identical letters using Arcs, if there is an intersection obtained, then it is known as Arc Intersection. Below is the illustration of the same.
Examples:
Input: arr[] = {“0101”, “0011”, “0110”}
Output: 2
Explanation: The string “0101” have Arc Intersection. Therefore, the count of strings that doesn’t have any Arc Intersection is 2.
Input: arr[] = {“0011”, “0110”, “00011000”}
Output: 3
Explanation: All the given strings doesn’t have any Arc Intersection. Therefore, the count is 3.
Naive Approach: The simplest approach is to traverse the array and check for each string, if similar characters are grouped together at consecutive indices or not. If found to be true, keep incrementing count of such strings. Finally, print the value of count obtained.
Time Complexity: O(N*M2), where M is the maximum length of string in the given array.
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to use Stack. Follow the steps below to solve the problem:
- Initialize count, to store the count of strings that doesn’t contain any arc intersection.
- Initialize a stack to store every character of the string into it.
- Iterate the given string and perform the following operations:
- After completing the above steps, if stack is empty, then it doesn’t contain any arc intersection.
- Follow the Step 2 to Step 4 for each string in the array to check whether string contains arc intersection or not. If it doesn’t contain then count this string.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int arcIntersection(string S, int len)
{
stack< char > stk;
for ( int i = 0; i < len; i++) {
stk.push(S[i]);
if (stk.size() >= 2) {
char temp = stk.top();
stk.pop();
if (stk.top() == temp) {
stk.pop();
}
else {
stk.push(temp);
}
}
}
if (stk.empty())
return 1;
return 0;
}
void countString(string arr[], int N)
{
int count = 0;
for ( int i = 0; i < N; i++) {
int len = arr[i].length();
count += arcIntersection(
arr[i], len);
}
cout << count << endl;
}
int main()
{
string arr[] = { "0101" , "0011" , "0110" };
int N = sizeof (arr) / sizeof (arr[0]);
countString(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int arcIntersection(String S, int len)
{
Stack<Character> stk = new Stack<>();
for ( int i = 0 ; i < len; i++)
{
stk.push(S.charAt(i));
if (stk.size() >= 2 )
{
char temp = stk.peek();
stk.pop();
if (stk.peek() == temp)
{
stk.pop();
}
else
{
stk.add(temp);
}
}
}
if (stk.isEmpty())
return 1 ;
return 0 ;
}
static void countString(String arr[], int N)
{
int count = 0 ;
for ( int i = 0 ; i < N; i++)
{
int len = arr[i].length();
count += arcIntersection(
arr[i], len);
}
System.out.print(count + "\n" );
}
public static void main(String[] args)
{
String arr[] = { "0101" , "0011" , "0110" };
int N = arr.length;
countString(arr, N);
}
}
|
Python3
def arcIntersection(S, lenn):
stk = []
for i in range (lenn):
stk.append(S[i])
if ( len (stk) > = 2 ):
temp = stk[ - 1 ]
del stk[ - 1 ]
if (stk[ - 1 ] = = temp):
del stk[ - 1 ]
else :
stk.append(temp)
if ( len (stk) = = 0 ):
return 1
return 0
def countString(arr, N):
count = 0
for i in range (N):
lenn = len (arr[i])
count + = arcIntersection(arr[i], lenn)
print (count)
if __name__ = = '__main__' :
arr = [ "0101" , "0011" , "0110" ]
N = len (arr)
countString(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int arcIntersection(String S, int len)
{
Stack< char > stk = new Stack< char >();
for ( int i = 0; i < len; i++)
{
stk.Push(S[i]);
if (stk.Count >= 2)
{
char temp = stk.Peek();
stk.Pop();
if (stk.Peek() == temp)
{
stk.Pop();
}
else
{
stk.Push(temp);
}
}
}
if (stk.Count == 0)
return 1;
return 0;
}
static void countString(String []arr, int N)
{
int count = 0;
for ( int i = 0; i < N; i++)
{
int len = arr[i].Length;
count += arcIntersection(
arr[i], len);
}
Console.Write(count + "\n" );
}
public static void Main(String[] args)
{
String [] arr = { "0101" , "0011" , "0110" };
int N = arr.Length;
countString(arr, N);
}
}
|
Javascript
<script>
function arcIntersection(S, len)
{
var stk = [];
for ( var i = 0; i < len; i++) {
stk.push(S[i]);
if (stk.length >= 2) {
var temp = stk[stk.length-1];
stk.pop();
if (stk[stk.length-1] == temp) {
stk.pop();
}
else {
stk.push(temp);
}
}
}
if (stk.length==0)
return 1;
return 0;
}
function countString(arr, N)
{
var count = 0;
for ( var i = 0; i < N; i++) {
var len = arr[i].length;
count += arcIntersection(
arr[i], len);
}
document.write( count + "<br>" );
}
var arr = [ "0101" , "0011" , "0110" ];
var N = arr.length;
countString(arr, N);
</script>
|
Time Complexity: O(N*M), where M is the maximum length of string in the given array.
Auxiliary Space: O(M), where M is the maximum length of string in the given array.
Last Updated :
14 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...