-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added README.md and Sol1.java files in in a directory . 76.Minimum Window Substring issue #212
- Loading branch information
1 parent
dd00f59
commit f11b2b9
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |