Q005 Longest Palindromic Substring
defines f(i...j): string[i...j] is palindrome or not
if i == j, we are checking ''
or j - i == 1 'a', j - i == 2, 'aa', just need to make sure stirng[i] == string[j]
j - i > 2, we have to check if stirng[i] == string[j] and f[i+1...j-1]
f(i...j) = string[i] == string[j] && (j - i < 3 || f(i+1...j-1))
So we need a 2D array to store results of sub problems
https://github.com/Sylvia-YiyinShen/Algorithm/blob/master/DynamicProgramming/Q005LongestPalindrome.playground/Contents.swift
No comments:
Post a Comment