Skip to content

Commit 432950b

Browse files
add 1337
1 parent df54486 commit 432950b

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ _If you like this project, please leave me a star._ ★
1111
|1343|[Maximum Product of Splitted Binary Tree](https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1343.java) | |Medium||
1212
|1342|[Reduce Array Size to The Half](https://leetcode.com/problems/reduce-array-size-to-the-half/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1342.java) | |Medium||
1313
|1341|[The K Weakest Rows in a Matrix](https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1341.java) | |Easy||
14+
|1337|[Remove Palindromic Subsequences](https://leetcode.com/problems/remove-palindromic-subsequences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1337.java) | |Easy|String|
1415
|1333|[Filter Restaurants by Vegan-Friendly, Price and Distance](https://leetcode.com/problems/filter-restaurants-by-vegan-friendly-price-and-distance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1333.java) | |Medium||
1516
|1331|[Rank Transform of an Array](https://leetcode.com/problems/rank-transform-of-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1331.java) | |Easy||
1617
|1329|[Sort the Matrix Diagonally](https://leetcode.com/problems/sort-the-matrix-diagonally/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1329.java) | |Medium||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1337. Remove Palindromic Subsequences
5+
*
6+
* Given a string s consisting only of letters 'a' and 'b'. In a single step you can remove one palindromic subsequence from s.
7+
* Return the minimum number of steps to make the given string empty.
8+
* A string is a subsequence of a given string, if it is generated by deleting some characters of a given string without changing its order.
9+
* A string is called palindrome if is one that reads the same backward as well as forward.
10+
*
11+
* Example 1:
12+
* Input: s = "ababa"
13+
* Output: 1
14+
* Explanation: String is already palindrome
15+
*
16+
* Example 2:
17+
* Input: s = "abb"
18+
* Output: 2
19+
* Explanation: "abb" -> "bb" -> "".
20+
* Remove palindromic subsequence "a" then "bb".
21+
*
22+
* Example 3:
23+
* Input: s = "baabb"
24+
* Output: 2
25+
* Explanation: "baabb" -> "b" -> "".
26+
* Remove palindromic subsequence "baab" then "b".
27+
*
28+
* Example 4:
29+
* Input: s = ""
30+
* Output: 0
31+
*
32+
* Constraints:
33+
* 0 <= s.length <= 1000
34+
* s only consists of letters 'a' and 'b'
35+
* */
36+
public class _1337 {
37+
public static class Solution1 {
38+
public int removePalindromeSub(String s) {
39+
if (s.isEmpty()) {
40+
return 0;
41+
} else {
42+
String reverse = new StringBuilder(s).reverse().toString();
43+
if (s.equals(reverse)) {
44+
return 1;
45+
} else {
46+
return 2;
47+
}
48+
}
49+
}
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1337;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1337Test {
10+
private static _1337.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1337.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(1, solution1.removePalindromeSub("ababa"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(2, solution1.removePalindromeSub("abb"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(2, solution1.removePalindromeSub("baabb"));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(0, solution1.removePalindromeSub(""));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)