leet-code/src/main/java/leetcode/editor/cn/RemovePalindromicSubsequences.java

67 lines
1.7 KiB
Java
Raw Normal View History

2021-04-27 22:36:46 +08:00
//给你一个字符串 s它仅由字母 'a' 和 'b' 组成。每一次删除操作都可以从 s 中删除一个回文 子序列。
//
// 返回删除给定字符串中所有字符(字符串为空)的最小删除次数。
//
// 「子序列」定义:如果一个字符串可以通过删除原字符串某些字符而不改变原字符顺序得到,那么这个字符串就是原字符串的一个子序列。
//
// 「回文」定义:如果一个字符串向后和向前读是一致的,那么这个字符串就是一个回文。
//
//
//
// 示例 1
//
// 输入s = "ababa"
//输出1
//解释:字符串本身就是回文序列,只需要删除一次。
//
//
// 示例 2
//
// 输入s = "abb"
//输出2
//解释:"abb" -> "bb" -> "".
//先删除回文子序列 "a",然后再删除 "bb"。
//
//
// 示例 3
//
// 输入s = "baabb"
//输出2
//解释:"baabb" -> "b" -> "".
//先删除回文子序列 "baab",然后再删除 "b"。
//
//
// 示例 4
//
// 输入s = ""
//输出0
//
//
//
//
// 提示:
//
//
// 0 <= s.length <= 1000
// s 仅包含字母 'a' 和 'b'
//
// Related Topics 字符串
// 👍 58 👎 0
package leetcode.editor.cn;
//1332:删除回文子序列
public class RemovePalindromicSubsequences{
public static void main(String[] args) {
//测试代码
Solution solution = new RemovePalindromicSubsequences().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int removePalindromeSub(String s) {
return new StringBuilder(s).reverse().toString().equals(s) ? 1 : 2;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}