From 0e3ce2ab0b7cea2bb229a2edd5414cb181100810 Mon Sep 17 00:00:00 2001 From: ye144440154 <80632267+ye144440154@users.noreply.github.com> Date: Wed, 25 Nov 2026 23:31:19 +0800 Subject: [PATCH] Add sliding-window cases to longest-substring Thirteen 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. That input has a longest stretch on each side, so trimming only the start or only the end still passed; on " abca" and "abca " they return 3 instead of 4. The eight 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 #857 Change-Id: I1e3728d771aeb5344e56c036ea4151849cb9cec3 --- problem-bank/judges.json | 56 ++++++++ tests/browser/compiler-explorer.test.js | 154 +++++++++++++++++++++ tests/golden/task-posed.json | 2 +- tests/web/contract.rs | 34 ++++- web/judges/distinct-keystroke-stretch.json | 56 ++++++++ 5 files changed, 300 insertions(+), 2 deletions(-) diff --git a/problem-bank/judges.json b/problem-bank/judges.json index 2af030fff..640b0f79c 100644 --- a/problem-bank/judges.json +++ b/problem-bank/judges.json @@ -2636,6 +2636,62 @@ "" ], "expected": 0 + }, + { + "label": "repeat in the middle of the stretch", + "input": [ + "dvdf" + ], + "expected": 3 + }, + { + "label": "left edge jumps more than one key", + "input": [ + "abccad" + ], + "expected": 3 + }, + { + "label": "longest stretch away from both ends", + "input": [ + "aabcdaa" + ], + "expected": 4 + }, + { + "label": "every key distinct", + "input": [ + "abc" + ], + "expected": 3 + }, + { + "label": "case and symbols count", + "input": [ + "aA 1" + ], + "expected": 4 + }, + { + "label": "space before and after the keys", + "input": [ + " A!1 " + ], + "expected": 4 + }, + { + "label": "leading space only", + "input": [ + " abca" + ], + "expected": 4 + }, + { + "label": "trailing space only", + "input": [ + "abca " + ], + "expected": 4 } ], "paramNames": [ diff --git a/tests/browser/compiler-explorer.test.js b/tests/browser/compiler-explorer.test.js index 23d6a54bb..53ed6eff2 100644 --- a/tests/browser/compiler-explorer.test.js +++ b/tests/browser/compiler-explorer.test.js @@ -727,6 +727,160 @@ test("remove-element cases reject skipping a last unchecked target", () => { } }); +test("longest-substring cases reject shortcuts in moving the stretch", () => { + const problem = "longest-substring-without-repeating-characters"; + const longest = (s) => { + const last = new Map(); + let best = 0; + let left = 0; + [...s].forEach((key, i) => { + if (last.has(key) && last.get(key) >= left) left = last.get(key) + 1; + last.set(key, i); + best = Math.max(best, i - left + 1); + }); + return best; + }; + for (const { label, input, expected } of judges[problem].cases) { + assert.equal( + longest(...input), + expected, + `"${label}" must expect the longest stretch`, + ); + } + // Both keep only the repeated key, so a key between its two copies is lost. + const restart = (s) => { + const seen = new Set(); + let best = 0; + for (const key of s) { + if (seen.has(key)) seen.clear(); + seen.add(key); + best = Math.max(best, seen.size); + } + return best; + }; + const leftAtRepeat = (s) => { + const last = new Map(); + let best = 0; + let left = 0; + [...s].forEach((key, i) => { + if (last.has(key) && last.get(key) >= left) left = i; + last.set(key, i); + best = Math.max(best, i - left + 1); + }); + return best; + }; + // `if` where `while` belongs: the left edge drops one key per repeat. + const oneStep = (s) => { + const seen = new Set(); + let best = 0; + let left = 0; + for (const key of s) { + if (seen.has(key)) seen.delete(s[left++]); + seen.add(key); + best = Math.max(best, seen.size); + } + return best; + }; + // Measures only the stretch from the first key and the one to the last. + const endsOnly = (s) => { + const run = (keys) => { + const seen = new Set(); + for (const key of keys) { + if (seen.has(key)) break; + seen.add(key); + } + return seen.size; + }; + return Math.max(run(s), run([...s].reverse())); + }; + // The edge starts at 0, not -1, so a stretch from the first key is one short. + const edgeAtZero = (s) => { + const last = new Map(); + let best = 0; + let edge = 0; + [...s].forEach((key, i) => { + if (last.has(key)) edge = Math.max(edge, last.get(key)); + last.set(key, i); + best = Math.max(best, i - edge); + }); + return best; + }; + // Measures a stretch only when a repeat ends it, so the last one is never counted. + const bestAtRepeat = (s) => { + const last = new Map(); + let best = 0; + let left = 0; + [...s].forEach((key, i) => { + if (last.has(key) && last.get(key) >= left) { + best = Math.max(best, i - left); + left = last.get(key) + 1; + } + last.set(key, i); + }); + return best; + }; + // The contract compares case-sensitively and counts every character. + const lowercased = (s) => longest(s.toLowerCase()); + const lettersOnly = (s) => longest(s.replace(/[^A-Za-z]/g, "")); + const symbolsDropped = (s) => longest(s.replace(/[^A-Za-z0-9 ]/g, "")); + const trimmed = (s) => longest(s.trim()); + const trimmedStart = (s) => longest(s.trimStart()); + const trimmedEnd = (s) => longest(s.trimEnd()); + // A last-seen table of 26 slots: a key outside a to z is never recorded. + const slots26 = (s) => { + const last = new Int32Array(26).fill(-1); + let best = 0; + let left = 0; + [...s].forEach((key, i) => { + const slot = key.charCodeAt(0) - 97; + if (slot >= 0 && slot < 26) { + if (last[slot] >= left) left = last[slot] + 1; + last[slot] = i; + } + best = Math.max(best, i - left + 1); + }); + return best; + }; + const mid = "repeat in the middle of the stretch"; + for (const [name, shortcut, label] of [ + ["clearing the stretch and restarting at the repeat", restart, mid], + ["moving the left edge to the repeat itself", leftAtRepeat, mid], + [ + "moving the left edge one key per repeat", + oneStep, + "left edge jumps more than one key", + ], + [ + "measuring only the stretches at the ends", + endsOnly, + "longest stretch away from both ends", + ], + ["starting the edge at 0 instead of -1", edgeAtZero, "every key distinct"], + [ + "measuring a stretch only when a repeat ends it", + bestAtRepeat, + "every key distinct", + ], + ["lowercasing the keys first", lowercased, "case and symbols count"], + [ + "dropping keys that are not letters", + lettersOnly, + "case and symbols count", + ], + ["a 26-slot last-seen table", slots26, "space before and after the keys"], + ["dropping symbols", symbolsDropped, "space before and after the keys"], + [ + "trimming the spaces at the ends", + trimmed, + "space before and after the keys", + ], + ["trimming only the leading spaces", trimmedStart, "leading space only"], + ["trimming only the trailing spaces", trimmedEnd, "trailing space only"], + ]) { + assertRejects(problem, label, shortcut, `"${label}" must reject ${name}`); + } +}); + test("basic-calculator cases reject parsers without a unary minus", () => { // One parser for the wrong ones, which differ only in when a "-" that starts // a term is read as a sign, and in what that sign negates. `sign` sees the diff --git a/tests/golden/task-posed.json b/tests/golden/task-posed.json index 288083e16..26650102c 100644 --- a/tests/golden/task-posed.json +++ b/tests/golden/task-posed.json @@ -70,7 +70,7 @@ "longest-consecutive-sequence": "fbc9d01a4cc93c6666cbbe7d2e7da71b35ad353d8b38eb3101de487c224cdf1c", "longest-increasing-subsequence": "0c3712a3922832ae9a5db5768610c840becaa741510eedf585d3aae911a42f76", "longest-palindromic-substring": "41b0768dd2694b8b0a2bb8f8c838f60d4f50a1a6c8fe612f0fee1fa98d7f81b2", - "longest-substring-without-repeating-characters": "ebd99596d34cb7ad7e874257d098f7b06664d91130f4e32737413aa9f774a6fd", + "longest-substring-without-repeating-characters": "d613e28875435580789032eac9182f23401f3bafd3c573946fa45e55956b5000", "lowest-common-ancestor-of-a-binary-tree": "62870890439fbf57da12dd60644329b2898034659a9f5a7e6a6d3a1bdd7fb951", "lru-cache": "fd5849f0a787c27530a6b9e10752b8ac0d8f9680f7cc49142328d5e96293ed69", "majority-element": "fcb70e7b9a38ddcbe45758af0b52784b2e6dfe70f42259196a0af97e113cacee", diff --git a/tests/web/contract.rs b/tests/web/contract.rs index f751de79e..423e75555 100644 --- a/tests/web/contract.rs +++ b/tests/web/contract.rs @@ -609,7 +609,39 @@ fn static_problem_bank_and_judges_cover_each_problem() { ("happy-number", "loops through four"), ( "longest-substring-without-repeating-characters", - "left edge", + "left edge must not move backward", + ), + ( + "longest-substring-without-repeating-characters", + "repeat in the middle of the stretch", + ), + ( + "longest-substring-without-repeating-characters", + "left edge jumps more than one key", + ), + ( + "longest-substring-without-repeating-characters", + "longest stretch away from both ends", + ), + ( + "longest-substring-without-repeating-characters", + "every key distinct", + ), + ( + "longest-substring-without-repeating-characters", + "case and symbols count", + ), + ( + "longest-substring-without-repeating-characters", + "space before and after the keys", + ), + ( + "longest-substring-without-repeating-characters", + "leading space only", + ), + ( + "longest-substring-without-repeating-characters", + "trailing space only", ), ("minimum-window-substring", "duplicate required"), ( diff --git a/web/judges/distinct-keystroke-stretch.json b/web/judges/distinct-keystroke-stretch.json index e7d97e0d9..accc4430b 100644 --- a/web/judges/distinct-keystroke-stretch.json +++ b/web/judges/distinct-keystroke-stretch.json @@ -37,6 +37,62 @@ "" ], "expected": 0 + }, + { + "label": "repeat in the middle of the stretch", + "input": [ + "dvdf" + ], + "expected": 3 + }, + { + "label": "left edge jumps more than one key", + "input": [ + "abccad" + ], + "expected": 3 + }, + { + "label": "longest stretch away from both ends", + "input": [ + "aabcdaa" + ], + "expected": 4 + }, + { + "label": "every key distinct", + "input": [ + "abc" + ], + "expected": 3 + }, + { + "label": "case and symbols count", + "input": [ + "aA 1" + ], + "expected": 4 + }, + { + "label": "space before and after the keys", + "input": [ + " A!1 " + ], + "expected": 4 + }, + { + "label": "leading space only", + "input": [ + " abca" + ], + "expected": 4 + }, + { + "label": "trailing space only", + "input": [ + "abca " + ], + "expected": 4 } ], "paramNames": [