I need to prove that in a string with a number of characters n, at most n distinct, non-empty palindromic substrings are possible. I can understand that this is because every character can be a palindrome in and of itself, and so the max possible number of substrings possible is going to be equal to the number of characters in the string. However, I cannot seem to express this in the form of mathematical proof. How can I do so?
-
`aaa` size 3: `a`, `a`, `a`, `aa`, `aa`, `aaa`, count 6. That seems to disprove your assertion. I don't get it. Unless you discount single characters? – Neil Dec 12 '21 at 18:07
-
I mentioned "distinct". In your case, if we count only the distinct palindromes, then we will count a, and aa only once and not three a's and two aa's. – user202004 Dec 12 '21 at 18:10
-
So items are indistinct. Maybe with a recurrence; one shows that, with another item, the maximum one can be added? Maybe CS or Math would get you more results? – Neil Dec 12 '21 at 18:22
-
A recurrence on the number of characters seems like a good idea. If you have a string with n+1 character, consider the possibilities for the last character: it might or might not be equal to a character present in the first n characters. How many palindromes does every case add? – Stef Dec 12 '21 at 18:51
1 Answers
Given xc, where x is any string and c is any character:
All substrings of xc that are not already in x must be suffixes of xc. If they're palindromes, then they have the form cyc, where cy is a suffix of x and y is a palindrome or empty.
Imagine that there are two such new palindromic suffixes. Since they are distinct suffixes of the same string, they must be different lengths, so we have cyczc, where z and ycz are both palindromes or empty.
If ycz is a palindrome, there are a few cases to consider.
If len(y) = len(z), then that would imply that y = z and that czc, therefore, was already a substring of x -- a contradiction.
If z is shorter, then we can partition again into czrcwczc, but z is a palindrome so zr = z and we have the same contradiction.
If z is longer, then we can partition into cycwcyrc, with wcyr = z, and that's a palindrome, so also z = ycw and again the contradiction that czc already appears in x.
So... adding a single character to a string can introduce at most one new palindromic substring. The total number of such substrings in any string, therefore, cannot be more than the string's length.

- 53,709
- 3
- 46
- 87
-
-
1-character strings are trivial -- they're only new if there are no other occurrences of that character in the string, in which case the only palindromic suffix is the single character. – Matt Timmermans Dec 13 '21 at 13:36
-
1In terms of the proof, there is nothing about 1-character strings that invalidates the assertion that if you have two new palindromic suffixes, then you must have the cyczc form. – Matt Timmermans Dec 13 '21 at 14:00