Skip to content

Commit 5afc4eb

Browse files
authored
Merge pull request #907 from rohitdbabar/rohitdbabar-patch-2
Added Solution for LeetCode Problem No. 76
2 parents 3460fe3 + 46276ed commit 5afc4eb

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// https://leetcode.com/problems/minimum-window-substring/
2+
3+
class Solution {
4+
public String minWindow(String s, String t)
5+
{
6+
if(s.length() == 0 || t.length() == 0)
7+
return "";
8+
HashMap<Character,Integer> map = new HashMap<>();
9+
int n = s.length(),m=t.length();
10+
11+
for(int i=0; i<m;i++)
12+
map.put(t.charAt(i) , map.getOrDefault(t.charAt(i), 0)+1);
13+
14+
int count = map.size();
15+
int start =0,end =0,min =Integer.MAX_VALUE;
16+
String substring = "";
17+
18+
while(end < n)
19+
{
20+
char ch = s.charAt(end);
21+
if(map.containsKey(ch))
22+
{
23+
map.put(ch , map.get(ch) -1);
24+
if(map.get(ch) == 0)
25+
count--;
26+
}
27+
if(count > 0)
28+
end++;
29+
30+
else if(count == 0)
31+
{
32+
while(count == 0)
33+
{
34+
if(end-start +1 < min)
35+
{
36+
min = end-start+1;
37+
substring = s.substring(start,end+1);
38+
}
39+
char temp = s.charAt(start);
40+
if(map.containsKey(temp))
41+
{
42+
map.put(temp , map.get(temp)+1);
43+
if(map.get(temp) == 1)
44+
count++;
45+
}
46+
start++;
47+
}
48+
end++;
49+
}
50+
}
51+
return substring;
52+
}
53+
}

0 commit comments

Comments
 (0)