Zepruv › Coding problems

Longest Nice Substring

Easy · Hash Table, String, Divide and Conquer, Bit Manipulation, Sliding Window

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"

"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"

"Bb" is a nice string because both 'B' and 'b' appear. The whole string is a substring.

Example 3

Input: s = "c"
Output: ""

There are no nice substrings.

Constraints

Starter code (python)

class Solution(object):
    def longestNiceSubstring(self, s):
        """
        :type s: str
        :rtype: str
        """
        

Related problems

Previous: Can You Eat Your Favorite Candy on Your Favorite Day? · Next: Form Array by Concatenating Subarrays of Another Array · All problems

Zepruv is a technical interview practice platform. Read any problem for free; sign in to run your code, submit and keep your progress.