#K91732. Shortest Sequential Palindromic Substring
Shortest Sequential Palindromic Substring
Shortest Sequential Palindromic Substring
You are given a string s
. A sequential palindromic substring is defined as a contiguous substring that contains at least one pair of identical consecutive characters. Since the smallest possible substring that meets this requirement is of length 2 (i.e. the pair of identical letters), your task is to determine if such a substring exists in s
. If it exists, output 2; otherwise, output -1.
Examples:
- For
s = "abbce"
, the output is2
because the substring "bb" exists. - For
s = "abcdef"
, there is no sequential palindromic substring, so the output is-1
. - For
s = "abcddcba"
, even though the string is a palindrome as a whole, the shortest sequential palindromic substring is "dd" and the output is2
.
The solution must read the input from stdin
and print the answer to stdout
.
inputFormat
The input consists of a single line containing a non-empty string s
made up of lowercase English letters.
Note: The input is provided via standard input (stdin).
outputFormat
Output a single integer: 2
if a sequential palindromic substring exists in s
(i.e. at least one pair of consecutive identical letters is found), or -1
otherwise. The result should be printed via standard output (stdout).
abbce
2
</p>