可以替换的最大字符数?最多 A 0 和 B 1,没有相邻的重复
给定一个字符串S只包含两个特殊字符' * ' 和 ' ? ',以及两个整数A和B ,表示可用的 0和1 的计数。任务是计算可以放置在 ' 的位置的最大字符数? ' 这样没有两个相邻的字符是相同的。
例子:
Input: s = “*???*???*”, A = 4, B = 3
Output: 6
Explanation: The string can be modified to “*010*010*”. Therefore, maximum number of characters that can be placed in the place of ‘?’ are (4 + 2 = 6)
Input: s = “???*??*”, A = 0, B = 5
Output: 3
Explanation: The string can be modified to “*1?1*?1*”. Therefore, maximum number of characters that can be placed in the place of ‘?’ are (0 + 3 = 3)
方法:可以通过跟踪“?”的连续段并放置 0 和 1 来解决该任务,这样在替换“?”之后,结果字符串中没有两个相邻的元素是相同的。
请按照以下步骤解决问题:
- 初始化一个向量“v”,它将存储?s的连续段的长度
- 变量 'cur' 存储?s 的当前计数,一旦遇到 '*',就将cur的值存储在v中
- 现在,开始迭代存储的?s 段长度,并贪婪地分配0和1代替?s
- 跟踪可以放置在?s位置的最大字符数
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the maximum number
// of characters 'A' and 'B' that can be
// placed in position of '?'
int maximumChar(string s, int A, int B)
{
// Length of the string
int len = s.size();
// Store the current count of '?'s
int curr = 0;
// Store the lengths of contiguous
// segments of '?'s
vector v;
// Traversing the string
for (int i = 0; i < len; i++) {
// If character is '?'
// increment curr by 1
if (s[i] == '?') {
curr++;
}
// If character is '*'
else {
// If curr is not equal to 0
if (curr != 0) {
v.push_back(curr);
// Re-initialise curr to 0
curr = 0;
}
}
}
// After traversing the string
// if curr is not equal to 0
if (curr != 0) {
v.push_back(curr);
}
// Variable for maximum count
int count = 0;
// Traversing the vector
for (int i = 0; i < v.size(); i++) {
// Variable to store half of
// each elements in vector
int x = v[i] / 2;
// Variable to store half of
// each elements with its remainder
int y = v[i] / 2 + v[i] % 2;
// If A is greater than B
if (A > B) {
// Swap both
swap(A, B);
}
// Increment count by
// minimum of A and x
count += min(A, x);
// Update A
A -= min(A, x);
// Increment count by
// minimum of B and y
count += min(B, y);
// Update B
B -= min(B, y);
}
// Return count
return count;
}
// Driver Code
int main()
{
string s = "*???*???*";
int A = 4, B = 3;
cout << maximumChar(s, A, B);
return 0;
}
Java
// Java program for the above approach
import java.util.ArrayList;
class GFG {
// Function to count the maximum number
// of characters 'A' and 'B' that can be
// placed in position of '?'
public static int maximumChar(String s, int A, int B)
{
// Length of the string
int len = s.length();
// Store the current count of '?'s
int curr = 0;
// Store the lengths of contiguous
// segments of '?'s
ArrayList v = new ArrayList();
// Traversing the string
for (int i = 0; i < len; i++) {
// If character is '?'
// increment curr by 1
if (s.charAt(i) == '?') {
curr++;
}
// If character is '*'
else {
// If curr is not equal to 0
if (curr != 0) {
v.add(curr);
// Re-initialise curr to 0
curr = 0;
}
}
}
// After traversing the string
// if curr is not equal to 0
if (curr != 0) {
v.add(curr);
}
// Variable for maximum count
int count = 0;
// Traversing the vector
for (int i = 0; i < v.size(); i++)
{
// Variable to store half of
// each elements in vector
int x = v.get(i) / 2;
// Variable to store half of
// each elements with its remainder
int y = v.get(i) / 2 + v.get(i) % 2;
// If A is greater than B
if (A > B)
{
// Swap both
int temp = A;
A = B;
B = temp;
}
// Increment count by
// minimum of A and x
count += Math.min(A, x);
// Update A
A -= Math.min(A, x);
// Increment count by
// minimum of B and y
count += Math.min(B, y);
// Update B
B -= Math.min(B, y);
}
// Return count
return count;
}
// Driver Code
public static void main(String args[]) {
String s = "*???*???*";
int A = 4, B = 3;
System.out.println(maximumChar(s, A, B));
}
}
// This code is contributed by saurabh_jaiswal.
Python3
# python program for the above approach
import math
# Function to count the maximum number
# of characters 'A' and 'B' that can be
# placed in position of '?'
def maximumChar(s, A, B):
# Length of the string
le = len(s)
# Store the current count of '?'s
curr = 0
# Store the lengths of contiguous
# segments of '?'s
v = []
# Traversing the string
for i in range(0, le):
# If character is '?'
# increment curr by 1
if (s[i] == '?'):
curr += 1
# If character is '*'
else:
# If curr is not equal to 0
if (curr != 0):
v.append(curr)
# Re-initialise curr to 0
curr = 0
# After traversing the string
# if curr is not equal to 0
if (curr != 0):
v.append(curr)
# Variable for maximum count
count = 0
# Traversing the vector
for i in range(0, len(v)):
# Variable to store half of
# each elements in vector
x = v[i] // 2
# Variable to store half of
# each elements with its remainder
y = v[i] // 2 + v[i] % 2
# If A is greater than B
if (A > B):
# Swap both
temp = A
A = B
B = temp
# Increment count by
# minimum of A and x
count += min(A, x)
# Update A
A -= min(A, x)
# Increment count by
# minimum of B and y
count += min(B, y)
# Update B
B -= min(B, y)
# Return count
return count
# Driver Code
if __name__ == "__main__":
s = "*???*???*"
A = 4
B = 3
print(maximumChar(s, A, B))
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG {
// Function to count the maximum number
// of characters 'A' and 'B' that can be
// placed in position of '?'
public static int maximumChar(String s, int A, int B)
{
// Length of the string
int len = s.Length;
// Store the current count of '?'s
int curr = 0;
// Store the lengths of contiguous
// segments of '?'s
List v = new List();
// Traversing the string
for (int i = 0; i < len; i++)
{
// If character is '?'
// increment curr by 1
if (s[i] == '?') {
curr++;
}
// If character is '*'
else {
// If curr is not equal to 0
if (curr != 0) {
v.Add(curr);
// Re-initialise curr to 0
curr = 0;
}
}
}
// After traversing the string
// if curr is not equal to 0
if (curr != 0) {
v.Add(curr);
}
// Variable for maximum count
int count = 0;
// Traversing the vector
for (int i = 0; i < v.Count; i++)
{
// Variable to store half of
// each elements in vector
int x = v[i] / 2;
// Variable to store half of
// each elements with its remainder
int y = v[i] / 2 + v[i] % 2;
// If A is greater than B
if (A > B)
{
// Swap both
int temp = A;
A = B;
B = temp;
}
// Increment count by
// minimum of A and x
count += Math.Min(A, x);
// Update A
A -= Math.Min(A, x);
// Increment count by
// minimum of B and y
count += Math.Min(B, y);
// Update B
B -= Math.Min(B, y);
}
// Return count
return count;
}
// Driver Code
public static void Main(String []args) {
String s = "*???*???*";
int A = 4, B = 3;
Console.WriteLine(maximumChar(s, A, B));
}
}
// This code is contributed by shikhasingrajput
Javascript
输出
6
时间复杂度: O(N)
辅助空间: O(N)