Skip to content

Commit cff8014

Browse files
add a solution for 76
1 parent b5c9395 commit cff8014

File tree

2 files changed

+77
-10
lines changed

2 files changed

+77
-10
lines changed

src/main/java/com/fishercoder/solutions/_76.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,45 @@ public String minWindow(String s, String t) {
4141
return s.substring(minStart, minStart + minLen);
4242
}
4343
}
44+
45+
public static class Solution2 {
46+
/**
47+
* I implemented below solution on my own following the hints on LeetCode.
48+
* In comparison, Solution1 is more optimized and runs faster.
49+
*/
50+
public String minWindow(String s, String t) {
51+
if (t.length() > s.length()) {
52+
return "";
53+
}
54+
int[] tCount = new int[256];
55+
for (int i = 0; i < t.length(); i++) {
56+
tCount[t.charAt(i)]++;
57+
}
58+
int left = 0;
59+
int right = 0;
60+
int[] sCount = new int[256];
61+
String ans = "";
62+
while (right < s.length()) {
63+
sCount[s.charAt(right)]++;
64+
while (isValid(sCount, tCount)) {
65+
if (right - left < ans.length() || ans.equals("")) {
66+
ans = s.substring(left, right + 1);
67+
}
68+
sCount[s.charAt(left)]--;
69+
left++;
70+
}
71+
right++;
72+
}
73+
return ans;
74+
}
75+
76+
private boolean isValid(int[] sCount, int[] tCount) {
77+
for (int i = 0; i < sCount.length; i++) {
78+
if (sCount[i] < tCount[i]) {
79+
return false;
80+
}
81+
}
82+
return true;
83+
}
84+
}
4485
}

src/test/java/com/fishercoder/_76Test.java

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,42 @@
77
import static org.junit.Assert.assertEquals;
88

99
public class _76Test {
10-
private static _76.Solution1 solution1;
11-
private static int[] nums;
10+
private static _76.Solution1 solution1;
11+
private static _76.Solution2 solution2;
12+
private static String expected;
13+
private static String s;
14+
private static String t;
1215

13-
@BeforeClass
14-
public static void setup() {
15-
solution1 = new _76.Solution1();
16-
}
16+
@BeforeClass
17+
public static void setup() {
18+
solution1 = new _76.Solution1();
19+
solution2 = new _76.Solution2();
20+
}
1721

18-
@Test
19-
public void test1() {
20-
assertEquals("BANC", solution1.minWindow("ADOBECODEBANC", "ABC"));
21-
}
22+
@Test
23+
public void test1() {
24+
expected = "BANC";
25+
s = "ADOBECODEBANC";
26+
t = "ABC";
27+
assertEquals(expected, solution1.minWindow(s, t));
28+
assertEquals(expected, solution2.minWindow(s, t));
29+
}
30+
31+
@Test
32+
public void test2() {
33+
expected = "";
34+
s = "A";
35+
t = "B";
36+
assertEquals(expected, solution1.minWindow(s, t));
37+
assertEquals(expected, solution2.minWindow(s, t));
38+
}
39+
40+
@Test
41+
public void test3() {
42+
expected = "cwae";
43+
s = "cabwefgewcwaefgcf";
44+
t = "cae";
45+
assertEquals(expected, solution1.minWindow(s, t));
46+
assertEquals(expected, solution2.minWindow(s, t));
47+
}
2248
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy