tinycodinglm / cp2 /cp-0035.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
12.5 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. Tools: write_file(path, content) writes a UTF-8 file (creating directories); run_shell(command) runs a bash command and returns its output and exit status. Test solutions on the provided examples before answering.

User 2026-11-08T08:20:03.537Z

Balanced Trio Windows

Topic: Hashing and counting (maps/sets) | Difficulty: medium

Problem

A string s consists only of the letters a, b and c. Count the non-empty substrings in which the letters a, b and c each occur the same number of times.

Input

One line containing s.

Output

One integer, the number of such substrings (substrings at different positions count separately).

Constraints

  • 1 <= |s| <= 2*10^5

Examples

Example 1

Input:

markdown
abcabc

Output:

markdown
5

Note: abc, bca, cab, abc (positions 4-6) and the whole string.

Example 2

Input:

markdown
aaa

Output:

markdown
0

Example 3

Input:

markdown
aabbcc

Output:

markdown
1

Solve this task. Write solutions in Go, Kotlin, Java 17, test what you can run on the examples, and send back the final programs. Include a concise rationale (approach, correctness idea, time/space complexity).

Assistant 2026-11-08T08:20:05.511Z

Writing the Go solution and testing it on the examples.

Assistant 2026-11-08T08:20:09.870Z

Saving the example inputs.

3 tool calls (write_file)
Assistant 2026-11-08T08:20:18.432Z

Compiling the Go solution.

Assistant 2026-11-08T08:20:22.635Z

Running every example.

3 tool calls (run_shell)
Assistant 2026-11-08T08:20:31.785Z

Cross-checking with the Kotlin version.

Assistant 2026-11-08T08:20:35.832Z

Compiling the Kotlin solution.

Assistant 2026-11-08T08:20:40.427Z

Running every example.

3 tool calls (run_shell)
Assistant 2026-11-08T08:20:48.561Z

Cross-checking with the Java 17 version.

Assistant 2026-11-08T08:20:53.000Z

Compiling the Java 17 solution.

Assistant 2026-11-08T08:20:57.987Z

Running every example.

3 tool calls (run_shell)
Assistant 2026-11-08T08:21:06.005Z

Approach. Track prefix counts (A,B,C) and reduce each to the pair (A-B, B-C). Two prefixes give a balanced substring exactly when their pairs are equal, so count equal pairs with a hash map.

Correctness. Substring counts are equal iff A-B and B-C do not change between its end prefixes, i.e. the pairs coincide; each unordered pair of equal prefixes is one substring.

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

All solutions below passed every example.

Go

go
package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    r := bufio.NewReaderSize(os.Stdin, 1<<20)
    var s string
    fmt.Fscan(r, &s)
    m := map[[2]int]int{{0, 0}: 1}
    a, b, c := 0, 0, 0
    var ans int64
    for _, ch := range s {
        switch ch {
        case 'a':
            a++
        case 'b':
            b++
        default:
            c++
        }
        k := [2]int{a - b, b - c}
        ans += int64(m[k])
        m[k]++
    }
    fmt.Println(ans)
}

Kotlin

kotlin
fun main() {
    val s = readLine()!!.trim()
    val m = HashMap<Long, Int>()
    val off = 300000L
    fun key(x: Int, y: Int): Long = (x + off) * 1000003L + (y + off)
    m[key(0, 0)] = 1
    var a = 0
    var b = 0
    var c = 0
    var ans = 0L
    for (ch in s) {
        when (ch) {
            'a' -> a++
            'b' -> b++
            else -> c++
        }
        val k = key(a - b, b - c)
        val v = m.getOrDefault(k, 0)
        ans += v
        m[k] = v + 1
    }
    println(ans)
}

Java 17

java
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        String s = new BufferedReader(new InputStreamReader(System.in)).readLine().trim();
        HashMap<Long, Integer> m = new HashMap<>();
        long off = 300000L;
        m.put(off * 1000003L + off, 1);
        int a = 0, b = 0, c = 0;
        long ans = 0;
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (ch == 'a') a++;
            else if (ch == 'b') b++;
            else c++;
            long k = (a - b + off) * 1000003L + (b - c + off);
            int v = m.getOrDefault(k, 0);
            ans += v;
            m.put(k, v + 1);
        }
        System.out.println(ans);
    }
}