Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Longest Nice Substring (easy)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Given a string str, return the longest nice substring of a given string.

A substring is considered nice if for every lowercase letter in the substring, its uppercase counterpart is also present, and vice versa.

If no such string exists, return an empty string.

Examples

  1. Example 1:

    • Input: "BbCcXxY"
    • Expected Output: "BbCcXx"
    • Justification: Here, "BbCcXx" is the longest substring where each letter's uppercase and lowercase forms are present.
  2. Example 2:

    • Input: "aZAbcD"
    • Expected Output: ""
    • Justification: There is no contiguous substring where each character exists in both its uppercase and lowercase forms.
  3. Example 3:

    • Input: "qQwWeErR"
    • Expected Output: "qQwWeErR"
    • Justification: The entire string is the longest nice substring since every letter exists in both uppercase and lowercase forms.

Constraints:

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

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible