#C55. Smallest and Largest Substring

    ID: 49155 Type: Default 1000ms 256MiB

Smallest and Largest Substring

Smallest and Largest Substring

Given a string \(S\) consisting of lowercase English letters and an integer \(K\), your task is to find the lexicographically smallest and largest substrings of length \(K\) from \(S\). If \(S\) is empty, \(K\) is 0, or \(K\) is greater than the length of \(S\), output two empty lines.

For example, when \(S = \texttt{welcometojava}\) and \(K = 3\), all substrings of length 3 are considered, yielding \(\texttt{ava}\) as the smallest and \(\texttt{wel}\) as the largest substring according to lexicographical order.

Note: The lexicographical order follows the standard dictionary order.

inputFormat

The input is read from standard input (stdin) and consists of two lines:

  • The first line contains the string \(S\) (which may be empty).
  • The second line contains an integer \(K\) indicating the length of substrings to consider.

outputFormat

Print the result to standard output (stdout) in two lines:

  • The first line should contain the lexicographically smallest substring of length \(K\).
  • The second line should contain the lexicographically largest substring of length \(K\).

If no valid substring exists, output two empty lines.

## sample
welcometojava
3
ava

wel

</p>