相关问题1:最长回文子串
相关问题2:[LeetCode] Shortest Palindrome I
给你一个字符串S,插入一些字符,把 S 转换成一个回文字符串。
例如:
ab: Number of insertions required is 1. bab
aa: Number of insertions required is 0. aa
abcd: Number of insertions required is 3. dcbabcd
abcda: Number of insertions required is 2. adcbcda which is same as number of insertions in the substring bcd
思路:可以用递归的方法。
The minimum number of insertions in the string str[l…..h] can be given as:
minInsertions(str[l+1…..h-1]) if str[l] == str[h]
min(minInsertions(str[l…..h-1]), minInsertions(str[l+1…..h])) + 1 if str[l] != str[h]
本文探讨了如何通过最少次数的插入将一个普通字符串转换为回文字符串的问题,并提供了具体的解决思路与示例。文中详细解释了递归方法的应用,包括如何计算所需的最小插入次数。

被折叠的 条评论
为什么被折叠?



