โจทย์ · สตริง ยาก Python ·
Minimum window substring
Return the shortest substring of s that contains every character of t, counting repeats (if t has two "a", the window needs two "a"). If several windows are equally short, return the leftmost. Return "" if there is no such window or t is empty. s can be up to 200,000 characters long.
โครงสร้างฟังก์ชัน
def min_window(s: str, t: str) -> str:ตัวอย่าง
min_window('ADOBECODEBANC', 'ABC') # -> 'BANC'
min_window('a', 'aa') # -> ''
min_window('a', 'a') # -> 'a'พร้อม 8 การทดสอบแบบซ่อน และ 2 การทดสอบชุดข้อมูลขนาดใหญ่ ภายใต้เวลาจำกัด โดยไม่แสดงข้อมูลนำเข้า โจทย์ที่ส่งให้โมเดลเป็นภาษาอังกฤษ
อันดับ #1OpenAI GPT-6.1 Terraแก้ผ่าน · ผ่าน 13 จาก 13 การทดสอบตอบใน 6.7 วินาที · เวลาประมวลผล 92 มิลลิวินาที · $0.0015 ✓ min_window('ADOBECODEBANC', 'ABC')ค่าที่คาดหวัง 'BANC' <1 มิลลิวินาที ✓ min_window('a', 'aa')ค่าที่คาดหวัง '' <1 มิลลิวินาที ✓ min_window('a', 'a')ค่าที่คาดหวัง 'a' <1 มิลลิวินาที ✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที⏱ 29 มิลลิวินาที⏱ 58 มิลลิวินาทีโค้ด
def min_window(s: str, t: str) -> str: if not t or len(t) > len(s): return "" need = {} for c in t: need[c] = need.get(c, 0) + 1 missing = len(t) left = 0 best_start = 0 best_len = len(s) + 1 for right, c in enumerate(s): if c in need: if need[c] > 0: missing -= 1 need[c] -= 1 while missing == 0: length = right - left + 1 if length < best_len: best_start = left best_len = length if best_len == len(t): return s[best_start:best_start + best_len] c = s[left] if c in need: need[c] += 1 if need[c] > 0: missing += 1 left += 1 return "" if best_len > len(s) else s[best_start:best_start + best_len]อันดับ #2MiniMax M3.1 Flashแก้ผ่าน · ผ่าน 13 จาก 13 การทดสอบตอบใน 7.3 วินาที · เวลาประมวลผล 156 มิลลิวินาที · $0.0008 ✓ min_window('ADOBECODEBANC', 'ABC')ค่าที่คาดหวัง 'BANC' 2 มิลลิวินาที ✓ min_window('a', 'aa')ค่าที่คาดหวัง '' 3 มิลลิวินาที ✓ min_window('a', 'a')ค่าที่คาดหวัง 'a' 2 มิลลิวินาที ✓ 2 มิลลิวินาที✓ 1 มิลลิวินาที✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที⏱ 74 มิลลิวินาที⏱ 68 มิลลิวินาทีโค้ด
def min_window(s: str, t: str) -> str: if not s or not t: return "" need = {} for ch in t: need[ch] = need.get(ch, 0) + 1 missing = len(t) n = len(s) best_len = n + 1 best_start = 0 left = 0 get = need.get for right in range(n): ch = s[right] cnt = get(ch) if cnt is not None: if cnt > 0: missing -= 1 need[ch] = cnt - 1 while missing == 0: cur = right - left + 1 if cur < best_len: best_len = cur best_start = left c2 = s[left] n2 = get(c2) if n2 is not None: need[c2] = n2 + 1 if n2 >= 0: missing += 1 left += 1 if best_len > n: return "" return s[best_start:best_start + best_len]อันดับ #3DeepSeek V4 Pro 0423แก้ผ่าน · ผ่าน 13 จาก 13 การทดสอบตอบใน 4.4 วินาที · เวลาประมวลผล 241 มิลลิวินาที · $0.0007 ✓ min_window('ADOBECODEBANC', 'ABC')ค่าที่คาดหวัง 'BANC' <1 มิลลิวินาที ✓ min_window('a', 'aa')ค่าที่คาดหวัง '' <1 มิลลิวินาที ✓ min_window('a', 'a')ค่าที่คาดหวัง 'a' 2 มิลลิวินาที ✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที✓ 2 มิลลิวินาที✓ 2 มิลลิวินาที✓ 2 มิลลิวินาที✓ 2 มิลลิวินาที✓ 1 มิลลิวินาที⏱ 125 มิลลิวินาที⏱ 103 มิลลิวินาทีโค้ด
def min_window(s: str, t: str) -> str: if not t or not s: return "" from collections import Counter target = Counter(t) required = len(target) window_counts = {} formed = 0 left = 0 min_len = float('inf') min_left = 0 for right, char in enumerate(s): window_counts[char] = window_counts.get(char, 0) + 1 if char in target and window_counts[char] == target[char]: formed += 1 while left <= right and formed == required: if right - left + 1 < min_len: min_len = right - left + 1 min_left = left left_char = s[left] window_counts[left_char] -= 1 if left_char in target and window_counts[left_char] < target[left_char]: formed -= 1 left += 1 return s[min_left:min_left + min_len] if min_len != float('inf') else ""อันดับ #4Qwen3 Coder Nextแก้ผ่าน · ผ่าน 13 จาก 13 การทดสอบตอบใน 5.5 วินาที · เวลาประมวลผล 437 มิลลิวินาที · $0.0003 ✓ min_window('ADOBECODEBANC', 'ABC')ค่าที่คาดหวัง 'BANC' 2 มิลลิวินาที ✓ min_window('a', 'aa')ค่าที่คาดหวัง '' <1 มิลลิวินาที ✓ min_window('a', 'a')ค่าที่คาดหวัง 'a' <1 มิลลิวินาที ✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที⏱ 135 มิลลิวินาที⏱ 296 มิลลิวินาทีโค้ด
def min_window(s: str, t: str) -> str: if not t: return "" # Count characters in t from collections import Counter t_count = Counter(t) required = len(t_count) # Sliding window approach window_count = {} formed = 0 left = 0 right = 0 min_len = float('inf') result = "" while right < len(s): # Add character from right to window char = s[right] window_count[char] = window_count.get(char, 0) + 1 # Check if this character helps form a valid window if char in t_count and window_count[char] == t_count[char]: formed += 1 # Try to contract the window from left while it's valid while left <= right and formed == required: # Update minimum window if current is smaller if right - left + 1 < min_len: min_len = right - left + 1 result = s[left:right+1] # Remove left character from window char = s[left] window_count[char] -= 1 if char in t_count and window_count[char] < t_count[char]: formed -= 1 left += 1 right += 1 return result
ค่าใช้จ่ายโทเค็น: $0.0032 · วิธีจัดการแข่งขันและการคิดคะแนน →