tinycodinglm / cp2 /cp-0016.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
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.

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

Shortest Covering Window

Topic: Two pointers and sliding window | Difficulty: medium

Problem

You are given a text s and a pattern t, both of lowercase letters. A window of s is a contiguous substring. A window covers t if it contains every letter of t at least as many times as t does (letter multiplicities matter, order does not). Find the minimum length of a window covering t, and among windows of that length the smallest starting position (1-indexed). If no window covers t, print -1.

Input

The first line contains s. The second line contains t.

Output

Print "len start" (two integers separated by a space), or -1 if no window covers t.

Constraints

  • 1 <= |s|, |t| <= 2*10^5
  • s and t consist of lowercase English letters

Examples

Example 1

Input:

markdown
adobecodebanc
abc

Output:

markdown
4 10

Note: The window "banc" starting at position 10 is the shortest cover.

Example 2

Input:

markdown
a
aa

Output:

markdown
-1

Example 3

Input:

markdown
abab
ab

Output:

markdown
2 1

Note: Windows ab (position 1), ba (2) and ab (3) all have length 2; the smallest start is reported.

Solve this in JavaScript (Node.js), Ruby, C#, one complete program per language. Include a concise rationale (approach, correctness idea, time/space complexity).

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

Approach. Expand the right end while tracking how many required letters are still missing; once nothing is missing, shrink from the left as far as the cover holds and record the window if strictly shorter than the best.

Correctness. For each right end the shrunk window is the shortest cover ending there, and scanning right ends in increasing order with a strict improvement test keeps the leftmost among equal lengths.

Complexity. Time: O(|s| + |t|). Space: O(1).

JavaScript (Node.js)

javascript
const lines = require('fs').readFileSync(0, 'utf8').split('\n').map(x => x.trim());
const s = lines[0], t = lines[1];
const need = new Array(26).fill(0);
for (const ch of t) need[ch.charCodeAt(0) - 97]++;
let missing = t.length, l = 0, bestLen = Infinity, bestStart = -1;
for (let r = 0; r < s.length; r++) {
  const c = s.charCodeAt(r) - 97;
  if (need[c] > 0) missing--;
  need[c]--;
  while (l <= r && need[s.charCodeAt(l) - 97] < 0) {
    need[s.charCodeAt(l) - 97]++;
    l++;
  }
  if (missing === 0 && r - l + 1 < bestLen) {
    bestLen = r - l + 1;
    bestStart = l + 1;
  }
}
console.log(bestStart < 0 ? '-1' : bestLen + ' ' + bestStart);

Ruby

ruby
s = gets.strip
t = gets.strip
need = Array.new(26, 0)
t.each_byte { |c| need[c - 97] += 1 }
missing = t.size
l = 0
best_len = nil
best_start = -1
sb = s.bytes
sb.each_with_index do |ch, r|
  c = ch - 97
  missing -= 1 if need[c] > 0
  need[c] -= 1
  while l <= r && need[sb[l] - 97] < 0
    need[sb[l] - 97] += 1
    l += 1
  end
  if missing == 0 && (best_len.nil? || r - l + 1 < best_len)
    best_len = r - l + 1
    best_start = l + 1
  end
end
puts best_start < 0 ? "-1" : "#{best_len} #{best_start}"

C#

csharp
using System;

class Program
{
    static void Main()
    {
        string s = Console.ReadLine().Trim();
        string t = Console.ReadLine().Trim();
        int[] need = new int[26];
        foreach (char ch in t) need[ch - 'a']++;
        int missing = t.Length, l = 0, bestLen = int.MaxValue, bestStart = -1;
        for (int r = 0; r < s.Length; r++)
        {
            int c = s[r] - 'a';
            if (need[c] > 0) missing--;
            need[c]--;
            while (l <= r && need[s[l] - 'a'] < 0)
            {
                need[s[l] - 'a']++;
                l++;
            }
            if (missing == 0 && r - l + 1 < bestLen)
            {
                bestLen = r - l + 1;
                bestStart = l + 1;
            }
        }
        Console.WriteLine(bestStart < 0 ? "-1" : bestLen + " " + bestStart);
    }
}