Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0a0405e

Browse files
author
Tushar Roy
committed
smallest window containing all chars
1 parent c46be33 commit 0a0405e

File tree

1 file changed

+34
-82
lines changed

1 file changed

+34
-82
lines changed

‎src/com/interview/string/SmallestWindowContaingAllCharacters.java‎

Lines changed: 34 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -12,97 +12,49 @@ public class SmallestWindowContaingAllCharacters {
1212

1313
public String minWindow(String s, String t) {
1414
Map<Character, Integer> countMap = new HashMap<>();
15-
for (char s1 : t.toCharArray()) {
16-
countMap.compute(s1, (key, val) -> {
17-
if (val == null) {
18-
return 1;
19-
}
20-
return val + 1;
21-
});
22-
}
23-
24-
Map<Character, Integer> foundMap = new HashMap<>();
25-
for (char s1 : t.toCharArray()) {
26-
foundMap.put(s1, 0);
27-
}
28-
29-
int startWindow;
30-
int charsFound = 0;
31-
for (startWindow = 0; startWindow < s.length(); startWindow++) {
32-
if (countMap.containsKey(s.charAt(startWindow))) {
33-
break;
15+
for (char ch : t.toCharArray()) {
16+
Integer val = countMap.get(ch);
17+
if (val == null) {
18+
val = 0;
3419
}
20+
countMap.put(ch, val + 1);
3521
}
36-
int minWindowStart = Integer.MIN_VALUE;
37-
int minWindowEnd = -1;
38-
int totalCharacters = t.length();
39-
for (int i = startWindow; i < s.length(); i++) {
40-
char ch = s.charAt(i);
41-
Integer count = countMap.get(ch);
42-
if (count != null) {
43-
Integer actualCount = foundMap.get(ch);
44-
foundMap.put(ch, actualCount + 1);
45-
if (actualCount < count) {
46-
charsFound++;
47-
if (charsFound == totalCharacters) {
48-
if (i - startWindow < minWindowEnd - minWindowStart) {
49-
minWindowStart = startWindow;
50-
minWindowEnd = i;
51-
}
52-
ShrinkResult sr = shrinkWindow(countMap, foundMap, startWindow, s, i);
53-
if (sr.fullStartWindow != -1) {
54-
if (i - sr.fullStartWindow < minWindowEnd - minWindowStart) {
55-
minWindowStart = sr.fullStartWindow;
56-
minWindowEnd = i;
57-
}
58-
}
59-
startWindow = sr.startWindow;
60-
charsFound--;
61-
}
62-
}
22+
int start = 0;
23+
int currLen = t.length();
24+
int minWindow = Integer.MAX_VALUE;
25+
int minStart = 0;
26+
int i = 0;
27+
while (i < s.length()) {
28+
Integer val = countMap.get(s.charAt(i));
29+
if (val == null) {
30+
i++;
31+
continue;
6332
}
64-
}
65-
if (minWindowEnd == -1) {
66-
return "";
67-
} else {
68-
return s.substring(minWindowStart, minWindowEnd + 1);
69-
}
70-
}
71-
72-
class ShrinkResult {
73-
int startWindow;
74-
int fullStartWindow;
75-
}
76-
77-
private ShrinkResult shrinkWindow(Map<Character, Integer> countMap, Map<Character, Integer> foundMap,
78-
int startWindow, String s, int end) {
79-
boolean firstViolation = false;
80-
int diff = -1;
81-
int i;
82-
for(i = startWindow; i <= end; i++) {
83-
if(!firstViolation) {
84-
diff = i;
33+
if (val > 0) {
34+
currLen--;
8535
}
86-
char ch = s.charAt(i);
87-
Integer count = countMap.get(ch);
88-
if (count != null) {
89-
int actualCount = foundMap.get(ch);
90-
if (firstViolation) {
91-
if (actualCount <= count) {
36+
val--;
37+
countMap.put(s.charAt(i), val);
38+
while (currLen == 0) {
39+
if (minWindow > i - start + 1) {
40+
minWindow = i - start + 1;
41+
minStart = start;
42+
}
43+
Integer val1 = countMap.get(s.charAt(start));
44+
if (val1 != null) {
45+
if (val1 == 0) {
9246
break;
93-
}
94-
} else {
95-
if (actualCount == count) {
96-
firstViolation = true;
47+
} else {
48+
val1++;
49+
countMap.put(s.charAt(start), val1);
9750
}
9851
}
99-
foundMap.compute(ch, (key, val) -> val - 1);
52+
start++;
10053
}
54+
i++;
10155
}
102-
ShrinkResult sr = new ShrinkResult();
103-
sr.fullStartWindow = diff;
104-
sr.startWindow = i;
105-
return sr;
56+
57+
return minWindow != Integer.MAX_VALUE ? s.substring(minStart, minStart + minWindow) : "";
10658
}
10759

10860
public static void main(String args[]) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /