Smallest Character Window
Python
Medium
3 views
Problem Description
A string s is provided and integer k. Output the lexicographically smallest substring of length k. If k>len(s), output EMPTY.
Input Format
Line1: s. Line2: k.
Output Format
One substring or EMPTY.
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if len(lines)<2: sys.exit(0)
s=lines[0].strip()
k=int(lines[1].strip())
if k>len(s) or k<=0:
sys.stdout.write('EMPTY')
else:
best=s[0:k]
for i in range(1,len(s)-k+1):
sub=s[i:i+k]
if sub<best:
best=sub
sys.stdout.write(best)
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!