Minimum length substring with each letter occurring both in uppercase and lowercase
Last Updated :
23 Jul, 2025
Find the minimum length substring in the given string str such that, in the substring, each alphabet appears at least once in lower case and at least once in upper case.
Examples:
Input: S = “AaBbCc”
Output: Aa
Explanation: Possible substrings that has each alphabet in lowercase and uppercase are:
Aa
Bb
Cc
AaBb
BbCc
AaBbCc
Among these, the minimum length substrings are Aa, Bb and Cc. Hence any of them can be a possible answer.
Input: S = "Geeks"
Output: -1
Explanation: No such substring present.
Naive Approach: The most straightforward approach is to generate all possible substrings of the given string and check if there exists any substring satisfying the given conditions. Print the smallest of all such substrings.
Time Complexity: O(N3)
Auxiliary Space: O(N)
Efficient Approach: To optimize the above approach, the idea is to use the concept of Sliding Window. Follow the steps below to solve the problem:
- Traverse the given string and store the characters whose only lowercase or uppercase form are present in the input string in a Map mp.
- Initialize two arrays to keep track of the lowercase and uppercase characters obtained so far.
- Now, traverse the string maintaining two pointers i and st (initialized with 0), where st will point to the start of the current substring and i will point to the current character.
- If the current character is in mp, neglect all the characters obtained so far and start from the next character and adjust the arrays accordingly.
- If the current character is not in mp, remove the extra characters from the beginning of the substring with the help of st pointer, such that the frequency of any character does not convert to 0 and adjust the arrays accordingly.
- Now, check whether the substring {S[st], ....., S[i]} is balanced or not. If balanced and i - st + 1 is smaller than the length of balanced substring obtained so far. Update the length and also store the start and end indices of the substring, i.e. st and i respectively.
- Repeat the steps till the end of the string.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check if the current
// string is balanced or not
bool balanced(int small[], int caps[])
{
// For every character, check if
// there exists uppercase as well
// as lowercase characters
for (int i = 0; i < 26; i++) {
if (small[i] != 0 && (caps[i] == 0))
return 0;
else if ((small[i] == 0) && (caps[i] != 0))
return 0;
}
return 1;
}
// Function to find smallest length substring
// in the given string which is balanced
void smallestBalancedSubstring(string s)
{
// Store frequency of
// lowercase characters
int small[26];
// Stores frequency of
// uppercase characters
int caps[26];
memset(small, 0, sizeof(small));
memset(caps, 0, sizeof(caps));
// Count frequency of characters
for (int i = 0; i < s.length(); i++) {
if (s[i] >= 65 && s[i] <= 90)
caps[s[i] - 'A']++;
else
small[s[i] - 'a']++;
}
// Mark those characters which
// are not present in both
// lowercase and uppercase
unordered_map<char, int> mp;
for (int i = 0; i < 26; i++) {
if (small[i] && !caps[i])
mp[char(i + 'a')] = 1;
else if (caps[i] && !small[i])
mp[char(i + 'A')] = 1;
}
// Initialize the frequencies
// back to 0
memset(small, 0, sizeof(small));
memset(caps, 0, sizeof(caps));
// Marks the start and
// end of current substring
int i = 0, st = 0;
// Marks the start and end
// of required substring
int start = -1, end = -1;
// Stores the length of
// smallest balanced substring
int minm = INT_MAX;
while (i < s.length()) {
if (mp[s[i]]) {
// Remove all characters
// obtained so far
while (st < i) {
if (s[st] >= 65 && s[st] <= 90)
caps[s[st] - 'A']--;
else
small[s[st] - 'a']--;
st++;
}
i += 1;
st = i;
}
else {
if (s[i] >= 65 && s[i] <= 90)
caps[s[i] - 'A']++;
else
small[s[i] - 'a']++;
// Remove extra characters from
// front of the current substring
while (1) {
if (s[st] >= 65 && s[st] <= 90
&& caps[s[st] - 'A'] > 1) {
caps[s[st] - 'A']--;
st++;
}
else if (s[st] >= 97 && s[st] <= 122
&& small[s[st] - 'a'] > 1) {
small[s[st] - 'a']--;
st++;
}
else
break;
}
// If substring (st, i) is balanced
if (balanced(small, caps)) {
if (minm > (i - st + 1)) {
minm = i - st + 1;
start = st;
end = i;
}
}
i += 1;
}
}
// No balanced substring
if (start == -1 || end == -1)
cout << -1 << endl;
// Store answer string
else {
string ans = "";
for (int i = start; i <= end; i++)
ans += s[i];
cout << ans << endl;
}
}
// Driver Code
int main()
{
// Given string
string s = "azABaabba";
smallestBalancedSubstring(s);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG{
// Function to check if the current
// string is balanced or not
static boolean balanced(int small[],
int caps[])
{
// For every character, check if
// there exists uppercase as well
// as lowercase characters
for(int i = 0; i < 26; i++)
{
if (small[i] != 0 && (caps[i] == 0))
return false;
else if ((small[i] == 0) && (caps[i] != 0))
return false;
}
return true;
}
// Function to find smallest length substring
// in the given string which is balanced
static void smallestBalancedSubstring(String s)
{
// Store frequency of
// lowercase characters
int[] small = new int[26];
// Stores frequency of
// uppercase characters
int[] caps = new int[26];
Arrays.fill(small, 0);
Arrays.fill(caps, 0);
// Count frequency of characters
for(int i = 0; i < s.length(); i++)
{
if (s.charAt(i) >= 65 && s.charAt(i) <= 90)
caps[s.charAt(i) - 'A']++;
else
small[s.charAt(i) - 'a']++;
}
// Mark those characters which
// are not present in both
// lowercase and uppercase
Map<Character,
Integer> mp = new HashMap<Character,
Integer>();
for(int i = 0; i < 26; i++)
{
if (small[i] != 0 && caps[i] == 0)
mp.put((char)(i + 'a'), 1);
else if (caps[i] != 0 && small[i] == 0)
mp.put((char)(i + 'A'), 1);
// mp[char(i + 'A')] = 1;
}
// Initialize the frequencies
// back to 0
Arrays.fill(small, 0);
Arrays.fill(caps, 0);
// Marks the start and
// end of current substring
int i = 0, st = 0;
// Marks the start and end
// of required substring
int start = -1, end = -1;
// Stores the length of
// smallest balanced substring
int minm = Integer.MAX_VALUE;
while (i < s.length())
{
if (mp.get(s.charAt(i)) != null)
{
// Remove all characters
// obtained so far
while (st < i)
{
if (s.charAt(st) >= 65 &&
s.charAt(st) <= 90)
caps[s.charAt(st) - 'A']--;
else
small[s.charAt(st) - 'a']--;
st++;
}
i += 1;
st = i;
}
else
{
if (s.charAt(i) >= 65 && s.charAt(i) <= 90)
caps[s.charAt(i) - 'A']++;
else
small[s.charAt(i) - 'a']++;
// Remove extra characters from
// front of the current substring
while (true)
{
if (s.charAt(st) >= 65 &&
s.charAt(st) <= 90 &&
caps[s.charAt(st) - 'A'] > 1)
{
caps[s.charAt(st) - 'A']--;
st++;
}
else if (s.charAt(st) >= 97 &&
s.charAt(st) <= 122 &&
small[s.charAt(st) - 'a'] > 1)
{
small[s.charAt(st) - 'a']--;
st++;
}
else
break;
}
// If substring (st, i) is balanced
if (balanced(small, caps))
{
if (minm > (i - st + 1))
{
minm = i - st + 1;
start = st;
end = i;
}
}
i += 1;
}
}
// No balanced substring
if (start == -1 || end == -1)
System.out.println(-1);
// Store answer string
else
{
String ans = "";
for(int j = start; j <= end; j++)
ans += s.charAt(j);
System.out.println(ans);
}
}
// Driver Code
public static void main(String[] args)
{
// Given string
String s = "azABaabba";
smallestBalancedSubstring(s);
}
}
// This code is contributed by Dharanendra L V
Python3
# python 3 program for the above approach
import sys
# Function to check if the current
# string is balanced or not
def balanced(small, caps):
# For every character, check if
# there exists uppercase as well
# as lowercase characters
for i in range(26):
if (small[i] != 0 and (caps[i] == 0)):
return 0
elif((small[i] == 0) and (caps[i] != 0)):
return 0
return 1
# Function to find smallest length substring
# in the given string which is balanced
def smallestBalancedSubstring(s):
# Store frequency of
# lowercase characters
small = [0 for i in range(26)]
# Stores frequency of
# uppercase characters
caps = [0 for i in range(26)]
# Count frequency of characters
for i in range(len(s)):
if (ord(s[i]) >= 65 and ord(s[i]) <= 90):
caps[ord(s[i]) - 65] += 1
else:
small[ord(s[i]) - 97] += 1
# Mark those characters which
# are not present in both
# lowercase and uppercase
mp = {}
for i in range(26):
if (small[i] and caps[i]==0):
mp[chr(i + 97)] = 1
elif (caps[i] and small[i]==0):
mp[chr(i + 65)] = 1
# Initialize the frequencies
# back to 0
for i in range(len(small)):
small[i] = 0
caps[i] = 0
# Marks the start and
# end of current substring
i = 0
st = 0
# Marks the start and end
# of required substring
start = -1
end = -1
# Stores the length of
# smallest balanced substring
minm = sys.maxsize
while (i < len(s)):
if(s[i] in mp):
# Remove all characters
# obtained so far
while (st < i):
if (ord(s[st]) >= 65 and ord(s[st]) <= 90):
caps[ord(s[st]) - 65] -= 1
else:
small[ord(s[st]) - 97] -= 1
st += 1
i += 1
st = i
else:
if (ord(s[i]) >= 65 and ord(s[i]) <= 90):
caps[ord(s[i]) - 65] += 1
else:
small[ord(s[i] )- 97] += 1
# Remove extra characters from
# front of the current substring
while (1):
if (ord(s[st]) >= 65 and ord(s[st])<= 90 and caps[ord(s[st])- 65] > 1):
caps[ord(s[st]) - 65] -= 1
st += 1
elif (ord(s[st]) >= 97 and ord(s[st]) <= 122 and small[ord(s[st]) - 97] > 1):
small[ord(s[st]) - 97] -= 1
st += 1
else:
break
# If substring (st, i) is balanced
if (balanced(small, caps)):
if (minm > (i - st + 1)):
minm = i - st + 1
start = st
end = i
i += 1
# No balanced substring
if (start == -1 or end == -1):
print(-1)
# Store answer string
else:
ans = ""
for i in range(start,end+1,1):
ans += s[i]
print(ans)
# Driver Code
if __name__ == '__main__':
# Given string
s = "azABaabba"
smallestBalancedSubstring(s)
# This code is contributed by bgangwar59.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
public const int MaxValue = 2147483647;
// Function to check if the current
// string is balanced or not
static bool balanced(int []small,
int []caps)
{
// For every character, check if
// there exists uppercase as well
// as lowercase characters
for(int i = 0; i < 26; i++)
{
if (small[i] != 0 && (caps[i] == 0))
return false;
else if ((small[i] == 0) && (caps[i] != 0))
return false;
}
return true;
}
// Function to find smallest length substring
// in the given string which is balanced
static void smallestBalancedSubstring(string s)
{
// Store frequency of
// lowercase characters
int[] small = new int[26];
int i;
// Stores frequency of
// uppercase characters
int[] caps = new int[26];
Array.Clear(small, 0, small.Length);
Array.Clear(caps, 0, caps.Length);
// Count frequency of characters
for(i = 0; i < s.Length; i++)
{
if (s[i] >= 65 && s[i] <= 90)
caps[(int)s[i] - 65]++;
else
small[(int)s[i]- 97]++;
}
// Mark those characters which
// are not present in both
// lowercase and uppercase
Dictionary<char,int> mp = new Dictionary<char,int>();
for(i = 0; i < 26; i++)
{
if (small[i] != 0 && caps[i] == 0){
mp[(char)(i+97)] = 1;
}
else if (caps[i] != 0 && small[i] == 0)
mp[(char)(i+65)] = 1;
// mp[char(i + 'A')] = 1;
}
// Initialize the frequencies
// back to 0
Array.Clear(small, 0, small.Length);
Array.Clear(caps, 0, caps.Length);
// Marks the start and
// end of current substring
i = 0;
int st = 0;
// Marks the start and end
// of required substring
int start = -1, end = -1;
// Stores the length of
// smallest balanced substring
int minm = MaxValue;
while (i < s.Length)
{
if (mp.ContainsKey(s[i]))
{
// Remove all characters
// obtained so far
while (st < i)
{
if ((int)s[st] >= 65 &&
(int)s[st] <= 90)
caps[(int)s[st] - 65]--;
else
small[(int)s[st] - 97]--;
st++;
}
i += 1;
st = i;
}
else
{
if ((int)s[i] >= 65 && (int)s[i] <= 90)
caps[(int)s[i] - 65]++;
else
small[(int)s[i] - 97]++;
// Remove extra characters from
// front of the current substring
while (true)
{
if ((int)s[st] >= 65 &&
(int)s[st] <= 90 &&
caps[(int)s[st] - 65] > 1)
{
caps[(int)s[st] - 65]--;
st++;
}
else if ((int)s[st] >= 97 &&
(int)s[st] <= 122 &&
small[(int)s[st] - 97] > 1)
{
small[(int)s[st] - 97]--;
st++;
}
else
break;
}
// If substring (st, i) is balanced
if (balanced(small, caps))
{
if (minm > (i - st + 1))
{
minm = i - st + 1;
start = st;
end = i;
}
}
i += 1;
}
}
// No balanced substring
if (start == -1 || end == -1)
Console.WriteLine(-1);
// Store answer string
else
{
string ans = "";
for(int j = start; j <= end; j++)
ans += s[j];
Console.WriteLine(ans);
}
}
// Driver Code
public static void Main()
{
// Given string
string s = "azABaabba";
smallestBalancedSubstring(s);
}
}
// This code is contributed by SURENDRA_GANGWAR.
JavaScript
<script>
// Javascript program for the above approach
let MaxValue = 2147483647;
// Function to check if the current
// string is balanced or not
function balanced(small, caps)
{
// For every character, check if
// there exists uppercase as well
// as lowercase characters
for(let i = 0; i < 26; i++)
{
if (small[i] != 0 && (caps[i] == 0))
return false;
else if ((small[i] == 0) && (caps[i] != 0))
return false;
}
return true;
}
// Function to find smallest length substring
// in the given string which is balanced
function smallestBalancedSubstring(s)
{
// Store frequency of
// lowercase characters
let small = new Array(26);
let i;
// Stores frequency of
// uppercase characters
let caps = new Array(26);
small.fill(0);
caps.fill(0);
// Count frequency of characters
for(i = 0; i < s.length; i++)
{
if (s[i].charCodeAt() >= 65 && s[i].charCodeAt() <= 90)
caps[s[i].charCodeAt() - 65]++;
else
small[s[i].charCodeAt()- 97]++;
}
// Mark those characters which
// are not present in both
// lowercase and uppercase
let mp = new Map();
for(i = 0; i < 26; i++)
{
if (small[i] != 0 && caps[i] == 0){
mp.set(String.fromCharCode(i+97), 1);
}
else if (caps[i] != 0 && small[i] == 0)
mp.set(String.fromCharCode(i+65), 1);
// mp[char(i + 'A')] = 1;
}
// Initialize the frequencies
// back to 0
small.fill(0);
caps.fill(0);
// Marks the start and
// end of current substring
i = 0;
let st = 0;
// Marks the start and end
// of required substring
let start = -1, end = -1;
// Stores the length of
// smallest balanced substring
let minm = MaxValue;
while (i < s.length)
{
if (mp.has(s[i]))
{
// Remove all characters
// obtained so far
while (st < i)
{
if (s[st].charCodeAt() >= 65 &&
s[st].charCodeAt() <= 90)
caps[s[st].charCodeAt() - 65]--;
else
small[s[st].charCodeAt() - 97]--;
st++;
}
i += 1;
st = i;
}
else
{
if (s[i].charCodeAt() >= 65 && s[i].charCodeAt() <= 90)
caps[s[i].charCodeAt() - 65]++;
else
small[s[i].charCodeAt() - 97]++;
// Remove extra characters from
// front of the current substring
while (true)
{
if (s[st].charCodeAt() >= 65 &&
s[st].charCodeAt() <= 90 &&
caps[s[st].charCodeAt() - 65] > 1)
{
caps[s[st].charCodeAt() - 65]--;
st++;
}
else if (s[st].charCodeAt() >= 97 &&
s[st].charCodeAt() <= 122 &&
small[s[st].charCodeAt() - 97] > 1)
{
small[s[st].charCodeAt() - 97]--;
st++;
}
else
break;
}
// If substring (st, i) is balanced
if (balanced(small, caps))
{
if (minm > (i - st + 1))
{
minm = i - st + 1;
start = st;
end = i;
}
}
i += 1;
}
}
// No balanced substring
if (start == -1 || end == -1)
document.write(-1 + "</br>");
// Store answer string
else
{
let ans = "";
for(let j = start; j <= end; j++)
ans += s[j];
document.write(ans + "</br>");
}
}
// Given string
let s = "azABaabba";
smallestBalancedSubstring(s);
// This code is contributed by decode2207.
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem