Problem:
Score : 500 points

Problem Statement

You are given a string S consisting of 0 and 1.
Find the maximum integer K not greater than |S| such that we can turn all the characters of S into 0 by repeating the following operation some number of times.

Choose a contiguous segment [l,r] in S whose length is at least K (that is, r-l+1\geq K must be satisfied). For each integer i such that l\leq i\leq r, do the following: if S_i is 0, replace it with 1; if S_i is 1, replace it with 0.

Constraints

1\leq |S|\leq 10^5

S_i(1\leq i\leq N) is either 0 or 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum integer K such that we can turn all the characters of S into 0 by repeating the operation some number of times.

Sample Input 1

010

Sample Output 1

2

We can turn all the characters of S into 0 by the following operations:

Perform the operation on the segment S[1,3] with length 3. S is now 101.

Perform the operation on the segment S[1,2] with length 2. S is now 011.

Perform the operation on the segment S[2,3] with length 2. S is now 000.

Sample Input 2

100000000

Sample Output 2

8

Sample Input 3

00001111

Sample Output 3

4

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

class Main
{
	public static void main(String[] args) throws Exception
	{
		invoke(System.in);
//		invoke(new FileInputStream("/tmp/test.txt"));
	}

	public static void	invoke(InputStream is) throws IOException
	{
		// input
		BufferedReader	reader = new BufferedReader(new InputStreamReader(is));
		String	line = reader.readLine();
		char[]	ca = line.toCharArray();
		boolean[]	ba = new boolean[ca.length];

		for (int i = 0; i < ba.length; ++i)
		{
			ba[i] = (ca[i] == '1');
		}

		boolean	isAllSame = true;
		for (int i = 1; i < ba.length; ++i)
		{
			if (ba[i] != ba[0])
			{
				isAllSame = false;
			}
		}
		if (isAllSame)
		{
			System.out.println(ca.length);
			return;
		}

		int	k;
		for (k = ca.length - 1; k > 0; --k)
		{
			if (checkR(ba, k, 0))
			{
				break;
			}
		}

		System.out.println(k);
	}

	public static boolean	checkR(boolean[] caPrev, int k, int pos)
	{
		if (checkArray(caPrev, k))
		{
			return	true;
		}

		boolean[]	ca = new boolean[caPrev.length];
		System.arraycopy(caPrev, 0, ca, 0, caPrev.length);

		// flip
		for (int i = 0; i < k; ++i)
		{
			ca[pos + i] = !ca[pos + i];
		}

		for (int i = pos + 1; i + k <= caPrev.length; ++i)
		{
			if (checkR(ca, k, i))
			{
				return	true;
			}
		}
		return	false;
	}


	private static boolean checkArray(boolean[] ca, int k)
	{
		boolean	c = ca[0];
		int	len = 1;
		int	nFlip = 0;
		int	twoFlipLen = 0;

		for (int i = 1; i < ca.length; ++i)
		{
			if (ca[i] != c)
			{
				// check
				if (nFlip == 0 && len < k)
				{
					twoFlipLen = len;	// ok
				}
				else if (len % k != 0)
				{
					return	false;
				}
				nFlip += 1;

				// ok
				c = ca[i];
				len = 1;
			}
			else
			{
				len += 1;
			}
		}

		if (nFlip == 1)
		{
			return (twoFlipLen % k == 0 || (ca.length - twoFlipLen) % k == 0);
		}
		return true;
	}
}