🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
This blog post delves into a common problem in string manipulation: finding the smallest substring in a string s that contains all the characters of another string t, including duplicates. This problem is a classic example of the sliding window technique.
Problem
Given two strings s and t, find the smallest substring of s that has all the characters of t. If no such substring exists, return an empty string.
2. Solution Steps
1. Use a sliding window approach to go through s.
2. Keep track of the characters of t in a frequency map.
3. Expand the window until it contains all characters of t.
4. Once all characters are included, try to shrink the window from the left.
5. Keep track of the minimum length substring that satisfies the condition.
6. Return the smallest substring found, or an empty string if no such substring exists.
3. Code Program
import java.util.HashMap;
public class Solution {
// Main method for testing
public static void main(String[] args) {
System.out.println("Minimum window substring: " + minWindow("ADOBECODEBANC", "ABC"));
System.out.println("Minimum window substring: " + minWindow("a", "a"));
System.out.println("Minimum window substring: " + minWindow("a", "aa"));
}
// Method to find the minimum window substring
public static String minWindow(String s, String t) {
if (t.length() > s.length()) return "";
HashMap<Character, Integer> tMap = new HashMap<>();
for (char c : t.toCharArray()) {
tMap.put(c, tMap.getOrDefault(c, 0) + 1);
}
int required = tMap.size();
int formed = 0;
int left = 0, right = 0;
int minLength = Integer.MAX_VALUE, minLeft = 0;
HashMap<Character, Integer> windowCounts = new HashMap<>();
while (right < s.length()) {
char c = s.charAt(right);
windowCounts.put(c, windowCounts.getOrDefault(c, 0) + 1);
if (tMap.containsKey(c) && windowCounts.get(c).intValue() == tMap.get(c).intValue()) {
formed++;
}
// Try to contract the window till the point where it ceases to be 'desirable'
while (left <= right && formed == required) {
c = s.charAt(left);
// Save the smallest window until now
if (right - left + 1 < minLength) {
minLength = right - left + 1;
minLeft = left;
}
windowCounts.put(c, windowCounts.get(c) - 1);
if (tMap.containsKey(c) && windowCounts.get(c) < tMap.get(c)) {
formed--;
}
left++;
}
right++;
}
return minLength == Integer.MAX_VALUE ? "" : s.substring(minLeft, minLeft + minLength);
}
}
Output:
Minimum window substring: BANC Minimum window substring: a Minimum window substring:
Explanation:
The minWindow method applies the sliding window technique to find the smallest substring of s that contains all the characters of t.
For the input s = "ADOBECODEBANC" and t = "ABC", it identifies "BANC" as the smallest such substring. In the case of s = "a" and t = "a", the whole string "a" is the answer.
For s = "a" and t = "aa", no such substring exists, so it returns an empty string.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment