Data Structures and Algorithms B
  • Introduction
  • Stable Marrage
  • Huffman Codes
    • ABL
    • Implementation
  • Graph Algorithms
    • Connect Component
    • Bipartiteness
    • Strongly Connected Components
      • Implementation
    • Topological Sort
      • Implementation 1
      • Implementation 2
    • Dijkstra’s Shortest Path
    • Minimum Spanning Tree
      • Prims
        • Implementation 1
      • Kruskels
        • Implementation 1
      • Traveling Salesman Problem
    • k-Clustering
    • Dynamic Programming with Trees
      • Implementation 1
      • Implementation 2
    • Disjoint Sets
      • Implementation
    • Eularian Cycle
      • Implementation
    • Hamiltonian Path
      • Implementation
  • Divide and Conquer
    • Merge Sort
    • Integer Multiplication
    • Closest Pair
    • Master Method
  • Dynamic Programming
    • Interval Scheduling
    • Knapsack Problem
  • Network Flow
    • Maximum Network Flow
    • Improvements
    • Image Segmentation
  • String Algorithms
    • Z Algorithm
    • Boyer-Moore
    • Knuth-Morris-Pratt
    • Suffix Trees
      • Naive Implementation
      • Ukkonens Algorithm
        • Implementation
      • Applications
        • Longest Common Substring
        • Longest Palindromic Substring
        • Longest Repeated Substring
  • Randomized Algorithms
    • Bloom Filters
      • Implementation
Powered by GitBook
On this page

Was this helpful?

  1. String Algorithms
  2. Suffix Trees
  3. Applications

Longest Palindromic Substring

Given a strings, find the longest palindromic substring ins. You may assume that the maximum length ofsis 1000.

Example:

Input:
 "babad"


Output:
 "bab"


Note:
 "aba" is also a valid answer.

Example:

Input:
 "cbbd"


Output:
 "bb"

Solution

Assume the string S = "banana". Begin by building the suffix tree, for S + R, where R is the reverse of S.

S = b a n a n a $ a n a n a b #
I = 1 2 3 4 5 6 7 8 9 0 1 2 3 4

Finally, return the path label along the LCS.

Why this works

This works exactly for the same reasons justified in the previous application. Since a palindrome is the same both backwards and forwards, its common shared occurrence will remain the same when the pattern is reversed. Then the properties of the LCS remain the same as before.

PreviousLongest Common SubstringNextLongest Repeated Substring

Last updated 5 years ago

Was this helpful?