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
cp-practice-sim trace
System 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 com...

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.

User 2026-11-07T11:20:03.537Z

Two Flavour Necklace

Topic: Two pointers and sliding window | Difficulty: easy

Problem

A 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.

Input

A single line containing the string s.

Output

Print the maximum length.

Constraints

  • 1 <= |s| <= 2*10^5
  • s consists of lowercase English letters

Examples

Example 1

Input:

markdown
abcbbbbcccbdddadacb

Output:

markdown
10

Note: The substring bcbbbbcccb uses only b and c.

Example 2

Input:

markdown
aaaa

Output:

markdown
4

Solve this in Rust, TypeScript, C++17, one complete program per language. Include a concise rationale (approach, correctness idea, time/space complexity).

Assistant 2026-11-07T11:20:05.511Z

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.

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.

Complexity. Time: O(n). Space: O(1).

Rust

rust
use std::io::*;
fn main() {
    let mut s = String::new();
    stdin().read_line(&mut s).unwrap();
    let b = s.trim().as_bytes();
    let mut cnt = [0usize; 26];
    let mut distinct = 0;
    let mut l = 0;
    let mut best = 0;
    for r in 0..b.len() {
        let c = (b[r] - b'a') as usize;
        if cnt[c] == 0 { distinct += 1; }
        cnt[c] += 1;
        while distinct > 2 {
            let d = (b[l] - b'a') as usize;
            cnt[d] -= 1;
            if cnt[d] == 0 { distinct -= 1; }
            l += 1;
        }
        best = best.max(r - l + 1);
    }
    println!("{}", best);
}

TypeScript

typescript
declare const require: any;
const s: string = require('fs').readFileSync(0, 'utf8').trim();
const cnt: number[] = new Array(26).fill(0);
let distinct = 0, l = 0, best = 0;
for (let r = 0; r < s.length; r++) {
  const c = s.charCodeAt(r) - 97;
  if (cnt[c] === 0) distinct++;
  cnt[c]++;
  while (distinct > 2) {
    const d = s.charCodeAt(l) - 97;
    cnt[d]--;
    if (cnt[d] === 0) distinct--;
    l++;
  }
  best = Math.max(best, r - l + 1);
}
console.log(String(best));

C++17

cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
    string s;
    cin >> s;
    int cnt[26] = {0}, distinct = 0, l = 0, best = 0;
    for (int r = 0; r < (int)s.size(); r++) {
        if (cnt[s[r] - 'a']++ == 0) distinct++;
        while (distinct > 2) {
            if (--cnt[s[l] - 'a'] == 0) distinct--;
            l++;
        }
        best = max(best, r - l + 1);
    }
    printf("%d\n", best);
}