Given an integer n. Find a string of characters ‘a’ and ‘b’ such that the string doesn’t contain any palindrome of length 3.
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.
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.
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- 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
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- Count all palindrome which is square of a palindrome
- 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
- Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.
- To check a number is palindrome or not without using any extra space
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- K length words that can be formed from given characters without repetition
- Convert the string into palindrome string by changing only one character.
- Generate a string whose all K-size substrings can be concatenated to form the given string
- Length of longest palindrome list in a linked list using O(1) extra space
- Nth Even length Palindrome
- Sum of first K even-length Palindrome numbers
- Length of the longest substring that do not contain any palindrome
- 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
- Longest palindrome formed by concatenating and reordering strings of equal length
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.