Skip to content

Commit f0278a8

Browse files
[N-0] add 727
1 parent 6e2ac92 commit f0278a8

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2424
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
25+
|727|[Minimum Window Subsequence](https://leetcode.com/problems/minimum-window-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_727.java) | O(m*n) | O(m*n) | Hard | DP
2526
|725|[Split Linked List in Parts](https://leetcode.com/problems/split-linked-list-in-parts/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_725.java) | O(n+k) | O(k) | Medium | LinkedList
2627
|724|[Find Pivot Index](https://leetcode.com/problems/find-pivot-index/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_724.java) | O(n) | O(1) | Easy | Array
2728
|723|[Candy Crush](https://leetcode.com/problems/candy-crush/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_723.java) | O((r*c)^2) | O((r*c)) | Medium | Array, Two Pointers
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 727. Minimum Window Subsequence
7+
*
8+
* Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequence of W.
9+
* If there is no such window in S that covers all characters in T,
10+
* return the empty string "". If there are multiple such minimum-length windows, return the one with the left-most starting index.
11+
12+
Example 1:
13+
Input:
14+
S = "abcdebdde", T = "bde"
15+
Output: "bcde"
16+
17+
Explanation:
18+
"bcde" is the answer because it occurs before "bdde" which has the same length.
19+
"deb" is not a smaller window because the elements of T in the window must occur in order.
20+
21+
Note:
22+
All the strings in the input will only contain lowercase letters.
23+
The length of S will be in the range [1, 20000].
24+
The length of T will be in the range [1, 100].
25+
*/
26+
public class _727 {
27+
public static class Solution1 {
28+
/**
29+
* This naive brute force results in TLE.
30+
*/
31+
public String minWindow(String S, String T) {
32+
String result = S;
33+
for (int i = 0; i < S.length(); i++) {
34+
for (int j = i + T.length(); j <= S.length(); j++) {
35+
String sub = S.substring(i, j);
36+
if (sub.length() < result.length() && isSubsequence(T, sub)) {
37+
result = sub;
38+
}
39+
}
40+
}
41+
return result.equals(S) ? "" : result;
42+
}
43+
44+
private boolean isSubsequence(String T, String sub) {
45+
int i = 0;
46+
for (int j = 0; i < T.length() && j < sub.length(); j++) {
47+
if (T.charAt(i) == sub.charAt(j)) {
48+
i++;
49+
}
50+
}
51+
return i == T.length();
52+
}
53+
}
54+
55+
public static class Solution2 {
56+
/**credit: https://github.com/lydxlx1/LeetCode/blob/master/src/_727.java*/
57+
public String minWindow(String S, String T) {
58+
int[][] dp = new int[S.length() + 1][T.length() + 1];
59+
int INFINITY = 1 << 29;
60+
Arrays.fill(dp[0], INFINITY);
61+
dp[0][0] = 0;
62+
for (int i = 1; i <= S.length(); i++) {
63+
for (int j = 1; j <= T.length(); j++) {
64+
dp[i][j] = dp[i - 1][j] + 1;
65+
if (S.charAt(i - 1) == T.charAt(j - 1)) {
66+
dp[i][j] = dp[i - 1][j - 1] + 1;
67+
}
68+
}
69+
}
70+
int ans = INFINITY;
71+
int tail = -1;
72+
for (int i = 1; i <= S.length(); i++) {
73+
if (dp[i][T.length()] < ans) {
74+
ans = dp[i][T.length()];
75+
tail = i;
76+
}
77+
}
78+
return ans == INFINITY ? "" : S.substring(tail - ans, tail);
79+
}
80+
81+
}
82+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._727;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _727Test {
10+
private static _727.Solution1 solution1;
11+
private static _727.Solution2 solution2;
12+
private static String S;
13+
private static String T;
14+
15+
@Before
16+
public void setup() {
17+
solution1 = new _727.Solution1();
18+
solution2 = new _727.Solution2();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
S = "abcdebdde";
24+
T = "bde";
25+
assertEquals("bcde", solution1.minWindow(S, T));
26+
assertEquals("bcde", solution2.minWindow(S, T));
27+
}
28+
29+
@Test
30+
public void test2() {
31+
String S = "jmeqksfrsdcmsiwvaovztaqenprpvnbstl";
32+
String T = "l";
33+
assertEquals("l", solution1.minWindow(S, T));
34+
assertEquals("l", solution2.minWindow(S, T));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)