youngyangyang04/leetcode-master-comment

[Vssue]0647.回文子串.md

youngyangyang04 opened this issue · 2 comments

如果字符 s[i] 与字符 s[j] 相同,那么:
若 j - i = 0,例如 1 - 1 = 0,则 a 是回文子串
若 j - i = 1,例如 2 - 1 = 1,则 aa 是回文子串
若 j - i > 1,且 i+1j-1 也是回文子串。例如 b 是回文子串,则 aba 也是。
// 例: s = "aaa"
i = 2:
  j = 2: 满足 'a' == 'a', 满足 2-2 <= 1 (情况), result = 1, dp[2][2] = t
i = 1:
  j = 1: 满足 'a' == 'a', 满足 1-1 <= 1 (情况), result = 2, dp[1][1] = t
  j = 2: 满足 'a' == 'a', 满足 2-1 <= 1 (情况二), result = 3, dp[1][2] = t
i = 0:
  j = 0: 满足 'a' == 'a', 满足 0-0 <= 1 (情况), result = 4, dp[0][0] = t
  j = 1: 满足 'a' == 'a', 满足 1-0 <= 1 (情况二), result = 5, dp[0][1] = t
  j = 2: 满足 'a' == 'a', 满足 dp[1][1] = t (情况三), result = 6, dp[0][2] = t

这题和分割回文串又不一样,这题用dp,后者用回溯