tinycodinglm / cp2 /cp-0014.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
3.8 kB
{"type": "session", "harness": "cp-practice-sim", "id": "cp-0014", "name": "Two Flavour Necklace", "mode": "chat", "topic": "Two pointers and sliding window", "difficulty": "easy", "tags": ["sliding window", "strings"], "explanation_mode": "concise-rationale", "tool_scenario": "none", "expected_recovery": "N/A - no fault injected.", "languages": ["rust", "typescript", "cpp"], "unexecuted_languages": [], "time_complexity": "O(n)", "space_complexity": "O(1)"}
{"type": "message", "message": {"role": "system", "content": "You are a competitive-programming assistant. Deliver correct, complete, runnable solutions that read standard input and write standard output. Give only a concise rationale: approach, correctness idea, and time/space complexity. ", "timestamp": 1794050401700}}
{"type": "message", "message": {"role": "user", "content": "# Two Flavour Necklace\n\nTopic: Two pointers and sliding window | Difficulty: easy\n\n## Problem\nA string s of lowercase letters describes a row of beads. Find the length of the longest contiguous substring that contains at most two distinct letters.\n\n## Input\nA single line containing the string s.\n\n## Output\nPrint the maximum length.\n\n## Constraints\n- 1 <= |s| <= 2*10^5\n- s consists of lowercase English letters\n\n## Examples\n### Example 1\nInput:\n```\nabcbbbbcccbdddadacb\n```\nOutput:\n```\n10\n```\nNote: The substring bcbbbbcccb uses only b and c.\n\n### Example 2\nInput:\n```\naaaa\n```\nOutput:\n```\n4\n```\n\nSolve this in Rust, TypeScript, C++17, one complete program per language. Include a concise rationale (approach, correctness idea, time/space complexity).", "timestamp": 1794050403537}}
{"type": "message", "message": {"role": "assistant", "content": "**Approach.** Maintain a window with letter counts and the number of distinct letters; when more than two distinct letters appear, advance the left end until at most two remain.\n\n**Correctness.** Removing characters from the left only decreases the distinct count, so for each right end the window kept is the longest valid one ending there.\n\n**Complexity.** Time: O(n). Space: O(1).\n\n### Rust\n```rust\nuse std::io::*;\nfn main() {\n let mut s = String::new();\n stdin().read_line(&mut s).unwrap();\n let b = s.trim().as_bytes();\n let mut cnt = [0usize; 26];\n let mut distinct = 0;\n let mut l = 0;\n let mut best = 0;\n for r in 0..b.len() {\n let c = (b[r] - b'a') as usize;\n if cnt[c] == 0 { distinct += 1; }\n cnt[c] += 1;\n while distinct > 2 {\n let d = (b[l] - b'a') as usize;\n cnt[d] -= 1;\n if cnt[d] == 0 { distinct -= 1; }\n l += 1;\n }\n best = best.max(r - l + 1);\n }\n println!(\"{}\", best);\n}\n```\n\n### TypeScript\n```typescript\ndeclare const require: any;\nconst s: string = require('fs').readFileSync(0, 'utf8').trim();\nconst cnt: number[] = new Array(26).fill(0);\nlet distinct = 0, l = 0, best = 0;\nfor (let r = 0; r < s.length; r++) {\n const c = s.charCodeAt(r) - 97;\n if (cnt[c] === 0) distinct++;\n cnt[c]++;\n while (distinct > 2) {\n const d = s.charCodeAt(l) - 97;\n cnt[d]--;\n if (cnt[d] === 0) distinct--;\n l++;\n }\n best = Math.max(best, r - l + 1);\n}\nconsole.log(String(best));\n```\n\n### C++17\n```cpp\n#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n string s;\n cin >> s;\n int cnt[26] = {0}, distinct = 0, l = 0, best = 0;\n for (int r = 0; r < (int)s.size(); r++) {\n if (cnt[s[r] - 'a']++ == 0) distinct++;\n while (distinct > 2) {\n if (--cnt[s[l] - 'a'] == 0) distinct--;\n l++;\n }\n best = max(best, r - l + 1);\n }\n printf(\"%d\\n\", best);\n}\n```", "timestamp": 1794050405511}}