查找所有偶数为 1 的子字符串,其反向也存在于给定的字符串中
给定一个二进制字符串str 。任务是找到子串的集合(包含唯一的子串)的大小,使得如果有一个长度为n的子串(假设A )具有偶数个1并且还有另一个相同的子串(假设B )长度n和偶数个1并且它是A的倒数,那么在字符串集合中只考虑A 。
例子:
Input: str = “10101”
Output: 8
Explanation: All unique substrings of given string={1, 0, 10, 01, 101, 010, 1010, 0101, 10101}.
In the above set (1010, 0101) is following the special property.
Therefore, only 1010 will be considered in the given set.
Updated Set = A = {1, 0, 10, 01, 101, 010, 1010, 10101}.
Length of A=8
Input: str = “10001”
Output: 11
Explanation: All unique substrings of given string={1, 0, 10, 01, 00, 100, 000, 001, 1000, 0001, 10001}.
In the above set no string is following the special property. Length of Set=11
方法:遵循以下两个特定规则来解决问题:
- All pairwise strings not following special property should be identified uniquely.
- All pairwise strings following special property should be identified as one.
因此,要解决这个问题,请使用一组整数列表,其中每个列表将包含三个变量: {length, even, odd} 。此外,使用计数变量来描述子字符串中1 的计数。这里的“长度”将描述子字符串的长度。偶数和奇数变量用于在子字符串中遇到“0”时将1 的计数描述为偶数或奇数。之后,将其添加到子字符串集合中,然后打印集合的大小。
WHY DOES THIS WORK?
Consider a binary string with an even number of 1’s and whenever 0 is encountered in it, see how many 1’s occur in it before that 0 and if it is even increase the even variable count otherwise increase the odd variable count.
Suppose, when 0 is encountered in a substring and till that it has even 1’s then remaining would also be 1’s(because we have considered a string with even 1’s), similarly if we have odd 1’s till whenever we encounter 0 therefore remaining would also be odd 1’s. Therefore the reverse string would have the same parity(i.e count of 1’s as even or odd). That’s why it would not take the reverse string into consideration.
请按照以下步骤解决问题:
- 初始化 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)