Leetcode 5: Longest Palindromic Substring
Given a string s, return the longest palindromic substring in s. Example 1: 1 2 3 Input: s = "babad" Output: "bab" Explanation: "aba" is also a valid answer. Example 2: 1 2 Input: s = "cbbd" Output: "bb" Constraints: 1 <= s.length <= 1000 s consist of only digits and English letters. Solution 因為要找最大迴文子字串,而迴文字串前後讀法都一樣,意即可以將題目理解為若字串 s="babad 而將字串倒至則 reverse(s)="dabab,找到這兩個字串最大相同子字串,並且 要再正確的位置(相對倒至相同之位置). Step 1. 如何找最大相同子字串? if len(s1) = len(s2) 將s1, s2組成二維矩陣 比對矩陣對應字母是否相等 找到最長標記相同對角線即為最大相同子字串 若 s1="acbcbcef", s2="abcbced", 則矩陣...