Open In App

Count of strings that does not contain Arc intersection

Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Initialize count, to store the count of strings that doesn’t contain any arc intersection.
  2. Initialize a stack to store every character of the string into it.
  3. Iterate the given string and perform the following operations:
  4. After completing the above steps, if stack is empty, then it doesn’t contain any arc intersection.
  5. 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++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if there is arc
// intersection or not
int arcIntersection(string S, int len)
{
    stack<char> stk;
 
    // Traverse the string S
    for (int i = 0; i < len; i++) {
 
        // Insert all the elements in
        // the stack one by one
        stk.push(S[i]);
 
        if (stk.size() >= 2) {
 
            // Extract the top element
            char temp = stk.top();
 
            // Pop out the top element
            stk.pop();
 
            // Check if the top element
            // is same as the popped element
            if (stk.top() == temp) {
                stk.pop();
            }
 
            // Otherwise
            else {
                stk.push(temp);
            }
        }
    }
 
    // If the stack is empty
    if (stk.empty())
        return 1;
    return 0;
}
 
// Function to check if there is arc
// intersection or not for the given
// array of strings
void countString(string arr[], int N)
{
    // Stores count of string not
    // having arc intersection
    int count = 0;
 
    // Iterate through array
    for (int i = 0; i < N; i++) {
 
        // Length of every string
        int len = arr[i].length();
 
        // Function Call
        count += arcIntersection(
            arr[i], len);
    }
 
    // Print the desired count
    cout << count << endl;
}
 
// Driver Code
int main()
{
    string arr[] = { "0101", "0011", "0110" };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    countString(arr, N);
 
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
 
class GFG
{
 
// Function to check if there is arc
// intersection or not
static int arcIntersection(String S, int len)
{
    Stack<Character> stk  = new Stack<>();
 
    // Traverse the String S
    for (int i = 0; i < len; i++)
    {
 
        // Insert all the elements in
        // the stack one by one
        stk.push(S.charAt(i));
 
        if (stk.size() >= 2)
        {
 
            // Extract the top element
            char temp = stk.peek();
 
            // Pop out the top element
            stk.pop();
 
            // Check if the top element
            // is same as the popped element
            if (stk.peek() == temp)
            {
                stk.pop();
            }
 
            // Otherwise
            else
            {
                stk.add(temp);
            }
        }
    }
 
    // If the stack is empty
    if (stk.isEmpty())
        return 1;
    return 0;
}
 
// Function to check if there is arc
// intersection or not for the given
// array of Strings
static void countString(String arr[], int N)
{
   
    // Stores count of String not
    // having arc intersection
    int count = 0;
 
    // Iterate through array
    for (int i = 0; i < N; i++)
    {
 
        // Length of every String
        int len = arr[i].length();
 
        // Function Call
        count += arcIntersection(
            arr[i], len);
    }
 
    // Print the desired count
    System.out.print(count +"\n");
}
 
// Driver Code
public static void main(String[] args)
{
    String arr[] = { "0101", "0011", "0110" };
    int N = arr.length;
 
    // Function Call
    countString(arr, N);
}
}
 
// This code is contributed by 29AjayKumar


Python3




# Python3 program for the above approach
 
# Function to check if there is arc
# intersection or not
def arcIntersection(S, lenn):
     
    stk = []
     
    # Traverse the string S
    for i in range(lenn):
         
        # Insert all the elements in
        # the stack one by one
        stk.append(S[i])
 
        if (len(stk) >= 2):
             
            # Extract the top element
            temp = stk[-1]
 
            # Pop out the top element
            del stk[-1]
 
            # Check if the top element
            # is same as the popped element
            if (stk[-1] == temp):
                del stk[-1]
                 
            # Otherwise
            else:
                stk.append(temp)
                 
    # If the stack is empty
    if (len(stk) == 0):
        return 1
         
    return 0
     
# Function to check if there is arc
# intersection or not for the given
# array of strings
def countString(arr, N):
     
    # Stores count of string not
    # having arc intersection
    count = 0
 
    # Iterate through array
    for i in range(N):
 
        # Length of every string
        lenn = len(arr[i])
 
        # Function Call
        count += arcIntersection(arr[i], lenn)
 
    # Print the desired count
    print(count)
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ "0101", "0011", "0110" ]
    N = len(arr)
     
    # Function Call
    countString(arr, N)
     
# This code is contributed by mohit kumar 29


C#




// C# program for
// the above approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
// Function to check if there is arc
// intersection or not
static int arcIntersection(String S, int len)
{
    Stack<char> stk  = new Stack<char>();
 
    // Traverse the String S
    for (int i = 0; i < len; i++)
    {
 
        // Insert all the elements in
        // the stack one by one
        stk.Push(S[i]);
 
        if (stk.Count >= 2)
        {
 
            // Extract the top element
            char temp = stk.Peek();
 
            // Pop out the top element
            stk.Pop();
 
            // Check if the top element
            // is same as the popped element
            if (stk.Peek() == temp)
            {
                stk.Pop();
            }
 
            // Otherwise
            else
            {
                stk.Push(temp);
            }
        }
    }
 
    // If the stack is empty
    if (stk.Count == 0)
        return 1;
    return 0;
}
 
// Function to check if there is arc
// intersection or not for the given
// array of Strings
static void countString(String []arr, int N)
{
   
    // Stores count of String not
    // having arc intersection
    int count = 0;
 
    // Iterate through array
    for (int i = 0; i < N; i++)
    {
 
        // Length of every String
        int len = arr[i].Length;
 
        // Function Call
        count += arcIntersection(
            arr[i], len);
    }
 
    // Print the desired count
    Console.Write(count +"\n");
}
 
// Driver Code
public static void Main(String[] args)
{
    String [] arr = { "0101", "0011", "0110" };
    int N = arr.Length;
 
    // Function Call
    countString(arr, N);
}
}
 
// This code is contributed by jana_sayantan.


Javascript




<script>
 
// JavaScript program for the above approach
 
// Function to check if there is arc
// intersection or not
function arcIntersection(S, len)
{
    var stk = [];
 
    // Traverse the string S
    for (var i = 0; i < len; i++) {
 
        // Insert all the elements in
        // the stack one by one
        stk.push(S[i]);
 
        if (stk.length >= 2) {
 
            // Extract the top element
            var temp = stk[stk.length-1];
 
            // Pop out the top element
            stk.pop();
 
            // Check if the top element
            // is same as the popped element
            if (stk[stk.length-1] == temp) {
                stk.pop();
            }
 
            // Otherwise
            else {
                stk.push(temp);
            }
        }
    }
 
    // If the stack is empty
    if (stk.length==0)
        return 1;
    return 0;
}
 
// Function to check if there is arc
// intersection or not for the given
// array of strings
function countString(arr, N)
{
    // Stores count of string not
    // having arc intersection
    var count = 0;
 
    // Iterate through array
    for (var i = 0; i < N; i++) {
 
        // Length of every string
        var len = arr[i].length;
 
        // Function Call
        count += arcIntersection(
            arr[i], len);
    }
 
    // Print the desired count
    document.write( count + "<br>");
}
 
// Driver Code
 
var arr = ["0101", "0011", "0110" ];
var N = arr.length;
 
// Function Call
countString(arr, N);
 
 
</script>


Output: 

2

 

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
Previous
Next
Share your thoughts in the comments
Similar Reads