Trains the technique from
LeetCode 1763Longest Nice SubstringThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
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.
Example 1
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
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
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.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def longest_matched_run(line: str) -> str:public String longestMatchedRun(String line)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.