1763. Longest Nice Substring LeetCode Solution
In this guide, you will get 1763. Longest Nice Substring LeetCode Solution with the best time and space complexity. The solution to Longest Nice Substring problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Longest Nice Substring solution in C++
- Longest Nice Substring solution in Java
- Longest Nice Substring solution in Python
- Additional Resources

Problem Statement of Longest Nice Substring
A string s is nice if, for every letter of the alphabet that s contains, it appears both in uppercase and lowercase. For example, “abABB” is nice because ‘A’ and ‘a’ appear, and ‘B’ and ‘b’ appear. However, “abA” is not because ‘b’ appears, but ‘B’ does not.
Given a string s, return the longest substring of s that is nice. If there are multiple, return the substring of the earliest occurrence. If there are none, return an empty string.
Example 1:
Input: s = “YazaAay”
Output: “aAa”
Explanation: “aAa” is a nice string because ‘A/a’ is the only letter of the alphabet in s, and both ‘A’ and ‘a’ appear.
“aAa” is the longest nice substring.
Example 2:
Input: s = “Bb”
Output: “Bb”
Explanation: “Bb” is a nice string because both ‘B’ and ‘b’ appear. The whole string is a substring.
Example 3:
Input: s = “c”
Output: “”
Explanation: There are no nice substrings.
Constraints:
1 <= s.length <= 100
s consists of uppercase and lowercase English letters.
Complexity Analysis
- Time Complexity: O(n\log n) \to O(n^2)
- Space Complexity: O(n\log n) \to O(n^2)
1763. Longest Nice Substring LeetCode Solution in C++
class Solution {
public:
string longestNiceSubstring(string s) {
if (s.length() < 2)
return "";
unordered_set<char> seen{s.begin(), s.end()};
for (int i = 0; i < s.size(); ++i)
// If both upper and lower case letters exists in the string, keep moving,
// else take the erroneous character as a partition and check for its left
// and right parts to be nice strings.
if (!seen.contains(toggleCase(s[i]))) {
const string prefix = longestNiceSubstring(s.substr(0, i));
const string suffix = longestNiceSubstring(s.substr(i + 1));
return prefix.length() >= suffix.length() ? prefix : suffix;
}
return s;
}
private:
char toggleCase(char c) {
return islower(c) ? toupper(c) : tolower(c);
}
};
/* code provided by PROGIEZ */
1763. Longest Nice Substring LeetCode Solution in Java
class Solution {
public String longestNiceSubstring(String s) {
if (s.length() < 2)
return "";
Set<Character> seen = s.chars().mapToObj(c -> (char) c).collect(Collectors.toSet());
for (int i = 0; i < s.length(); ++i)
if (!seen.contains(toggleCase(s.charAt(i)))) {
final String prefix = longestNiceSubstring(s.substring(0, i));
final String suffix = longestNiceSubstring(s.substring(i + 1));
return prefix.length() >= suffix.length() ? prefix : suffix;
}
return s;
}
private char toggleCase(char c) {
return Character.isLowerCase(c) ? Character.toUpperCase(c) : Character.toLowerCase(c);
}
}
// code provided by PROGIEZ
1763. Longest Nice Substring LeetCode Solution in Python
class Solution:
def longestNiceSubstring(self, s: str) -> str:
if len(s) < 2:
return ''
seen = set(s)
for i, c in enumerate(s):
# If both upper and lower case letters exists in the string, keep moving,
# else take the erroneous character as a partition and check for its left
# and right parts to be nice strings.
if c.swapcase() not in seen:
prefix = self.longestNiceSubstring(s[:i])
suffix = self.longestNiceSubstring(s[i + 1:])
return max(prefix, suffix, key=len)
return s
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.