给定由0、1和*组成的字符串str ,任务是在执行给定的操作后从0和1中找出出现的最大字符:
- Replace * with 0 where * appears on the left side of the existing 0s in the string.
- Replace * with 1 where * appears on the right side of the existing 1s in the string.
- If any * can be replaced by both 0 and 1, then it remains unchanged.
注意:如果执行给定的操作后0和1的频率相同,则打印-1 。
例子:
Input: str = “**0**1***0”
Output: 0
Explanation:
Since 0 can replace the * to its left and 1 can replace the * to its right thus string becomes 000**1***0
Input: str = “0*1”
Output: -1
Explanation:
Both 0 and 1 have the same frequency hence the output is -1.
方法:生成最终结果字符串,然后比较0和1的频率的想法。以下是步骤:
- 计算字符串中0和1的初始频率,并将它们存储在变量count_0和count_1中。
- 将变量prev初始化为-1。遍历字符串并检查当前字符是否为* 。如果是这样,则继续。
- 如果它是遇到的第一个字符且为0,则将所有*添加到count_0并将prev更改为当前索引。
- 否则,如果第一个字符为1,则将prev更改为当前索引。
- 如果前一个字符为1 ,当前字符为0,则在字符之间将*的一半加到0 ,将一半加到1 。
- 如果前一个字符为0,当前字符为1,则它们之间的*字符都不能替换。
- 如果前两个字符与当前两个字符的类型相同,则将*的计数添加到频率中。
- 比较0和1的频率并打印最大出现的字符。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the
// maximum occurring character
void solve(string S)
{
// Initialize count of
// zero and one
int count_0 = 0, count_1 = 0;
int prev = -1;
// Iterate over the given string
for (int i = 0; i < S.length(); i++) {
// Count the zeros
if (S[i] == '0')
count_0++;
// Count the ones
else if (S[i] == '1')
count_1++;
}
// Iterate over the given string
for (int i = 0; i < S.length(); i++) {
// Check if character
// is * then continue
if (S[i] == '*')
continue;
// Check if first character
// after * is X
else if (S[i] == '0' && prev == -1) {
// Add all * to
// the frequency of X
count_0 = count_0 + i;
// Set prev to the
// i-th character
prev = i;
}
// Check if first character
// after * is Y
else if (S[i] == '1' && prev == -1) {
// Set prev to the
// i-th character
prev = i;
}
// Check if prev character is 1
// and current character is 0
else if (S[prev] == '1' && S[i] == '0') {
// Half of the * will be
// converted to 0
count_0 = count_0 + (i - prev - 1) / 2;
// Half of the * will be
// converted to 1
count_1 = count_1 + (i - prev - 1) / 2;
prev = i;
}
// Check if prev and current are 1
else if (S[prev] == '1' && S[i] == '1') {
// All * will get converted to 1
count_1 = count_1 + (i - prev - 1);
prev = i;
}
// No * can be replaced
// by either 0 or 1
else if (S[prev] == '0' && S[i] == '1')
// Prev becomes the ith character
prev = i;
// Check if prev and current are 0
else if (S[prev] == '0' && S[i] == '0') {
// All * will get converted to 0
count_0 = count_0 + (i - prev - 1);
prev = i;
}
}
// If frequency of 0
// is more
if (count_0 > count_1)
cout << "0";
// If frequency of 1
// is more
else if (count_1 > count_0)
cout << "1";
else {
cout << -1;
}
}
// Driver code
int main()
{
// Given string
string str = "**0**1***0";
// Function Call
solve(str);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to find the
// maximum occurring character
static void solve(String S)
{
// Initialize count of
// zero and one
int count_0 = 0, count_1 = 0;
int prev = -1;
// Iterate over the given string
for(int i = 0; i < S.length(); i++)
{
// Count the zeros
if (S.charAt(i) == '0')
count_0++;
// Count the ones
else if (S.charAt(i) == '1')
count_1++;
}
// Iterate over the given string
for(int i = 0; i < S.length(); i++)
{
// Check if character
// is * then continue
if (S.charAt(i) == '*')
continue;
// Check if first character
// after * is X
else if (S.charAt(i) == '0' && prev == -1)
{
// Add all * to
// the frequency of X
count_0 = count_0 + i;
// Set prev to the
// i-th character
prev = i;
}
// Check if first character
// after * is Y
else if (S.charAt(i) == '1' && prev == -1)
{
// Set prev to the
// i-th character
prev = i;
}
// Check if prev character is 1
// and current character is 0
else if (S.charAt(prev) == '1' &&
S.charAt(i) == '0')
{
// Half of the * will be
// converted to 0
count_0 = count_0 + (i - prev - 1) / 2;
// Half of the * will be
// converted to 1
count_1 = count_1 + (i - prev - 1) / 2;
prev = i;
}
// Check if prev and current are 1
else if (S.charAt(prev) == '1' &&
S.charAt(i) == '1')
{
// All * will get converted to 1
count_1 = count_1 + (i - prev - 1);
prev = i;
}
// No * can be replaced
// by either 0 or 1
else if (S.charAt(prev) == '0' &&
S.charAt(i) == '1')
// Prev becomes the ith character
prev = i;
// Check if prev and current are 0
else if (S.charAt(prev) == '0' &&
S.charAt(i) == '0')
{
// All * will get converted to 0
count_0 = count_0 + (i - prev - 1);
prev = i;
}
}
// If frequency of 0
// is more
if (count_0 > count_1)
System.out.print("0");
// If frequency of 1
// is more
else if (count_1 > count_0)
System.out.print("1");
else
{
System.out.print("-1");
}
}
// Driver code
public static void main (String[] args)
{
// Given string
String str = "**0**1***0";
// Function call
solve(str);
}
}
// This code is contributed by code_hunt
Python3
# Python3 program for the above approach
# Function to find the
# maximum occurring character
def solve(S):
# Initialize count of
# zero and one
count_0 = 0
count_1 = 0
prev = -1
# Iterate over the given string
for i in range(len(S)) :
# Count the zeros
if (S[i] == '0'):
count_0 += 1
# Count the ones
elif (S[i] == '1'):
count_1 += 1
# Iterate over the given string
for i in range(len(S)):
# Check if character
# is * then continue
if (S[i] == '*'):
continue
# Check if first character
# after * is X
elif (S[i] == '0' and prev == -1):
# Add all * to
# the frequency of X
count_0 = count_0 + i
# Set prev to the
# i-th character
prev = i
# Check if first character
# after * is Y
elif (S[i] == '1' and prev == -1):
# Set prev to the
# i-th character
prev = i
# Check if prev character is 1
# and current character is 0
elif (S[prev] == '1' and S[i] == '0'):
# Half of the * will be
# converted to 0
count_0 = count_0 + (i - prev - 1) / 2
# Half of the * will be
# converted to 1
count_1 = count_1 + (i - prev - 1) // 2
prev = i
# Check if prev and current are 1
elif (S[prev] == '1' and S[i] == '1'):
# All * will get converted to 1
count_1 = count_1 + (i - prev - 1)
prev = i
# No * can be replaced
# by either 0 or 1
elif (S[prev] == '0' and S[i] == '1'):
# Prev becomes the ith character
prev = i
# Check if prev and current are 0
elif (S[prev] == '0' and S[i] == '0'):
# All * will get converted to 0
count_0 = count_0 + (i - prev - 1)
prev = i
# If frequency of 0
# is more
if (count_0 > count_1):
print("0")
# If frequency of 1
# is more
elif (count_1 > count_0):
print("1")
else:
print("-1")
# Driver code
# Given string
str = "**0**1***0"
# Function call
solve(str)
# This code is contributed by code_hunt
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the
// maximum occurring character
static void solve(string S)
{
// Initialize count of
// zero and one
int count_0 = 0, count_1 = 0;
int prev = -1;
// Iterate over the given string
for(int i = 0; i < S.Length; i++)
{
// Count the zeros
if (S[i] == '0')
count_0++;
// Count the ones
else if (S[i] == '1')
count_1++;
}
// Iterate over the given string
for(int i = 0; i < S.Length; i++)
{
// Check if character
// is * then continue
if (S[i] == '*')
continue;
// Check if first character
// after * is X
else if (S[i] == '0' && prev == -1)
{
// Add all * to
// the frequency of X
count_0 = count_0 + i;
// Set prev to the
// i-th character
prev = i;
}
// Check if first character
// after * is Y
else if (S[i] == '1' && prev == -1)
{
// Set prev to the
// i-th character
prev = i;
}
// Check if prev character is 1
// and current character is 0
else if (S[prev] == '1' && S[i] == '0')
{
// Half of the * will be
// converted to 0
count_0 = count_0 + (i - prev - 1) / 2;
// Half of the * will be
// converted to 1
count_1 = count_1 + (i - prev - 1) / 2;
prev = i;
}
// Check if prev and current are 1
else if (S[prev] == '1' && S[i] == '1')
{
// All * will get converted to 1
count_1 = count_1 + (i - prev - 1);
prev = i;
}
// No * can be replaced
// by either 0 or 1
else if (S[prev] == '0' && S[i] == '1')
// Prev becomes the ith character
prev = i;
// Check if prev and current are 0
else if (S[prev] == '0' && S[i] == '0')
{
// All * will get converted to 0
count_0 = count_0 + (i - prev - 1);
prev = i;
}
}
// If frequency of 0
// is more
if (count_0 > count_1)
Console.Write("0");
// If frequency of 1
// is more
else if (count_1 > count_0)
Console.Write("1");
else
{
Console.Write("-1");
}
}
// Driver code
public static void Main ()
{
// Given string
string str = "**0**1***0";
// Function call
solve(str);
}
}
// This code is contributed by code_hunt
输出:
0
时间复杂度: O(N)
辅助空间: O(1)