Given an integer n. Find a string of characters ‘a’ and ‘b’ such that the string doesn’t contain any palindrome of length 3.

**Examples:**

Input : 3 Output : "aab" Explanation: aab is not a palindrome. Input : 5 Output : aabba Explanation: aabba does not contain a palindrome of size 3.

The approach here is that we can use this string ‘aabb’ and print the characters of the string according to the given integer.

We need to make make sure that every third character is different. If we perform operation AND on i and 2 where i = 0 to any positive integer. It will generate a pattern 0, 0, 2, 2, 0, 0, 2, 2,... 0 AND 2 = 0 1 AND 2 = 0 2 AND 2 = 2 3 AND 2 = 2 4 AND 2 = 0 //repeat the pattern.

Below is the code of above approach.

## C++

`// CPP program find a binary string of ` `// given length that doesn't contain ` `// a palindrome of size 3. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `void` `generatestring(` `int` `n) ` `{ ` ` ` `// Printing the character according to i ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `putchar` `(i & 2 ? ` `'b'` `: ` `'a'` `); ` ` ` `puts` `(` `""` `); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` `generatestring(n); ` ` ` `n = 8; ` ` ` `generatestring(n); ` ` ` `n = 10; ` ` ` `generatestring(n); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// JAVA program find a binary String of ` `// given length that doesn't contain ` `// a palindrome of size 3. ` ` ` `class` `GFG ` `{ ` ` ` `static` `void` `generateString(` `int` `n) ` `{ ` ` ` `String s = ` `""` `; ` ` ` ` ` `// Printing the character according to i ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `s += ((i & ` `2` `) > ` `1` `? ` `'b'` `: ` `'a'` `); ` ` ` `System.out.println(s); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `5` `; ` ` ` `generateString(n); ` ` ` `n = ` `8` `; ` ` ` `generateString(n); ` ` ` `n = ` `10` `; ` ` ` `generateString(n); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program find a binary String of ` `# given length that doesn't contain ` `# a palindrome of size 3. ` `def` `generateString(n): ` ` ` `s ` `=` `""; ` ` ` ` ` `# Printing the character according to i ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `if` `((i & ` `2` `) > ` `1` `): ` ` ` `s ` `+` `=` `'b'` `; ` ` ` `else` `: ` ` ` `s ` `+` `=` `'a'` `; ` ` ` `print` `(s); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `n ` `=` `5` `; ` ` ` `generateString(n); ` ` ` `n ` `=` `8` `; ` ` ` `generateString(n); ` ` ` `n ` `=` `10` `; ` ` ` `generateString(n); ` ` ` `# This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## C#

`// C# program find a binary String of ` `// given length that doesn't contain ` `// a palindrome of size 3. ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `static` `void` `generateString(` `int` `n) ` `{ ` ` ` `String s = ` `""` `; ` ` ` ` ` `// Printing the character according to i ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `s += ((i & 2) > 1 ? ` `'b'` `: ` `'a'` `); ` ` ` `Console.WriteLine(s); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 5; ` ` ` `generateString(n); ` ` ` `n = 8; ` ` ` `generateString(n); ` ` ` `n = 10; ` ` ` `generateString(n); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

aabba aabbaabb aabbaabbaa

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count substring of Binary string such that each character belongs to a palindrome of size greater than 1
- Number of ways to make binary string of length N such that 0s always occur together in groups of size K
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Maximum even length sub-string that is permutation of a palindrome
- Check if string can be rearranged so that every Odd length Substring is Palindrome
- Minimum flips required in a binary string such that all K-size substring contains 1
- Check if a binary string contains all permutations of length k
- Minimum size binary string required such that probability of deleting two 1's at random is 1/X
- Maximum number of set bits count in a K-size substring of a Binary String
- Length of longest consecutive ones by at most one swap in a Binary String
- Number of sub-sequences of non-zero length of a binary string divisible by 3
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Maximum length of consecutive 1's in a binary string in Python using Map function
- Nth Even length Palindrome
- Sum of first K even-length Palindrome numbers
- Length of the longest substring that do not contain any palindrome
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- Maximum length palindrome that can be created with characters in range L and R
- Total number of odd length palindrome sub-sequence around each centre
- Convert the string into palindrome string by changing only one character.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.