Leetcode 1763: Longest Nice Substring

grid47
grid47
Exploring patterns and algorithms
May 14, 2024 6 min read

A string is considered nice if every letter that appears in the string appears both in uppercase and lowercase. For example, ‘aA’ and ‘bB’ are nice strings, but ‘a’ and ‘B’ are not. Given a string s, find the longest nice substring of s. If there are multiple longest nice substrings, return the one that appears first. If no nice substring exists, return an empty string.
Problem
Approach
Steps
Complexity
Input: The input is a string s consisting of uppercase and lowercase English letters.
Example: Input: s = 'aAAbB'
Constraints:
• 1 <= s.length <= 100
• s consists of uppercase and lowercase English letters.
Output: Return the longest nice substring. If no such substring exists, return an empty string.
Example: Output: 'aA'
Constraints:
Goal: The goal is to find the longest substring where every letter appears in both its lowercase and uppercase form.
Steps:
• 1. Iterate through all possible substrings of the given string s.
• 2. For each substring, check if it contains every letter in both uppercase and lowercase.
• 3. Keep track of the longest valid substring found during the iteration.
Goal: The solution should efficiently handle strings of length up to 100.
Steps:
• The solution should perform efficiently even for the maximum length of the string (100 characters).
Assumptions:
• The string contains only alphabetic characters (both uppercase and lowercase).
• There are no special characters or spaces in the string.
Input: Input: s = 'YazaAay'
Explanation: The substring 'aAa' is a nice string because both 'A' and 'a' appear. 'aAa' is the longest nice substring.

Input: Input: s = 'Bb'
Explanation: The entire string 'Bb' is a nice string because both 'B' and 'b' appear.

Input: Input: s = 'c'
Explanation: There are no nice substrings because 'c' does not have a corresponding uppercase letter.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus