From f11b2b991c533557b7bac7801748e7e318abfa47 Mon Sep 17 00:00:00 2001 From: Bharathlax-2005 <116173614+Bharathlax-2005@users.noreply.github.com> Date: Tue, 31 Oct 2023 01:31:27 +0530 Subject: [PATCH] Added Problem no: 76 (Hard) Added README.md and Sol1.java files in in a directory . 76.Minimum Window Substring issue #212 --- Hard/76.Minimum Window Substring/README.md | 39 ++++++++++++++++ Hard/76.Minimum Window Substring/Sol1.java | 52 ++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 Hard/76.Minimum Window Substring/README.md create mode 100644 Hard/76.Minimum Window Substring/Sol1.java diff --git a/Hard/76.Minimum Window Substring/README.md b/Hard/76.Minimum Window Substring/README.md new file mode 100644 index 0000000..1b55703 --- /dev/null +++ b/Hard/76.Minimum Window Substring/README.md @@ -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/ diff --git a/Hard/76.Minimum Window Substring/Sol1.java b/Hard/76.Minimum Window Substring/Sol1.java new file mode 100644 index 0000000..08631ca --- /dev/null +++ b/Hard/76.Minimum Window Substring/Sol1.java @@ -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 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); + } +}