647. Palindromic Substrings
Last updated
Last updated
Given a string, your task is to count how many palindromic substrings in this string. The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters.
Example 1:
Example 2:
v1.
假設每一個字母為中央時,去確認向左向右延伸時是不是回文
- 偶數字母回文時,設定中央為右半格,例如 i = 3
時,另中央為 3 和 4 中間
- 奇數字母回文時,設定中央為自己
slower
由第一個字母開始尋找,每一個字母皆往後找到最後一個
利用 s[i:j+1][::-1]
寫法將文字倒轉
faster
已知 s 會有 2*Length + 1
個檢查中心點
分別為 s[1], s[1] 和 s[2] 中間, s[2], s[2] 和 s[3] 中間, s[3], .... 依序直到最後一個字母,所以每兩次會有一次在中間
在中間時 left 和 right 會分開,其餘相等