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
Inputs = "YazaAay"
Output"aAa"
"aAa" is nice because 'A/a' is the only letter of the alphabet in it, both 'A' and 'a' appear, and it is the longest nice substring.
Example 2
Inputs = "Bb"
Output"Bb"
"Bb" is nice because both 'B' and 'b' appear, so the whole string is a nice substring.

Constraints

  • 1 <= s.length <= 100
  • s consists of uppercase and lowercase English letters.

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate