# Maximum count of X that can be inserted with no 3 adjacent characters are X

• Difficulty Level : Expert
• Last Updated : 25 Apr, 2022

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:
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:

1. 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.
2. Now, find the number of groups of consecutive X having a size of 1 or 2 and store it in a variable countX.
3. 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.
4. 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

 `// 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);` `    ``}` `}`

## Python3

 `# Python program for the above approach`   `# 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` `def` `maxNumberOfXAddedUtil(``Str``, X):`   `    ``# Stores count of consecutive X` `    ``countX ``=` `0`   `    ``# Stores count of characters which` `    ``# is not equal to X` `    ``countNotX ``=` `0`   `    ``# Iterate over characters of String, Str` `    ``for` `i ``in` `range``(``len``(``Str``)):`   `        ``# If current character is X` `        ``if` `(``Str``[i] ``=``=` `X):`   `            ``# Update countX` `            ``countX ``+``=` `1`   `        ``# If countX is less` `        ``# than 3` `        ``elif` `(countX < ``3``):`   `            ``# Update countNotX` `            ``countNotX ``+``=` `1`   `            ``# Update countX` `            ``countX ``=` `0`   `    ``# If countX is greater than` `    ``# or equal to 3` `    ``if` `(countX >``=` `3``):` `        ``return` `-``1` `    ``else``:` `        ``return` `2` `*` `(countNotX ``+` `1``) ``-` `(``len``(``Str``) ``-` `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` `def` `maxNumberOfXAdded(``Str``,X):`   `    ``ans ``=` `maxNumberOfXAddedUtil(``Str``, X)`   `    ``# Print the answer` `    ``print``(ans)`   `# Driver code`   `# Given String` `Str` `=` `"xxyxy"`   `X ``=` `'x'`   `# Function Call` `maxNumberOfXAdded(``Str``, X)`   `# This code is contributed by shinjanpatra`

## 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

 ``

Output:

`3`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Recommended Articles
Page :