原文:
给定一个仅由 0 和 1 组成的二进制字符串 str ,任务是在从字符串中逐个删除“10”和“01”后打印该字符串。如果字符串为空,则打印-1。
示例:
输入: str = "101100" 输出: -1 解释: 第一步,从字符串中移除索引 0 和 1 处的“10”。 101100 - > 1100 在第二步中,从字符串中删除索引 1 和 2 处的“10”。 1100 - > 10 最后去掉“10”,串变成空的。 10 - >空
输入: str = "010110100" 输出: 0 解释: 在第一步中,从字符串中删除索引 0 和 1 处的“01”。 010110100 - > 0110100 在第二步中,从字符串中删除索引 0 和 1 处的“01”。 0110100 - > 10100 在第三步中,从字符串中删除索引 0 和 1 处的“10”。 10100 - > 100 最后去掉“10”,字符串变为“0”。 100 - > 0
观察:仔细观察,由于给定的字符串是二进制字符串,除了字符串中存在的不能与其补语配对的额外 0 和 1 之外,所有字符串都可以被清除。例如:
let str = "010110100 "。 对于这个字符串,0 的个数是 5,1 的个数是 4。 现在,让我们开始一个接一个地移除替换子字符串:
- 01 0110100 - > 0110100
- 01 10100 - > 10100
- 10 100 - > 100
- 10 0 - > 0
此时,字符串不能进一步减少。
因此,从上面的例子可以看出,只要字符串中有 1 和 0,字符串就可以减少。 我们已经在上一篇文章中讨论了查找 0 和 1 的
方法:从上面的观察可以得出结论,最终的字符串只包含额外的 1 或 0,它们不能与字符串中的任何数字配对。因此,解决这个问题的思路是对字符串中 0 和 1 的数进行计数,找出两个计数之间的差。该计数表示基于较高值的剩余 1 或 0的数量。
可以按照以下步骤计算答案:
- 获取字符串中存在的 0 的计数,并将其存储在变量中。
- 获取字符串中存在的 1 的计数,并将其存储在另一个变量中。****
- 如果 1 的计数等于 0 的,则可以减少整个字符串。因此,返回 -1 。
- 如果 1 的计数大于 0的计数,那么最后剩下许多 1,进一步减少是不可能的。因此,将许多 1 的追加到一个空字符串中并返回该字符串。
- 类似地,如果 0 的计数大于 1 的计数,则找出差异,并将那些许多 0 的附加到空字符串中并返回。
下面是上述方法的实现:
c
// c program to print the final string
// after removing all the occurrences of
// "10" and "01" from the given binary string
#include
using namespace std;
// function to print the final string
// after removing all the occurrences of
// "10" and "01" from the given binary string
void finalstring(string str)
{
// variables to store the
// count of 1's and 0's
int x = 0, y = 0;
// variable left will store
// whether 0's or 1's is left
// in the final string
int left;
// length of the string
int n = str.length();
// for loop to count the occurrences
// of 1's and 0's in the string
for (int i = 0; i < n; i ) {
if (str[i] == '1')
x ;
else
y ;
}
// to check if the count of 1's is
// greater than the count of 0's or not.
// if x is greater, then those many 1's
// are printed.
if (x > y)
left = 1;
else
left = 0;
// length of the final remaining string
// after removing all the occurrences
int length = n - 2 * min(x, y);
// printing the final string
for (int i = 0; i < length; i ) {
cout << left;
}
}
// driver code
int main()
{
string str = "010110100100000";
finalstring(str);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java program to print the final string
// after removing all the occurrences of
// "10" and "01" from the given binary string
import java.util.*;
class gfg{
// function to print the final string
// after removing all the occurrences of
// "10" and "01" from the given binary string
static void finalstring(string str)
{
// variables to store the
// count of 1's and 0's
int x = 0, y = 0;
// variable left will store
// whether 0's or 1's is left
// in the final string
int left;
// length of the string
int n = str.length();
// for loop to count the occurrences
// of 1's and 0's in the string
for (int i = 0; i < n; i ) {
if (str.charat(i) == '1')
x ;
else
y ;
}
// to check if the count of 1's is
// greater than the count of 0's or not.
// if x is greater, then those many 1's
// are printed.
if (x > y)
left = 1;
else
left = 0;
// length of the final remaining string
// after removing all the occurrences
int length = n - 2 * math.min(x, y);
// printing the final string
for (int i = 0; i < length; i ) {
system.out.print(left);
}
}
// driver code
public static void main(string[] args)
{
string str = "010110100100000";
finalstring(str);
}
}
// this code is contributed by sapnasingh4991
python 3
# python 3 program to print the final string
# after removing all the occurrences of
# "10" and "01" from the given binary string
# function to print the final string
# after removing all the occurrences of
# "10" and "01" from the given binary string
def finalstring(st):
# variables to store the
# count of 1's and 0's
x , y = 0 , 0
# length of the string
n = len(st)
# for loop to count the occurrences
# of 1's and 0's in the string
for i in range( n):
if (st[i] == '1'):
x = 1
else:
y = 1
# to check if the count of 1's is
# greater than the count of 0's or not.
# if x is greater, then those many 1's
# are printed.
if (x > y):
left = 1
else:
left = 0
# length of the final remaining string
# after removing all the occurrences
length = n - 2 * min(x, y);
# printing the final string
for i in range(length):
print(left, end="")
# driver code
if __name__ == "__main__":
st = "010110100100000"
finalstring(st)
# this code is contributed by chitranayal
c#
// c# program to print the readonly string
// after removing all the occurrences of
// "10" and "01" from the given binary string
using system;
class gfg{
// function to print the readonly string
// after removing all the occurrences of
// "10" and "01" from the given binary string
static void finalstring(string str)
{
// variables to store the
// count of 1's and 0's
int x = 0, y = 0;
// variable left will store
// whether 0's or 1's is left
// in the readonly string
int left;
// length of the string
int n = str.length;
// for loop to count the occurrences
// of 1's and 0's in the string
for (int i = 0; i < n; i ) {
if (str[i] == '1')
x ;
else
y ;
}
// to check if the count of 1's is
// greater than the count of 0's or not.
// if x is greater, then those many 1's
// are printed.
if (x > y)
left = 1;
else
left = 0;
// length of the readonly remaining string
// after removing all the occurrences
int length = n - 2 * math.min(x, y);
// printing the readonly string
for (int i = 0; i < length; i ) {
console.write(left);
}
}
// driver code
public static void main(string[] args)
{
string str = "010110100100000";
finalstring(str);
}
}
// this code is contributed by 29ajaykumar
java 描述语言
**output:
00000
**
*时间复杂度分析:*
- 计算 1 和 0 出现次数的 for 循环需要 o(n) 时间,其中 n 是字符串的长度。
- if 语句需要恒定的时间。所以 if 语句的时间复杂度是 o(1) 。
- 当整个字符串只有 0 或 1 时,打印最终字符串的循环在最坏的情况下需要 o(n)****
- 因此,整体时间复杂度为 o(n) 。
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处