From 176aac0d74fc7f84f9579a94fa11c91214a76285 Mon Sep 17 00:00:00 2001 From: "huangge1199@hotmail.com" Date: Tue, 27 Apr 2021 22:36:46 +0800 Subject: [PATCH] =?UTF-8?q?1332:=E5=88=A0=E9=99=A4=E5=9B=9E=E6=96=87?= =?UTF-8?q?=E5=AD=90=E5=BA=8F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/RemovePalindromicSubsequences.java | 67 +++++++++++++++++++ .../cn/RemovePalindromicSubsequences.md | 48 +++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 LeetCode/src/main/java/leetcode/editor/cn/RemovePalindromicSubsequences.java create mode 100644 LeetCode/src/main/java/leetcode/editor/cn/RemovePalindromicSubsequences.md diff --git a/LeetCode/src/main/java/leetcode/editor/cn/RemovePalindromicSubsequences.java b/LeetCode/src/main/java/leetcode/editor/cn/RemovePalindromicSubsequences.java new file mode 100644 index 0000000..c378288 --- /dev/null +++ b/LeetCode/src/main/java/leetcode/editor/cn/RemovePalindromicSubsequences.java @@ -0,0 +1,67 @@ +//给你一个字符串 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) + +} \ No newline at end of file diff --git a/LeetCode/src/main/java/leetcode/editor/cn/RemovePalindromicSubsequences.md b/LeetCode/src/main/java/leetcode/editor/cn/RemovePalindromicSubsequences.md new file mode 100644 index 0000000..55f1078 --- /dev/null +++ b/LeetCode/src/main/java/leetcode/editor/cn/RemovePalindromicSubsequences.md @@ -0,0 +1,48 @@ +

给你一个字符串 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
+
+ +

 

+ +

提示:

+ + +
Related Topics
  • 字符串
  • \n
  • 👍 58
  • 👎 0
  • \ No newline at end of file