#C12798. Smallest Lexicographical Substring
Smallest Lexicographical Substring
Smallest Lexicographical Substring
You are given a string s
and an integer n
. Your task is to find the lexicographically smallest substring of s
with length n
.
If there are multiple substrings that are equally smallest in lexicographical order, return the one that appears first in the string. If n
is greater than the length of s
, output an empty string.
Recall that a string a is lexicographically smaller than a string b if either
1. a is a prefix of b (and a ≠ b), or
2. there exists an index i such that \(a_1 = b_1, a_2 = b_2, ..., a_{i-1} = b_{i-1}\) and \(a_i < b_i\).
inputFormat
The input consists of two lines:
- The first line contains the string
s
, which may include letters, special characters, and spaces. - The second line contains an integer
n
indicating the length of the substring to find.
Read input from standard input (stdin).
outputFormat
Output the lexicographically smallest substring of s
with length n
to standard output (stdout). If no such substring exists (i.e. n
is greater than the length of s
), output an empty string.
programming
3
amm