Question DSA-TP-0016
HardWhat is the output of this code?
from collections import Counter
s, t = "ADOBECODEBANC", "ABC"
need, missing = Counter(t), len(t)
l = 0
best = (0, float("inf"))
for r, ch in enumerate(s):
if need[ch] > 0:
missing -= 1
need[ch] -= 1
while missing == 0:
if r - l < best[1] - best[0]:
best = (l, r)
need[s[l]] += 1
if need[s[l]] > 0:
missing += 1
l += 1
print(s[best[0]:best[1] + 1])
- A.
ADOBEC
- B.
CODEBA
- C.
Sign up free to see the answer and a step-by-step explanation.