Show problems as
Classic problem #1763EasyHash TableStringDivide and ConquerBit ManipulationSliding Window

Longest Nice Substring

Real-world version: Longest Matched-Case Run
Tracked in this browser only
Read Longest Nice Substring on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A proofreading tool reads one line of English letters, given as line. Upper and lower case both appear and the two cases of a letter are treated as different characters.

A contiguous stretch of the line is matched when, for every letter that occurs in that stretch, the stretch contains that letter in lowercase form and also in uppercase form. For example, a stretch holding d, D and nothing else is matched, while a stretch holding d alone is not.

Return the longest matched stretch of line. If several matched stretches share the greatest length, return the one that begins at the smallest index. If the line has no matched stretch at all, return the empty string.

Examples

Example 1

Input
line = "pQqPz"
Output
"pQqP"

The stretch `pQqP` covering indices 0 through 3 uses the letters p and q, and it holds both `p` and `P` as well as both `q` and `Q`, so it is matched and 4 characters long.

Example 2

Input
line = "aAwwbBcC"
Output
"bBcC"

The stretch `bBcC` covering indices 4 through 7 uses the letters b and c, each present in both cases, so it is matched with a length of 4.

Example 3

Input
line = "nnn"
Output
""

The only letter anywhere in the line is n and it never appears in uppercase, so no stretch is matched and the answer is the empty string.

Constraints

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

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def longest_matched_run(line: str) -> str:
Java
public String longestMatchedRun(String line)