📜  不包含弧形交点的字符串计数

📅  最后修改于: 2021-05-17 20:12:40             🧑  作者: Mango

给定一个由N个二进制字符串组成的数组arr [] ,任务是计算不包含任何Arc Intersection的字符串的数量。

例子:

天真的方法:最简单的方法是遍历数组并检查每个字符串,无论相似字符是否在连续索引处组合在一起。如果发现是真实的,不断递增,这样的字符串的计数。最后,打印获得的计数值

时间复杂度: O(N * M 2 ),其中M是给定数组中字符串的最大长度。
辅助空间: O(1)

高效的方法:为了优化上述方法,我们的想法是使用Stack。请按照以下步骤解决问题:

  1. 初始化count ,以存储不包含任何弧形交点的字符串的计数。
  2. 初始化堆栈以将字符串的每个字符存储到其中。
  3. 迭代给定的字符串并执行以下操作:
    • 将当前字符推入堆栈。
    • 如果堆栈大小大于2 ,则检查堆栈顶部的两个元素是否相同。如果发现为真,则同时弹出两个字符以删除最近的弧形交点。
  4. 完成上述步骤后,如果堆栈为空,则不包含任何弧形交点。
  5. 对数组中的每个字符串执行步骤2步骤4 ,以检查字符串是否包含弧形交点。如果不包含则计算这个字符串。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if there is arc
// intersection or not
int arcIntersection(string S, int len)
{
    stack 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 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 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[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.


输出:
2

时间复杂度: O(N * M),其中M是给定数组中字符串的最大长度。
辅助空间: O(M),其中M是给定数组中字符串的最大长度。