atcoder#AGC054A. [AGC054A] Remove Substrings
[AGC054A] Remove Substrings
Score : points
Problem Statement
Given is a string of length consisting of lowercase English letters.
You can do the following operation on any number of times.
- Choose its (non-empty) contiguous substring such that the first character and the last character are different, and delete this substring.
Determine whether it is possible to make empty. If it is possible, find the minimum number of operations needed to do so.
Constraints
- is a string of length consisting of lowercase English letters.
Input
Input is given from Standard Input in the following format:
Output
If it is possible to make empty, print the minimum number of operations needed to do so. Otherwise, print .
4
abba
2
We should do as follows: abba
→ (delete ab
) → ba
→ (delete ba
) → an empty string.
3
aba
-1