4

Given a string, I know how to find the number of palindromic substrings in linear time using Manacher's algorithm. But now I need to find the number of distinct/unique palindromic substrings. Now, this might lead to an O(n + n^2) algorithm - one 'n' for finding all such substrings, and n^2 for comparing each of these substrings with the ones already found, to check if it is unique.

I am sure there is an algorithm with better complexity. I was thinking of maybe trying my luck with suffix trees? Is there an algorithm with better time complexity?

Liam Willis
  • 661
  • 6
  • 17
  • Doesn't comparing every element to every other element lead to n²? So I think it'd be O(n + n²) = O(n²). Which I agree is still pretty bad, nonetheless. – CompuChip Dec 09 '13 at 14:51

2 Answers2

3

I would just put substrings you found into the hash table to prevent holding the same results twice.

The access time to hash table is O(1).

zavg
  • 10,351
  • 4
  • 44
  • 67
  • 2
    I know how to find the _number_ of such substrings in linear time, not the substrings themselves. Basically I am talking about the Manacher algorithm. – Liam Willis Dec 09 '13 at 15:11
  • 2
    @LiamWillis: Wikipedia http://en.wikipedia.org/wiki/Longest_palindromic_substring doesn't mention *number* but explicitly states: *However, as observed e.g., by Apostolico, Breslauer & Galil (1995), the same algorithm can also be used to find all maximal palindromic substrings anywhere within the input string, again in linear time.* Note word **find** used in this sentence. I'm confused. – Hynek -Pichi- Vychodil Dec 09 '13 at 16:17
  • 2
    In the same Manacher's algorithm, as you find the substrings around a centre, just store it in a hash table as suggested by @zavg. – Abhishek Bansal Dec 09 '13 at 16:22
  • 1
    we will have to calculate hash values of all palindromes possible and in a string of length L we can have L^2 palindromes , so doesn't the algorithm become O(n^2). – SHB Dec 13 '13 at 04:21
2

As of 2015, there is a linear time algorithm for computing the number of distinct palindromic substrings of a given string S. You can use a data structure known as an eertree (or palindromic tree), as described in the linked paper. The idea is fairly complicated, but the premise is to build a trie of palindromes, and augment it with longest proper palindromic suffixes in a similar manner to the failure function of the Aho-Corasick Algorithm. See the original paper for more details: https://arxiv.org/pdf/1506.04862.pdf

Eric Zhang
  • 591
  • 6
  • 13