problems / 254

Cutting the Ribbon Banner (easy version)

easy strings palindromes brute-force two-pointers 1000 ms · 256 MB

Cutting the Ribbon Banner (easy version)

A ribbon banner carries a string s of n lowercase English letters. The banner must be cut exactly once, between two neighbouring letters, into two non-empty pieces, and each piece must read the same from left to right as from right to left (it must be a palindrome).

A cut at position i (1 ≤ i ≤ n − 1) splits the banner into the first piece s[1..i] (the first i letters) and the second piece s[i+1..n] (the remaining n − i letters).

Find the smallest position i where the banner can be cut so that both pieces are palindromes.

Input

The first line contains an integer n (1 ≤ n ≤ 1000), the length of the banner.

The second line contains the string s of exactly n lowercase English letters.

Output

Print the smallest cut position i (1 ≤ i ≤ n − 1) such that both s[1..i] and s[i+1..n] are palindromes, or -1 if no such position exists.

Notes

In the first example, noonabba is cut after the fourth letter into noon and abba. No earlier cut works: for example, n is a palindrome but oonabba is not.

In the second example, neither a | bc nor ab | c gives two palindromes, so the answer is -1.

In the third example, every cut of aaaa works, and the smallest position is 1. A single letter is a palindrome, but a piece may never be empty, so a banner with just one letter can't be cut at all.

Example 1

Input

8
noonabba

Output

4

Example 2

Input

3
abc

Output

-1

Example 3

Input

4
aaaa

Output

1

Hints · reveal one at a time if you're stuck

Hint 1

There are only n − 1 places to cut, and n ≤ 1000. Could you simply try each of them, from left to right?

Hint 2

For one cut, check each piece with two indices walking inwards from its ends, comparing letters. The first cut where both pieces pass is the answer; a cut must leave both pieces non-empty.

report an issue Prefer your own editor? sooks prep 254 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.