Skip to content

Add sliding-window cases to longest-substring - #858

Open
ye144440154 wants to merge 1 commit into
sysprog21:mainfrom
ye144440154:distinct-run-repeat-case
Open

ye144440154 wants to merge 1 commit into
sysprog21:mainfrom
ye144440154:distinct-run-repeat-case

Conversation

@ye144440154

@ye144440154 ye144440154 commented Nov 25, 2026 •

Copy link
Copy Markdown

Problem and behavior

In Distinct Keystroke Stretch (longest-substring-without-repeating-characters), eleven wrong solutions passed all five cases:

  • clearing the stretch at a repeat and starting again at the repeated key;
  • moving the left edge to the repeat itself instead of one past its earlier copy;
  • moving the left edge one key per repeat (if where while belongs);
  • measuring only the stretch from the first key and the one ending at the last key;
  • starting the left edge at 0 instead of -1 in the last-position form, so a stretch from the first key comes out one short;
  • measuring a stretch only when a repeat ends it, so the last one is never counted;
  • lowercasing s first;
  • dropping digits, symbols and spaces;
  • dropping symbols only;
  • trimming the spaces at the ends;
  • a last-seen table of 26 slots that skips every key outside a to z.

The first two lose the v between the two ds of "dvdf" and return 2 instead of 3. The third returns 4 on "abccad", whose longest stretch is "cad", 3. The fourth passed because every case has a longest stretch touching an end of the string; on "aabcdaa" it returns 1 instead of 4 ("abcd"). The fifth passed because no case has its only longest stretch at the start; on "abc" it returns 2 instead of 3, and the sixth returns 0. The last two passed because every input was lowercase letters, while the contract compares case-sensitively and counts every character; on "aA 1" they return 3 and 2 instead of 4. The last three passed because no input had a space at an end or a repeated key outside a to z; on " A!1 " they return 3, 3 and 5 instead of 4. With this change all eleven fail, and a correct solution passes all eleven cases.

Approach

Six cases join the judge:

Label Input Expected Rejects
repeat in the middle of the stretch "dvdf" 3 the first two solutions
left edge jumps more than one key "abccad" 3 the third
longest stretch away from both ends "aabcdaa" 4 the fourth
every key distinct "abc" 3 the fifth and sixth
case and symbols count "aA 1" 4 the seventh and eighth
space before and after the keys " A!1 " 4 the ninth to eleventh

The inputs are inside the stated constraints (0 <= s.length <= 5 * 10^4, letters, digits, symbols and spaces). Like the other labels of this judge, the new ones describe the case without quoting the input, which the results panel shows only when the case fails. The labels pass check_labels in scripts/problem_bank/rules.py and join the tricky-case list in tests/web/contract.rs. That list matches each entry against the judge's labels joined together, so the existing left edge entry, which left edge jumps more than one key would now satisfy on its own, is spelled out as left edge must not move backward; removing the "abba" case fails the check again. python3 scripts/gen-problems.py regenerated web/judges/distinct-keystroke-stretch.json, and the longest-substring-without-repeating-characters digest in tests/golden/task-posed.json is recomputed the way tests/test_task_package.py computes it.

A test in tests/browser/compiler-explorer.test.js, next to the remove-element one, checks every expected value against a sliding window and runs each solution by name against its case through assertRejects, which fits here because the answer is a number.

Validation

  • node --test --test-name-pattern=longest-substring tests/browser/compiler-explorer.test.js: passes at this head. It fails on a missing pinned label when any of the five new cases is removed, and it fails, naming the solution, when the "abccad", "aabcdaa", "abc", "aA 1" or " A!1 " input is replaced with one that solution passes ("ab c1" for lowercasing, "aAbc" for dropping non-letters, "abca", " A1 " and "A!1 A!" for the 26-slot table, dropping symbols and trimming).
  • Each of the eleven solutions fails only the cases the table gives it (the fifth also fails case and symbols count, the sixth repeat in the middle of the stretch and case and symbols count). Five nearby mistakes also fail at least one case: moving the left edge without checking it stays ahead, counting the distinct keys of the whole string, resetting the count only at adjacent repeats, uppercasing s first, and dropping only spaces. A correct solution passes all eleven.
  • python3 scripts/gen-problems.py --check: clean.
  • cargo test --test web: 230 passed, 0 failed, 2 ignored.
  • ./scripts/test.sh at 7e238717, on 07f1d007, on Ubuntu 22.04 under WSL2, rustc 1.100.0, Node 26.13.1, Clang 23 from scripts/fetch-clang.sh: every gate passes. Not checked by that run: desktop clippy and tests (no libwebkit2gtk-4.1-dev).
  • Not run locally: the C, C++ and Java harnesses on the new cases through Compiler Explorer.

Close #857

cubic-dev-ai[bot]

This comment was marked as resolved.

@ye144440154
ye144440154 force-pushed the distinct-run-repeat-case branch from ea98694 to b695822 Compare November 25, 2026 16:44
jserv

This comment was marked as resolved.

@sakinu sakinu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new case rejects both shortcuts the description names, and its expected value checks out. Requesting changes because the judge still accepts wrong answers: the open thread on the test file shows one, and the comment on problem-bank/judges.json shows a second that only a case with its longest stretch away from both ends can catch.

Comment thread problem-bank/judges.json Resolved
@jserv

jserv commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Some review comments on this pull request are not settled yet. Please refine the change per the review comments listed below, reply on each thread with what you changed or why you did not, and mark each thread resolved yourself once it is settled (Resolve conversation, at the bottom of the thread). The pull request stays blocked while review threads are open.

Not addressed at the current head:

Checked at b695822.

@ye144440154
ye144440154 force-pushed the distinct-run-repeat-case branch from b695822 to 3c06811 Compare November 26, 2026 03:02
jserv

This comment was marked as resolved.

jserv

This comment was marked as resolved.

jserv

This comment was marked as resolved.

jserv

This comment was marked as resolved.

@ye144440154
ye144440154 force-pushed the distinct-run-repeat-case branch from 3c06811 to 1605c3e Compare November 26, 2026 03:44
@ye144440154 ye144440154 changed the title Add a mid-stretch repeat case to longest-substring Add sliding-window cases to longest-substring Nov 26, 2026
jserv

This comment was marked as resolved.

jserv

This comment was marked as resolved.

jserv

This comment was marked as outdated.

@ye144440154
ye144440154 force-pushed the distinct-run-repeat-case branch from 1605c3e to abac7a7 Compare November 26, 2026 09:51
jserv

This comment was marked as resolved.

Eleven wrong solutions passed every Distinct Keystroke Stretch case. Two
drop the whole stretch at a repeat: one clears it and starts again at
the repeated key, the other moves the left edge to the repeat itself
instead of one past its earlier copy. On "dvdf" both lose the v between
the two d's and return 2 instead of 3. A third moves the left edge one
key per repeat, with if where while belongs, and returns 4 on "abccad",
whose longest stretch is 3. A fourth measures only the stretch from the
first key and the one ending at the last key; every case has a longest
stretch touching an end, so it passed, and it returns 1 on "aabcdaa",
whose longest stretch is 4. A fifth starts its edge at 0 instead of -1,
so a stretch from the first key comes out one short; it passed because
no case has its only longest stretch at the start, and it returns 2 on
"abc". A sixth measures a stretch only when a repeat ends it, so the
last one is never counted, and returns 0 on "abc". Every input was
lowercase letters, so lowercasing s first, or dropping digits, symbols
and spaces, passed too, though the contract compares case-sensitively
and counts every character; on "aA 1" they return 3 and 2 instead of 4.
No input had a space at an end or a repeated key outside a to z, so
trimming s, dropping symbols, or a last-seen table of 26 slots that
skips every other key passed as well; on " A!1 " they return 3, 3 and 5
instead of 4. The six inputs become new cases. A test in
tests/browser/compiler-explorer.test.js runs each solution against its
case, looked up by label, and fails against the cases on main; the
labels join the tricky-case list in tests/web/contract.rs so the cases
stay. The existing left-edge entry there now names its whole label,
which the new left-edge label would otherwise satisfy, and the task
preview golden takes the judge's new hash.

Close sysprog21#857

Change-Id: Ie15579e63be1b975312cea211e0773647322d815
@ye144440154
ye144440154 force-pushed the distinct-run-repeat-case branch from abac7a7 to 7e23871 Compare November 26, 2026 11:01
Comment thread problem-bank/judges.json
{
"label": "space before and after the keys",
"input": [
" A!1 "

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trimming only one end still passes all eleven cases: " A!1 " has two longest stretches, " A!1" and "A!1 ", each holding one of the two spaces, so s.rstrip() or s.lstrip() in Python (trimEnd() or trimStart() in JavaScript) leaves a stretch of 4 and only trimming both ends returns 3. Since the space repeats, no single input can have its only longest stretch hold both end spaces, so this takes a case for each side: " abca" expecting 4 rejects trimming the start (3), and "abca " expecting 4 rejects trimming the end (3). Each would need a pin in tests/web/contract.rs and a one-sided trim added to the shortcuts in the browser test, the way trimmed is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Problem "Distinct Keystroke Stretch" accepts solutions that restart the stretch at a repeated key

3 participants