Longest Nice Substring
Real-world version: Longest Matched-Case RunThe 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.
def longest_matched_run(line: str) -> str:public String longestMatchedRun(String line)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 18 test cases: 18 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Longest Substring with At Least K Repeating CharactersReal-world version: Longest Solid Stretch Of The Intake LogMedium
- Repeated DNA SequencesReal-world version: Ten-Letter Runs That Come Round AgainMedium
- Maximum Length Substring With Two OccurrencesReal-world version: Longest Twice-Only StretchEasy
- Find the DifferenceReal-world version: The Sort That Joined the TrayEasy