Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions problem-bank/judges.json
Original file line number Diff line number Diff line change
Expand Up @@ -2636,6 +2636,48 @@
""
],
"expected": 0
},
{
"label": "repeat in the middle of the stretch",
"input": [
"dvdf"
Comment thread
jserv marked this conversation as resolved.
Resolved
],
"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",
Comment thread
ye144440154 marked this conversation as resolved.
Resolved
Comment thread
ye144440154 marked this conversation as resolved.
Resolved
"input": [
"abc"
],
"expected": 3
},
{
"label": "case and symbols count",
"input": [
"aA 1"
Comment thread
ye144440154 marked this conversation as resolved.
Resolved
],
"expected": 4
},
{
"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.

],
"expected": 4
}
],
"paramNames": [
Expand Down
150 changes: 150 additions & 0 deletions tests/browser/compiler-explorer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,156 @@ test("remove-element cases reject skipping a last unchecked target", () => {
}
});

test("longest-substring cases reject shortcuts in moving the stretch", () => {
Comment thread
ye144440154 marked this conversation as resolved.
Resolved
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) {
Comment thread
ye144440154 marked this conversation as resolved.
Resolved
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());
// 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"],
Comment thread
ye144440154 marked this conversation as resolved.
Resolved
[
"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",
],
]) {
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
Expand Down
2 changes: 1 addition & 1 deletion tests/golden/task-posed.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "e2028d2b91a474b1eb67e1c4ea010dccd6449eb9eaa91bbe41414651b6317b67",
"lowest-common-ancestor-of-a-binary-tree": "62870890439fbf57da12dd60644329b2898034659a9f5a7e6a6d3a1bdd7fb951",
"lru-cache": "fd5849f0a787c27530a6b9e10752b8ac0d8f9680f7cc49142328d5e96293ed69",
"majority-element": "fcb70e7b9a38ddcbe45758af0b52784b2e6dfe70f42259196a0af97e113cacee",
Expand Down
26 changes: 25 additions & 1 deletion tests/web/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,31 @@ 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",
),
("minimum-window-substring", "duplicate required"),
(
Expand Down
42 changes: 42 additions & 0 deletions web/judges/distinct-keystroke-stretch.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,48 @@
""
],
"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
}
],
"paramNames": [
Expand Down
Loading