using
System;
using
System.Collections;
using
System.Collections.Generic;
class
GFG {
static
Dictionary<
string
,
int
> dp
=
new
Dictionary<
string
,
int
>();
static
int
solve(
char
[] s,
char
c)
{
if
(s.Length == 1)
return
0;
if
(s.Length == 2) {
if
(s[0] == s[1] && s[0] == c)
return
1;
else
return
0;
}
if
(dp.ContainsKey(
new
string
(s) +
" "
+ c))
return
dp[
new
string
(s) +
" "
+ c];
int
ans = 0;
if
(s[0] == s[s.Length - 1] && s[0] == c)
{
for
(
char
c1 =
'a'
; c1 <=
'z'
; c1++)
if
(c1 != c) {
int
len = s.Length - 2;
char
[] tmp =
new
char
[len];
Array.Copy(s, 1, tmp, 0, len);
ans = Math.Max(ans, 1 + solve(tmp, c1));
}
}
else
{
for
(
int
i = 0; i < s.Length; i++)
{
if
(s[i] == c)
{
for
(
int
j = s.Length - 1; j > i; j--)
if
(s[j] == c)
{
if
(j == i)
break
;
int
len = j + 1 - i;
char
[] tmp =
new
char
[len];
Array.Copy(s, i, tmp, 0, len);
ans = solve(tmp, c);
break
;
}
break
;
}
}
}
dp[
new
string
(s) +
" "
+ c] = ans;
return
dp[
new
string
(s) +
" "
+ c];
}
public
static
void
Main(
string
[] args)
{
string
s =
"abscrcdba"
;
int
ma = 0;
for
(
char
c1 =
'a'
; c1 <=
'z'
; c1++)
ma = Math.Max(ma,
solve(s.ToCharArray(), c1) * 2);
Console.Write(ma +
"\n"
);
}
}