Smallest window substring that has both uppercase and corresponding lowercase characters
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Finding the smallest substring in a given string that contains both uppercase and lowercase versions of the same character is a practical string manipulation problem. It tests your understanding of the sliding window technique, character mapping, and efficient search strategies. This article walks through the problem definition, solution approach, and a working implementation.
Understanding the Problem
Given a string, find the smallest contiguous window (substring) such that for every letter in the window that appears in one case, the opposite case of that same letter also appears within the window.
For example, given the string aAbBcC, valid pairs include aA, bB, and cC. The smallest window containing at least one complete pair is aA (length 2). For the string abcABCabc, the smallest window containing all pairs aA, bB, cC would be cABC (length 4).
A key clarification: we only need to satisfy pairs for characters that actually appear in the window. The goal is to find a window where every letter present has both its cases represented.
Approach: Sliding Window with Hash Map
The sliding window technique is ideal here because we need a contiguous substring that satisfies a condition, and we want the minimum-length one.
Algorithm Steps
- Expand the window by moving the
endpointer to the right, adding characters to a frequency map. - Check validity: For every character in the current window, verify that both its uppercase and lowercase forms are present. Use ASCII arithmetic to toggle between cases. The difference between lowercase and uppercase for English letters is 32:
ord('a') - ord('A') = 32. - Contract the window by moving the
startpointer to the right while the window remains valid. Track the minimum window length. - Record the answer whenever a valid window is found that is smaller than the current best.
Implementation
This brute-force version runs in time where is the number of distinct characters in the window. For most practical inputs this is acceptable, but we can optimize further.
Optimized Sliding Window
This optimized version uses a true sliding window that avoids restarting from every position. The start pointer only moves forward, giving an amortized time complexity.
Worked Example
Consider the string "aASFabcdSgAB".
| Step | Window | Valid? | Reason |
Expand to aA | aA | Yes | Both a and A present |
| Contract from left | A | No | Missing lowercase a |
The first valid window found is aA with length 2. The algorithm continues scanning to see if any single-pair window of length 2 exists elsewhere, but aA at the start is already minimal.
For a string like "xXyYzZ", every two-character pair is valid, so the answer is length 2 (the first pair xX).
Edge Cases
- No valid window exists: If the string contains only uppercase or only lowercase letters, return an empty string.
- Single character: A single character can never form a valid pair.
- All identical pairs adjacent: The minimum window is always 2 in this case.
Time and Space Complexity
| Approach | Time | Space |
| Brute force | ||
| Sliding window |
Here is the string length and is the number of distinct characters in the current window (at most 52 for English letters).
Summary
The sliding window technique is the natural fit for this problem. By maintaining a frequency map and checking that every letter in the window has its case counterpart, you can efficiently find the smallest valid substring. The key insight is using swapcase() or ASCII arithmetic (add or subtract 32) to toggle between cases, and contracting the window from the left whenever the validity condition holds.
Related reading
- Smart pagination algorithm that works with local data cache
- Smart progress bar ETA computation
- Smoothing values over time moving average or something better?
- Solve all 4x4 mazes simultaneously with least moves
- Solving a graph issue with Python
- Solving a puzzle using search algorithms
- Solving linear equations represented as a string
- Solving N-Queens Problem... How far can we go?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.