📜  查找所有偶数为 1 的子字符串,其反向也存在于给定的字符串中

📅  最后修改于: 2022-05-13 01:56:04.809000             🧑  作者: Mango

查找所有偶数为 1 的子字符串,其反向也存在于给定的字符串中

给定一个二进制字符串str 。任务是找到子串的集合(包含唯一的子串)的大小,使得如果有一个长度为n的子串(假设A )具有偶数个1并且还有另一个相同的子串(假设B )长度n和偶数个1并且它是A的倒数,那么在字符串集合中只考虑A

例子:

方法:遵循以下两个特定规则来解决问题:

因此,要解决这个问题,请使用一组整数列表,其中每个列表将包含三个变量: {length, even, odd} 。此外,使用计数变量来描述子字符串中1 的计数。这里的“长度”将描述子字符串的长度。偶数奇数变量用于在子字符串中遇到“0”时将1 的计数描述为偶数或奇数。之后,将其添加到子字符串集合中,然后打印集合的大小。

请按照以下步骤解决问题:

  • 初始化 HashSet s[]。
  • 使用变量i遍历范围[0, str.length())并执行以下任务:
    • 将变量计数、偶数奇数初始化为0
    • 使用变量j遍历范围[0, a.length())并执行以下任务:
      • 如果str[j]等于1 ,则将count的值增加1。
      • 否则,如果count%2等于0 ,则将even的值增加1 ,否则将odd的值增加1。
      • 将变量len初始化为j-i+1。
      • 初始化一个新的 ArrayList x[]。
      • {len, even, odd}的值添加到 ArrayList x[]。
      • x[]添加到 HashSet s[]。
  • 执行上述步骤后,打印集合大小的值作为答案。

下面是上述方法的实现。

C++
// C++ code for the above approach
#include 
using namespace std;
 
// Function to find number of substrings
// fulfilling the condition
void find(string a)
{
 
  // Initialize the hash-set
  set> s;
 
  // Traverse the string
  for (int i = 0; i < a.length(); i++)
  {
 
    // Initialize the variables
    int count = 0, even = 0, odd = 0;
    for (int j = i; j < a.length();
         j++)
    {
 
      // Check the condition
      if (a[j] == '1')
      {
        count++;
      }
      else
      {
        if (count % 2 == 0)
        {
          even++;
        }
        else
        {
          odd++;
        }
      }
      int len = j - i + 1;
      vector x;
      x.push_back(len);
      x.push_back(even);
      x.push_back(odd);
      s.insert(x);
    }
  }
 
  // Print the result
  cout << (s.size());
}
 
// Driver Code
int main()
{
  string str = "10101";
  find(str);
  return 0;
}
 
// This code is contributed by Potta Lokesh


Java
// Java code to implement above approach
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to find number of substrings
    // fulfilling the condition
    public static void find(String a)
    {
        // Initialize the hash-set
        HashSet > s
            = new HashSet<>();
 
        // Traverse the string
        for (int i = 0; i < a.length(); i++) {
 
            // Initialize the variables
            int count = 0, even = 0, odd = 0;
            for (int j = i; j < a.length();
                 j++) {
 
                // Check the condition
                if (a.charAt(j) == '1') {
                    count++;
                }
                else {
                    if (count % 2 == 0) {
                        even++;
                    }
                    else {
                        odd++;
                    }
                }
                int len = j - i + 1;
                ArrayList x
                    = new ArrayList<>();
                x.add(len);
                x.add(even);
                x.add(odd);
                s.add(x);
            }
        }
 
        // Print the result
        System.out.println(s.size());
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String str = "10101";
        find(str);
    }
}


Python3
# Python code to implement above approach
 
# Function to find number of substrings
# fulfilling the condition
def find(a):
   
    # Initialize the hash-set
    s = set();
 
    # Traverse the string
    for i in range(len(a)):
 
        # Initialize the variables
        count = 0; even = 0; odd = 0;
        for j in range(i,len(a)):
 
            # Check the condition
            if (a[j] == '1'):
                count += 1;
            else:
                if (count % 2 == 0):
                    even += 1;
                else:
                    odd += 1;
 
            length = j - i + 1;
            x = str(length)+str(even)+str(odd);
            s.add(x)
 
    # Print the result
    print(len(s));
 
# Driver Code
if __name__ == '__main__':
    string = "10101";
    find(string);
 
# This code is contributed by 29AjayKumar


C#
// C# code to implement above approach
using System;
using System.Collections;
using System.Collections.Generic;
 
public class GFG{
 
    // Function to find number of substrings
    // fulfilling the condition
    public static void find(String a)
    {
        // Initialize the hash-set
        HashSet s = new HashSet();
        string x;
        // Traverse the string
        for (int i = 0; i < a.Length; i++) {
 
            // Initialize the variables
            int count = 0, even = 0, odd = 0;
            for (int j = i; j < a.Length;
                 j++) {
 
                // Check the condition
                if (a[j] == '1') {
                    count++;
                }
                else {
                    if (count % 2 == 0) {
                        even++;
                    }
                    else {
                        odd++;
                    }
                }
                int len = j - i + 1;
                x = len.ToString() + even.ToString() +odd.ToString();
                s.Add(x);
            }
        }
 
        // Print the result
        Console.Write(s.Count);
    }
 
    // Driver Code
    public static void Main()
    {
        string str = "10101";
        find(str);
    }
}
 
// This code is contributed by Shubham Singh


Javascript


输出:
8

时间复杂度: O(N*N) 其中 N 是字符串的长度
辅助空间: O(N)