Count number of palindromes alphabetically from a to z
Answers
It depends (slightly) on whether you want an odd number of digits in your string, or an even number of digits in your string.
Let's say that the "alphabet" contains N different "characters". (These could be the digits 1-9, or the letters A-Z, or any other set of distinguishable symbols.) You want to know how many palindromes of a given length there are.
If the string length is to be even: Say the string length is 2k. You can choose each of the first k characters to be anything you want; once you've chosen those, the rest of the string is forced on you by the required symmetry. So there are k free choices, each drawn from a set of N characters; that means there are Nk possibilities.
If the string length is to be odd: Say the string length is 2k+1. You can choose each of the first k+1 characters to be anything you want; once you've chosen those, the rest of the string is forced on you by the required symmetry. So there are k+1 free choices, each drawn from a set of N characters; that means there are Nk+1 possibilities.
✌✌✌✌✌✌✌✌✌✌✌✌✌✌✌✌
It depends (slightly) on whether you want an odd number of digits in your string, or an even number of digits in your string.
Let's say that the "alphabet" contains N different "characters". (These could be the digits 1-9, or the letters A-Z, or any other set of distinguishable symbols.) You want to know how many palindromes of a given length there are.