Skip to content

Commit

Permalink
Added Problem no: 76 (Hard)
Browse files Browse the repository at this point in the history
Added README.md and Sol1.java files in in a directory .
76.Minimum Window Substring
issue #212
  • Loading branch information
Bharathlax-2005 authored Oct 30, 2023
1 parent dd00f59 commit f11b2b9
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Hard/76.Minimum Window Substring/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# LeetCode 76.Minimum Window Substring

## Problem Description

Given two strings `s` and `t` of lengths `m` and `n` respectively, return the minimum window substring of `s` such that every character in `t` (including duplicates) is included in the window. If there is no such substring, return the empty string `""`.

The testcases will be generated such that the answer is **unique.**

Example 1:

Input: s = "ADOBECODEBANC", t = "ABC"
Output: "BANC"
Explanation: The minimum window substring "BANC" includes 'A', 'B', and 'C' from string t.

Example 2:

Input: s = "a", t = "a"
Output: "a"
Explanation: The entire string s is the minimum window.

Example 3:

Input: s= "a", t = "aa"
Output:""
Explanation: Both 'a's from t must be included in the window.
Since the largest window of s only has one 'a', return empty string.

**Constraints:**

- `m == s.length`
- `n == t.length`
- `1 <= m, n <= 105`
- `s` and `t` consist of uppercase and lowercase English letters.

Follow up: Could you find an algorithm that runs in `O(m + n)` time?

### Link

https://leetcode.com/problems/minimum-window-substring/
52 changes: 52 additions & 0 deletions Hard/76.Minimum Window Substring/Sol1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import java.util.HashMap;

class Solution {
public String minWindow(String s, String t) {
if (s == null || t == null || s.length() < t.length()) {
return "";
}


HashMap<Character, Integer> tMap = new HashMap<>();
for (char c : t.toCharArray()) {
tMap.put(c, tMap.getOrDefault(c, 0) + 1);
}

int left = 0;
int minLen = Integer.MAX_VALUE;
int minLeft = 0;
int count = tMap.size();

for (int right = 0; right < s.length(); right++) {
char rightChar = s.charAt(right);

if (tMap.containsKey(rightChar)) {
tMap.put(rightChar, tMap.get(rightChar) - 1);
if (tMap.get(rightChar) == 0) {
count--;
}
}


while (count == 0) {
if (right - left + 1 < minLen) {
minLen = right - left + 1;
minLeft = left;
}

char leftChar = s.charAt(left);

if (tMap.containsKey(leftChar)) {
tMap.put(leftChar, tMap.get(leftChar) + 1);
if (tMap.get(leftChar) > 0) {
count++;
}
}

left++;
}
}

return minLen == Integer.MAX_VALUE ? "" : s.substring(minLeft, minLeft + minLen);
}
}

0 comments on commit f11b2b9

Please sign in to comment.