Maximum count of X that can be inserted with no 3 adjacent characters are X
Given a string, str of length N and a character X, the task is to find the maximum count of characters X that are to be inserted into the string such that no three consecutive characters are equal to X. If it is not possible to find such a string, then print -1.
Examples:
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
Approach: The idea is to count all the positions where X can be inserted and then subtract the count of already present X in the string.
Below are the steps:
- The maximum number of X that can be inserted in the string is 2 * (N + 1) characters as it is possible to insert two X at the start and end of the string and between every consecutive character.
- Now, find the number of groups of consecutive X having a size of 1 or 2 and store it in a variable countX.
- The number of X that can be inserted in the given string is 2 * (number of places to be inserted + 1) – number of found Xs.
- In conclusion, a simple mathematical formula, 2 * (N + 1) – (N – Xs) can be used to find the final answer.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int maxNumberOfXAddedUtil(string str, char X)
{
int countX = 0;
int countNotX = 0;
for ( int i = 0; i < str.size(); i++)
{
if (str[i] == X)
{
countX++;
}
else if (countX < 3)
{
countNotX++;
countX = 0;
}
}
if (countX >= 3)
return -1;
else
return 2 * (countNotX + 1) - (str.size() - countNotX);
}
static void maxNumberOfXAdded(string str, char X)
{
int ans = maxNumberOfXAddedUtil(str, X);
cout << (ans);
}
int main()
{
string str = "xxyxy" ;
char X = 'x' ;
maxNumberOfXAdded(str, X);
}
|
Java
import java.util.*;
public class Main {
static int maxNumberOfXAddedUtil(String str, char X)
{
int countX = 0 ;
int countNotX = 0 ;
for ( int i = 0 ; i < str.length(); i++) {
if (str.charAt(i) == X) {
countX++;
}
else if (countX < 3 ) {
countNotX++;
countX = 0 ;
}
}
if (countX >= 3 )
return - 1 ;
else
return 2 * (countNotX + 1 )
- (str.length() - countNotX);
}
static void maxNumberOfXAdded(String str, char X)
{
int ans = maxNumberOfXAddedUtil(str, X);
System.out.println(ans);
}
public static void main(String[] args)
{
String str = "xxyxy" ;
char X = X;
maxNumberOfXAdded(str, X);
}
}
|
Python3
def maxNumberOfXAddedUtil( Str , X):
countX = 0
countNotX = 0
for i in range ( len ( Str )):
if ( Str [i] = = X):
countX + = 1
elif (countX < 3 ):
countNotX + = 1
countX = 0
if (countX > = 3 ):
return - 1
else :
return 2 * (countNotX + 1 ) - ( len ( Str ) - countNotX)
def maxNumberOfXAdded( Str ,X):
ans = maxNumberOfXAddedUtil( Str , X)
print (ans)
Str = "xxyxy"
X = 'x'
maxNumberOfXAdded( Str , X)
|
C#
using System;
class GFG{
static int maxNumberOfXAddedUtil( string str, char X)
{
int countX = 0;
int countNotX = 0;
for ( int i = 0; i < str.Length; i++) {
if (str[i] == X) {
countX++;
}
else if (countX < 3) {
countNotX++;
countX = 0;
}
}
if (countX >= 3)
return -1;
else
return 2 * (countNotX + 1)
- (str.Length - countNotX);
}
static void maxNumberOfXAdded( string str, char X)
{
int ans = maxNumberOfXAddedUtil(str, X);
Console.Write(ans);
}
public static void Main()
{
string str = "xxyxy" ;
char X = 'x' ;
maxNumberOfXAdded(str, X);
}
}
|
Javascript
<script>
function maxNumberOfXAddedUtil(str, X)
{
let countX = 0;
let countNotX = 0;
for (let i = 0; i < str.length; i++)
{
if (str[i] == X)
{
countX++;
}
else if (countX < 3)
{
countNotX++;
countX = 0;
}
}
if (countX >= 3)
return -1;
else
return 2 * (countNotX + 1) - (str.length - countNotX);
}
function maxNumberOfXAdded(str,X)
{
let ans = maxNumberOfXAddedUtil(str, X);
document.write(ans);
}
let str = "xxyxy" ;
let X = 'x' ;
maxNumberOfXAdded(str, X);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
25 Apr, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...