回文串

🔸验证回文串 https://leetcode-cn.com/problems/valid-palindrome/solution/

  • Character.isLetterOrDigit(char)
  • 回文串的一个巧妙方法:reverse后与源字符串比较是否相同

🔸验证回文字符串2 https://leetcode-cn.com/problems/valid-palindrome-ii/


🔸最长回文串 https://leetcode-cn.com/problems/longest-palindrome/


🔸最长回文子串 https://leetcode-cn.com/problems/longest-palindromic-substring/


🔸回文子串(的数量) https://leetcode-cn.com/problems/palindromic-substrings/

  • 中心扩展法
    遍历枚举出所有的回文字串:两种方法
    ①枚举出所有的字符串,并判断是否是回文串
    ②枚举出所有的中心位置,向两边扩展,以产生所有的回文子串(中心扩展法)。中心可能是一个char也可能是两个char,可分奇偶遍历两次,也可在一个循环中解决(n=2时共有2*n-1钟中心位置)
  • Manacher算法:Manacher用来求解最大回文串,width[i]中存放的是以I为中心的最长的回文串的长度,要求出所有的回文串数量,需要除以2

🔸分割回文串 https://leetcode-cn.com/problems/palindrome-partitioning/

  • dp预处理+回溯(dfs) 只能写出dfs,实在不会写迭代了,等刷搜索题的时候再学习吧(📌补)
  • 复杂度O(n*2^n)

🔸破坏回文串 https://leetcode-cn.com/problems/break-a-palindrome/

  • 我觉得它不配是一个中等题,但我更不配是一个刷题人,竟然WA了好几次

🔸构建回文串检测 https://leetcode-cn.com/problems/can-make-palindrome-from-substring/

  • 记录(0,i)的26个英文字母的奇偶情况
  • 可以使用二进制来解决

🔸分割两个字符串得到回文串 https://leetcode-cn.com/problems/split-two-strings-to-make-palindrome/

  • 头尾进行比较 2种
  • 找到分割点之后,2种构造方法
    所以一共有四种需要判断的情况

🔸分割回文串 II https://leetcode-cn.com/problems/palindrome-partitioning-ii/

  • 中心思想:dp
  • 两次dp,第一次预处理,用于判断任意一个子串是否为回文串;第二次一维判断【0,i】的最短分割数

🔸 分割回文串 III https://leetcode-cn.com/problems/palindrome-partitioning-iii/
(📌补)


🔸 回文串分割 IV https://leetcode-cn.com/problems/palindrome-partitioning-iv/

  • 要求判断能否分割为3个回文子串,直接指出了常数个数,就不用dp了
  • dp预处理后枚举两个分割点的位置即可
  • 也可以使用Manacher算法(📌补)

🔸最短回文串 https://leetcode-cn.com/problems/shortest-palindrome/

  • 基本思路比较简单,求一个最长前缀,且该前缀是回文串,但是基础的方法会超时
  • 方法一:使用Manacher算法
  • 方法二:kmp,next数组中存储的就是最长相同前后缀的长度,妙啊~

对于回文串来说常用的方法:

  • reverse
  • 中心扩展
  • dp🌟
  • 回溯
  • 对于26个英文字母的特殊处理
  • Manacher算法
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容