Finding the Minimum Window Substring! — 🚀Mastering Substring Search in JavaScript🌟

🎯 Uncover the magic of finding the minimum window substring in JavaScript. Level up your coding skills and conquer efficient string manipulation like never before! Let’s get started! 💡 #JavaScript #SubstringSearch #CodingMasterclass

Theodore John.S
4 min readJul 31, 2023

In this article, we will embark on an exciting journey to solve a challenging problem using JavaScript. Our goal is to find the minimum window in a string that contains all the characters of another given string. We will explore the logic behind this problem and provide you with two code snippets: a simple one and an optimized version that ensures excellent time and space complexity. Get ready to uncover the secrets of finding the minimum window substring!

Photo by Nathan Fertig on Unsplash

Logic:

To find the minimum window substring, we utilize a sliding window technique. We start with two pointers, left and right, that define the window boundaries. We expand the window by moving the right pointer to the right until we find a valid window that contains all the characters of the given string. Once we find such a window, we try to shrink it by moving the left pointer to the right. We continue this process until we can no longer shrink the window. The final window is the minimum window substring that contains all the characters.

Simple Code Snippet:

Let’s start by looking at a simple code snippet that embodies the logic of finding the minimum window substring:

function findMinimumWindow(s, t) {
const tCounts = {};
for (let char of t) {
tCounts[char] = (tCounts[char] || 0) + 1;
}
let left = 0;
let right = 0;
let minWindow = '';
let minWindowLength = Infinity;
let counter = t.length;
while (right < s.length) {
const char = s[right];
if (tCounts[char] > 0) {
counter--;
}
tCounts[char] = (tCounts[char] || 0) - 1;
right++;
while (counter === 0) {
if (right - left < minWindowLength) {
minWindowLength = right - left;
minWindow = s.slice(left, right);
}
const leftChar = s[left];
tCounts[leftChar]++;
if (tCounts[leftChar] > 0) {
counter++;
}
left++;
}
}
return minWindow;
}
// Usage
const s = 'ADOBECODEBANC';
const t = 'ABC';
const minimumWindow = findMinimumWindow(s, t);
console.log('Minimum Window:', minimumWindow);

Output:

Minimum Window: 'BANC'
  • In the provided simple code snippet, we define a function called findMinimumWindow that takes two strings, s and t, as input.
  • We initialize an object called tCounts to store the counts of characters in the target string t.
  • We also initialize left, right, minWindow, minWindowLength, and counter variables.
  • We iterate through the string s using the right pointer and check if the current character is in t. If it is, we decrement the counter and update the tCounts.
  • We then move the left pointer to shrink the window until the counter becomes non-zero.
  • We keep track of the minimum window and its length during this process. Finally, we return the minimum window substring.

Optimized Code Snippet:

Now, let’s delve into an optimized version of the code that ensures better time and space complexity:

function findMinimumWindowOptimized(s, t) {
const tCounts = new Map();
for (let char of t) {
tCounts.set(char, (tCounts.get(char) || 0) + 1);
}
let left = 0;
let right = 0;
let minWindow = '';
let minWindowLength = Infinity;
let counter = tCounts.size;
while (right < s.length) {
const char = s[right];
if (tCounts.has(char)) {
tCounts.set(char, tCounts.get(char) - 1);
if (tCounts.get(char) === 0) {
counter--;
}
}
right++;
while (counter === 0) {
if (right - left < minWindowLength) {
minWindowLength = right - left;
minWindow = s.slice(left, right);
}
const leftChar = s[left];
if (tCounts.has(leftChar)) {
tCounts.set(leftChar, tCounts.get(leftChar) + 1);
if (tCounts.get(leftChar) > 0) {
counter++;
}
}
left++;
}
}
return minWindow;
}
// Usage
const s = 'ADOBECODEBANC';
const t = 'ABC';
const minimumWindow = findMinimumWindowOptimized(s, t);
console.log('Minimum Window:', minimumWindow);

Output:

Minimum Window: 'BANC'

In the provided optimized code snippet, we use a Map data structure instead of an object to store the counts of characters in the target string t. This allows us to handle character counts more efficiently. We also optimize the condition checks inside the loops by utilizing Map methods like has() and get(). These optimizations improve both time and space complexity.

Summary:

In our quest to find the minimum window substring, we explored the logic behind this intriguing problem using JavaScript. We provided two code snippets: a simple version and an optimized version. The optimized version utilized a Map data structure and optimized the condition checks, resulting in improved time and space complexity. By utilizing these code snippets, you can now efficiently find the minimum window substring in a string, unveiling the secrets of substring searching. Unleash the power of finding the minimum window and navigate the world of efficient substring manipulation!

Hope the above article gave a better understanding. If you have any questions regarding the areas I have discussed in this article, areas of improvement don’t hesitate to comment below.

[Disclosure: This article is a collaborative creation blending my own ideation with the assistance of ChatGPT for optimal articulation.]

--

--

Theodore John.S

Passionate self-taught front-end dev. HTML, CSS, JS, React | Creating pixel-perfect web experiences |🌐Find me on LinkedIn: https://www.linkedin.com/in/stj/