可以插入没有 3 个相邻字符的 X 的最大计数是 X
给定一个字符串,长度为N的str和一个字符X ,任务是找到要插入到字符串中的字符X的最大计数,使得没有三个连续字符等于X。如果找不到这样的字符串,则打印-1 。
例子:
Input: str = “xxyxy”, X = X
Output: 3
Explanation:
Insert an ‘x’ at position 4: “xxyxxy”.
Insert two ‘x’ at position 7: “xxyxxyxx”
Now no more ‘x’ can be inserted, as it will lead to a size 3 substring with all x in it.
Hence the required count is 3.
Input:str = “gfg”, X = ‘X’
Output: 8
方法:这个想法是计算可以插入X的所有位置,然后减去字符串中已经存在的X的计数。
以下是步骤:
- 可以在字符串中插入的最大X数是2 * (N + 1) 个字符,因为可以在字符串的开头和结尾以及每个连续字符之间插入两个X。
- 现在,找到大小为1或2的连续X组的数量,并将其存储在变量countX中。
- 可以在给定字符串中插入的X数是2 *(要插入的位置数 + 1)- 找到的 X 数。
- 总之,可以使用一个简单的数学公式2 * (N + 1) – (N – Xs)来找到最终答案。
下面是上述方法的实现:
C++14
// C++ program for the above approach
#include
using namespace std;
// Utility function to find maximum count of
// X that are to be inserted into str such that
// no three consecutive characters are equal to X
int maxNumberOfXAddedUtil(string str, char X)
{
// Stores count of consecutive X
int countX = 0;
// Stores count of characters which
// is not equal to X
int countNotX = 0;
// Iterate over characters of string, str
for (int i = 0; i < str.size(); i++)
{
// If current character is X
if (str[i] == X)
{
// Update countX
countX++;
}
// If countX is less
// than 3
else if (countX < 3)
{
// Update countNotX
countNotX++;
// Update countX
countX = 0;
}
}
// If countX is greater than
// or equal to 3
if (countX >= 3)
return -1;
else
return 2 * (countNotX + 1) - (str.size() - countNotX);
}
// Function to find maximum count of X that
// are to be inserted into str such that no
// three consecutive characters are equal to X
static void maxNumberOfXAdded(string str, char X)
{
int ans = maxNumberOfXAddedUtil(str, X);
// Print the answer
cout << (ans);
}
// Driver code
int main()
{
// Given string
string str = "xxyxy";
char X = 'x';
// Function Call
maxNumberOfXAdded(str, X);
}
// This code is contributed by amreshkumar3.
Java
// Java program for the above approach
import java.util.*;
public class Main {
// Utility function to find maximum count of
// X that are to be inserted into str such that
// no three consecutive characters are equal to X
static int maxNumberOfXAddedUtil(String str, char X)
{
// Stores count of consecutive X
int countX = 0;
// Stores count of characters which
// is not equal to X
int countNotX = 0;
// Iterate over characters of string, str
for (int i = 0; i < str.length(); i++) {
// If current character is X
if (str.charAt(i) == X) {
// Update countX
countX++;
}
// If countX is less
// than 3
else if (countX < 3) {
// Update countNotX
countNotX++;
// Update countX
countX = 0;
}
}
// If countX is greater than
// or equal to 3
if (countX >= 3)
return -1;
else
return 2 * (countNotX + 1)
- (str.length() - countNotX);
}
// Function to find maximum count of X that
// are to be inserted into str such that no
// three consecutive characters are equal to X
static void maxNumberOfXAdded(String str, char X)
{
int ans = maxNumberOfXAddedUtil(str, X);
// Print the answer
System.out.println(ans);
}
// Driver Code
public static void main(String[] args)
{
// Given string
String str = "xxyxy";
char X = X;
// Function Call
maxNumberOfXAdded(str, X);
}
}
C#
// C# program for the above approach
using System;
class GFG{
// Utility function to find maximum count of
// X that are to be inserted into str such that
// no three consecutive characters are equal to X
static int maxNumberOfXAddedUtil(string str, char X)
{
// Stores count of consecutive X
int countX = 0;
// Stores count of characters which
// is not equal to X
int countNotX = 0;
// Iterate over characters of string, str
for (int i = 0; i < str.Length; i++) {
// If current character is X
if (str[i] == X) {
// Update countX
countX++;
}
// If countX is less
// than 3
else if (countX < 3) {
// Update countNotX
countNotX++;
// Update countX
countX = 0;
}
}
// If countX is greater than
// or equal to 3
if (countX >= 3)
return -1;
else
return 2 * (countNotX + 1)
- (str.Length - countNotX);
}
// Function to find maximum count of X that
// are to be inserted into str such that no
// three consecutive characters are equal to X
static void maxNumberOfXAdded(string str, char X)
{
int ans = maxNumberOfXAddedUtil(str, X);
// Print the answer
Console.Write(ans);
}
// Driver Code
public static void Main()
{
// Given string
string str = "xxyxy";
char X = 'x';
// Function Call
maxNumberOfXAdded(str, X);
}
}
// This code is contributed by target_2.
Javascript
输出:
3
时间复杂度: O(N)
辅助空间: O(1)