A palindrome is a string whose reversal is identical to the string. How many bitstrings of length n are palindromes?
Question
A palindrome is a string whose reversal is identical to the string. How many bitstrings of length n are palindromes?
Solution
The number of palindromic bitstrings of length n depends on whether n is even or odd.
-
If n is even, then the first n/2 bits can be anything, and the last n/2 bits are determined by the first n/2 bits (they must be the same in reverse order). Since each bit can be either 0 or 1, there are 2^(n/2) possible bitstrings for the first n/2 bits. Therefore, there are 2^(n/2) palindromic bitstrings of length n when n is even.
-
If n is odd, then the first (n-1)/2 bits can be anything, the middle bit can be anything, and the last (n-1)/2 bits are determined by the first (n-1)/2 bits. Therefore, there are 2^((n-1)/2) * 2 = 2^((n+1)/2) palindromic bitstrings of length n when n is odd.
So, in general, the number of palindromic bitstrings of length n is 2^(ceil(n/2)), where ceil is the ceiling function (rounding up to the nearest integer).
Similar Questions
Write a python program to count the number of palindrome strings that occurred in the given string.
Write a C++ program to check if a given number is a palindrome. A palindrome is a number that remains the same when its digits are reversed.
Which among the following methods is used to reverse the sequence of characters in a StringBuilder object?a)backwards()b)invert()c)reverse()d)flip()
The sum of two digits of a number is 15. if 9 is added to the number, the digit is reversed. The numbers are?
You are given a number ’n’.Find the number of digits of ‘n’ that evenly divide ‘n’.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.