Problem:


Score : 300 points

Problem Statement

Snuke can change a string t of length N into a string t' of length N - 1 under the following rule:

For each i (1 ≤ i ≤ N - 1), the i-th character of t' must be either the i-th or (i + 1)-th character of t.

There is a string s consisting of lowercase English letters.
Snuke's objective is to apply the above operation to s repeatedly so that all the characters in s are the same.
Find the minimum necessary number of operations.

Constraints

1 ≤ |s| ≤ 100

s consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s

Output

Print the minimum necessary number of operations to achieve the objective.

Sample Input 1

serval

Sample Output 1

3

One solution is: serval → srvvl → svvv → vvv.

Sample Input 2

jackal

Sample Output 2

2

One solution is: jackal → aacaa → aaaa.

Sample Input 3

zzz

Sample Output 3

0

All the characters in s are the same from the beginning.

Sample Input 4

whbrjpjyhsrywlqjxdbrbaomnw

Sample Output 4

8

In 8 operations, he can change s to rrrrrrrrrrrrrrrrrr.



Sample input:
serval


Sample output:
3

Submission status: Accepted

Solution code:
s = ["0"] + list(input())
n = len(s) - 1
alph = [chr(i) for i in range(97, 123)]
ans = 10000
for i in alph:
    dp = [[0] * (n + 1) for _ in range(n + 1)]
    for j in range(1, n + 1):
        if s[j] == i:
            dp[n][j] = 1
    if sum(dp[n]) != 0:
        if sum(dp[n]) == n:
            c = n
        else:
            for j in range(n - 1, 0, -1):
                for k in range(1, j + 1):
                    if dp[j + 1][k] == 1 or dp[j + 1][k + 1] == 1:
                        dp[j][k] = 1
                if sum(dp[j]) == j:
                    c = j
                    break
        if n - c < ans:
            ans = n - c
print(ans)

Submission status: Accepted

Solution code:
s=input()
N=len(s)
st=set(s)

ans=float("inf")
for str in st:
  a=s.split(str)
  max_len=0
  for part in a:
    leng=len(part)
    max_len=max(max_len,leng)
  #print(a,max_len)
  ans=min(ans,max_len)
  
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
S = input()
dict = defaultdict(int)

for s in S:
    dict[s] += 1

ans = 10 ** 3
for c in dict.keys():
    max_seq = 0
    tmp = -1
    for i, s in enumerate(S):
        if s == c:
            max_seq = max(max_seq, i - tmp - 1)
            tmp = i
    max_seq = max(max_seq, len(S)-tmp-1)
    ans = min(max_seq, ans)
print(ans)

Submission status: Accepted

Solution code:
s = list(input())
chara = set(s)
N = len(s)
ans = 100

for i in chara:
    tmp = 0
    high = 0
    for j in s:
        if i != j:
            tmp += 1
        elif tmp > high:
            high = tmp
            tmp = 0
        else:
            tmp = 0
    else:
        if tmp > high:
            high = tmp

    if high < ans:
        ans = high

print(ans)

Submission status: Accepted

Solution code:
sel=3
# ABC070-D
if sel==1:
    from collections import deque
    N=int(input())
    ver=[[] for i in range(N)] # (vertex,cost) in each vertex
    for _ in range(N-1):
        a,b,c=map(int,input().split())
    ver[a-1].append((b-1,c))
    ver[b-1].append((a-1,c))

    Q,K=map(int,input().split())

    par=[-2 for i in range(N)]
    par[K-1]=-1
    q=deque([K-1])
    while q:
        i=q.popleft()
        for j,c in ver[i]:
            if par[j]==-2:
                par[j]=i
                q.append(j)

    cost=[-1 for i in range(N)]
    cost[K-1]=0
    stack=[[K-1,0]]
    while stack:
        i,c=stack.pop()
        for j,d in ver[i]:
            if par[i]!=j:
                cost[j]=c+d
                stack.append([j,cost[j]])
    # print(cost)
    for _ in range(Q):
        x,y=map(int,input().split())
    print(cost[x-1]+cost[y-1])

  

# ABC079-D
if sel==2:
    H,W=map(int,input().split())
    C=[[int(i) for i in input().split()] for j in range(10)]
    for i in range(10):
        C[i][i]=0
    for k in range(10):
        for i in range(10):
            for j in range(10):
                C[i][j]=min(C[i][j],C[i][k]+C[k][j])
    ans=0
    for _ in range(H):
        for a in input().split():
            a=int(a)
            if a!=-1:
                ans+=C[a][1]
    print(ans)

#AGC016-A
if sel==3:
    s=input()
    ans=10**3
    for k in set(list(s)):
        t=s
        cnt=0
        while len(set(t))!=1:
            tmp=''
            for i in range(len(t)-1):
                if t[i]==k or t[i+1]==k:
                    tmp+=k
                else:
                    tmp+=t[i]
            t=tmp
            cnt+=1
        ans=min(ans,cnt)
    print(ans)

Submission status: Accepted

Solution code:
s=input()
ans=float('inf')
for c in set(s):
#    print (c)
    ans=min(ans,max(map(len,s.split(c))))
#    print (ans)
print(ans)

Submission status: Accepted

Solution code:
from copy import deepcopy
s = list(input())    
N = len(s)
count= 0
counter = []
ans = []
real = deepcopy(s)
syurui = set(s)
for j in syurui:
    while True:
        #終了条件
        if N == s.count(j):
            counter.append(count)
            count = 0
            s = deepcopy(real)
            N = len(s)
            break

        for i in range(N-1):
            if s[i]==j or s[i+1]==j:
                ans.append(j)
            else:
                ans.append(s[i])
        #値の更新
        N = len(ans)
        s = deepcopy(ans)
        count += 1

        #変数のリセット
        ans = []
print(min(counter))

Submission status: Accepted

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YNL = {False: 'No', True: 'Yes'}
YNU = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True


def main():
    s = S()
    c = Counter(s)
    mosts = c.most_common()
    ans = len(s)

    for most in mosts:
        t = [i for i in s]
        loop = 0
        while not all([i == most[0] for i in t]):
            cur = []
            for i in range(len(t)):
                if i < len(t) - 1 and t[i+1] == most[0]:
                    cur.append(t[i+1])
                else:
                    cur.append(t[i])
            cur.pop()
            t = cur
            loop += 1
        ans = min(ans, loop)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from decimal import *
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
INF = float('inf') #無限大
def gcd(a,b):return fractions.gcd(a,b) #最大公約数
def lcm(a,b):return (a*b) // fractions.gcd(a,b) #最小公倍数
def iin(): return int(sys.stdin.readline()) #整数読み込み
def ifn(): return float(sys.stdin.readline()) #浮動小数点読み込み
def isn(): return sys.stdin.readline().split() #文字列読み込み
def imn(): return map(int, sys.stdin.readline().split()) #整数map取得
def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) #整数-1map取得
def fmn(): return map(float, sys.stdin.readline().split()) #浮動小数点map取得
def iln(): return list(map(int, sys.stdin.readline().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def fln(): return list(map(float, sys.stdin.readline().split())) # 浮動小数点リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) # 順列の総数
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) #組み合わせの総数
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 # 2点間の距離
def m_add(a,b): return (a+b) % MOD
def print_list(l): print(*l, sep='\n')

def sieves_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime

S = input()
counter = collections.Counter(S)

ans = 101
for key, value in counter.items():
    T = copy.deepcopy(S) 
    T1 = ''
    count = 0
    if len(set(T)) > 1:
        while True:
            T1 = ''
            for i in range(len(T)-1):
                if T[i] == key or T[i+1] == key:
                    T1 += key
                else:
                    T1 += "?"
            count += 1
            T = copy.deepcopy(T1)
            if len(set(T1)) == 1: break
    ans = min(ans, count)
print(ans)

Submission status: Accepted

Solution code:
s = input()

ans = 1000
for cur in set(s):
    count = 0
    t_count = 0
    for i in s:
        if i == cur:
            count = max(count, t_count)
            t_count = 0
        else:
            t_count += 1

    count = max(count, t_count)
    ans = min(ans, count)

print(ans)

Submission status: Accepted

Solution code:
s = input()

ans = 1000
for cur in "abcdefghijklmnopqrstuvwxyz":
    count = 0
    t_count = 0
    for i in s:
        if i == cur:
            count = max(count, t_count)
            t_count = 0
        else:
            t_count += 1

    count = max(count, t_count)
    ans = min(ans, count)

print(ans)

Submission status: Accepted

Solution code:
s = input()

def f(s, c):
    result = 0
    while len(set(s)) != 1:
        t = ''
        for i in range(len(s) - 1):
            if s[i] == c or s[i + 1] == c:
                t += c
            else:
                t += s[i]
        s = t
        result += 1
    return result

result = float('inf')
for c in set(s):
    result = min(result, f(s, c))
print(result)

Submission status: Accepted

Solution code:
from string import ascii_lowercase

# a-zまで試す
s = input()
maxN = len(s)
for ch in ascii_lowercase:
    ss = list(s)
    count = 0

    if len(set(s)) == 1:
        count = 0
    else:
        while True:
            count += 1
            new_ss = []
            for j in range(len(ss) - 1):
                if ss[j] == ch or ss[j + 1] == ch:
                    new_ss.append(ch)
                else:
                    new_ss.append(ss[j])
            # print(new_ss)
            ss = new_ss
            if len(set(new_ss)) == 1:
                break
    maxN = min(maxN, count)
print(maxN)

Submission status: Accepted

Solution code:
from collections import Counter
from copy import deepcopy

s = input()
c = Counter(list(s))

lS = len(set(list(s)))

if lS == 1:
    print(0)
    exit()
if lS == len(s):
    print(len(s) // 2)
    exit()
else:
    maxN = len(s)
    for key in c:
        ss = list(s)
        count = 0
        # print(">", key)
        # print(ss)
        while True:
            count += 1
            new_ss = []
            for j in range(len(ss) - 1):
                if ss[j] == key or ss[j + 1] == key:
                    new_ss.append(key)
                else:
                    new_ss.append(ss[j])
            # print(new_ss)
            ss = deepcopy(new_ss)
            if len(set(new_ss)) == 1:
                break
        # print(">", key, count)
        maxN = min(maxN, count)
    print(maxN)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
# from collections import Counter

def solve(s: str):
    alpha_list = set(s)

    answer = 10**9
    for alpha in alpha_list: #全部をalphaにする
        S = s
        count = 0
        while len(set(S)) > 1:
            new_S = ""
            
            for i in range(len(S)-1):
                if S[i] == alpha or S[i+1] == alpha:
                    new_S += alpha
                else:
                    new_S += "@"
            S = new_S
            count += 1
        answer = min(answer,count)

    print(answer)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    s = next(tokens)  # type: str
    solve(s)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from string import ascii_lowercase


def main():
    word = input()
    target_list = list(ascii_lowercase)
    answer = float("inf")
    for target in target_list:
        now_ans = 0
        now_word = word
        while len(set(now_word)) > 1:
            tmp = ""
            for i in range(len(now_word) - 1):
                if now_word[i] == target or now_word[i + 1] == target:
                    tmp += target
                else:
                    tmp += now_word[i]
            now_word = tmp
            now_ans += 1
        answer = min(answer, now_ans)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S = input()
alpha = "abcdefghijklmnopqrstuvwxyz"
D = {alpha[i]: [-1] for i in range(26)}
N = len(S)
for i in range(N):
  D[S[i]].append(i)
cnt = 0
for i in D:
  D[i].append(N)
  if D[i] != [-1, N]:
    cnt += 1
if cnt == 1:
  print(0)
else:
  m = int(1e9)
  for i in D:
    n = len(D[i])
    M = max(D[i][j+1] - D[i][j] for j in range(n-1))
    if m > M:
      m = M
  print(m-1)

Submission status: Accepted

Solution code:
from string import ascii_lowercase

s = input()

# なんかわからんけど うーん やっぱわからん
def rec(target, ss):
    if len(set(ss)) == 1:
        return len(s) - len(ss)
    tt = ""
    cc = 0
    for i in range(len(ss) - 1):
        if ss[i] == target or ss[i + 1] == target:
            tt += target
        else:
            tt += ss[i]
    return rec(target, tt)


ans = 10**10
for c in ascii_lowercase:
    ans = min(ans, rec(c, s))
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
s = input()
ls = len(s)

def f(s,c):
    td = s
    cnt = 0
    while(td != c*len(td)):
        t = td
        td = ""
        cnt += 1
        for i in range(len(t)-1):
            if (t[i] == c)or(t[i+1] == c):
                td += c
            else:
                td += t[i]
    return cnt

l = []
for i in range(26):
    l.append(f(s,chr(ord("a")+i)))

print(min(l))

Submission status: Accepted

Solution code:
s = input()
chars = []
for i in range(97,123):
    chars.append(chr(i))

if( len(set(s)) == 1):
    print(0)
    exit()

ans = len(s)
for i in chars:
    tmp = s
    tmp_n = ''
    while(True):
        for j in range(len(tmp)-1):
            if((tmp[j] == i)|(tmp[j+1] == i)):
                tmp_n += i
            else:
                tmp_n += tmp[j]

        if( len(set(tmp_n)) == 1):
            ans = min(ans, len(s) - len(tmp_n))
            break

        tmp = tmp_n
        tmp_n = ''

print(ans)

Submission status: Accepted

Solution code:
s = list(input())

if len(set(s)) == 1:
    print(0)
elif len(set(s)) == len(s):
    print((len(s) + 1) // 2)
else:
    ans = 10010001001
    for i in range(ord('a'), ord('z') + 1):
        tmp = 0
        tmp2 = 0
        for c in s:
            if c == chr(i):
                tmp = 0
            else:
                tmp += 1
            tmp2 = max(tmp, tmp2)
        ans = min(ans, tmp2)

    print(ans)

Submission status: Accepted

Solution code:
S = list(input())
C = list(set(S))

ans = float("inf")
for c in C:
    cnt_max, cnt = 0, 0
    for s in S:
        if s == c:
            cnt_max = max(cnt_max, cnt)
            cnt = 0
        else:
            cnt += 1
    cnt_max = max(cnt_max, cnt)
    ans = min(ans, cnt_max)

print(ans)

Submission status: Accepted

Solution code:
s = input()
s_list = [i for i in s]
az = [chr(i) for i in range(97,97+26)]
min_ans = 10*5
for i in az:
    count = 0
    temp = [i for i in s]
    while temp.count(i)!=len(temp):
        ans = []
        for j in range(len(temp)-1):
            if temp[j]==i or temp[j+1]==i:
                ans.append(i)
            else:
                ans.append(temp[j])
        count += 1
        temp = ans
    min_ans = min(min_ans,count)
print(min_ans)

Submission status: Accepted

Solution code:
def long_diff(s, a):
    s = str(a) + s + str(a)
    index_num = [n for n, v in enumerate(s) if v == str(a)]
    res = 0
    for i in range(len(index_num)-1):
        tmp = index_num[i+1] - index_num[i]
        res = max(res,tmp)
    return res-1

s = input()
r = 100
for i in [chr(j) for j in range(97,97+26)]:
    r = min(r, long_diff(s, i))
print(r)

Submission status: Accepted

Solution code:
S = list(input())
dis = [0 for i in range(26)]
mm = [-1 for i in range(26)]

for i in range(len(S)):
    wd = ord(S[i])-97
    dis[wd] = max(dis[wd],i-mm[wd]-1)
    mm[wd] = i

for i in range(26):
    dis[i] = max(dis[i],len(S)-mm[i]-1)
print(min(dis))
# print(dis)

Submission status: Accepted

Solution code:
from copy import deepcopy as cp
s = input()
c = set(s)
ans = 10**9
for ch in c:
    #全ての文字をiにしよう！
    k = cp(s)
    cand = 0
    while len(set(k)) > 1:
        t = ""
        for i in range(len(k)-1):
            if k[i] == ch or k[i+1] == ch:
                t += ch
            else:
                t += k[i]
        cand += 1
        k = cp(t)
    ans = min(cand, ans)
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
s=input()

alpha=[chr(i+97) for i in range(26)]
ans=[]

for chara in alpha:
    temp=s
    while len(temp)>temp.count(chara):
        rna=""
        for i in range(len(temp)-1):
            if temp[i+1]==chara:
                rna+=(temp[i+1])
            elif temp[i]==chara:
                rna+=(temp[i])
            else:
                rna+=(temp[i+1])
        temp=rna
        #print(temp)
    ans.append(len(s)-len(temp))

print(min(ans))

Submission status: Accepted

Solution code:
def main():
    S = input()
    from string import ascii_lowercase
    ans = 1000
    for tar in ascii_lowercase:
        T = [s for s in S]
        if tar not in T:
            continue
        cur = 0
        while len(set(T)) != 1:
            for i in range(len(T)-1):
                if T[i] == tar or (i + 1 < len(T) and T[i+1] == tar):
                    T[i] = tar
            if T:
                T.pop()
            cur += 1
        ans = min(ans, cur)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    s = input().rstrip()
    ans = -1
    for i in range(len(s)):
        for j in range(26):
            c = chr(ord("a") + j)
            f = True
            for k in range(len(s)-i):
                sf = True
                for l in range(i+1):
                    if s[k+l] == c:
                        sf = False
                        break
                if sf:
                    f = False
                    break
            if f:
                ans = i
                break
        if ans != -1:
            break
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
S = input()

import collections
c_indices_dict = collections.defaultdict(list)
for i, c in enumerate(S):
    c_indices_dict[c].append(i)

len_S = len(S)
ret = float('inf')
for indices in c_indices_dict.values():
    v = max(indices[0], len_S - indices[-1] - 1)
    # print(indices)
    for i in range(len(indices)-1):
        v = max(v, indices[i+1] - indices[i] - 1)
        # print(indices[i+1], indices[i])
    ret = min(ret, v)
print(ret)

Submission status: Accepted

Solution code:
import collections
s=input()
l=[]
dist=[]
ma=0
cnt=0
s2=""
s3=""
l=list(set(s))
#print(l)
for i in range(len(l)):
    ma=0
    cnt=0
    for j in range(len(s)):
        #print(l[i],s[j],cnt)
        if s[j]!=l[i]:
            cnt=cnt+1
        else:
            if (ma<=cnt):
                ma=cnt
            cnt=0
    if ma <= cnt:
        ma = cnt
    #print(cnt)
    dist.append(ma)
dist.sort()
print(dist[0])

"""

l2=[]
#print(l)
for i in range(len(l)):
    c=l[i]
    k=0
    s2=s
    s3=""
    #print(len(set(s2)))
    while True:
        if len(set(s2))==1:
            break
        else:
            for j in range(len(s2)-1):
                if (s2[j]==c)or(s2[j+1]==c):
                    s3="".join((s3,c))
                else:
                    s3 = "".join((s3, s2[j]))
            print(s3)
            s2=s3
            s3=""
            k=k+1
    l2.append(k)
l2.sort()

print(l2[0])
"""

Submission status: Accepted

Solution code:
s = input()

ans = 10**9

for cand in set(s):
  cnt = 0
  a = 0
  for tmp in s:
    if tmp != cand:
      cnt+=1
    else:
      if a < cnt:
        a = cnt
      cnt = 0
  if a < cnt:
    a = cnt
  if ans > a:
    ans = a
    
print(ans)

Submission status: Accepted

Solution code:
S = input()
N = len(S)

from collections import Counter
chars = [chr(i) for i in range(ord('a'), ord('z')+1)]

def check(arr):
    e = arr[0]
    return len(arr) == arr.count(e)


ans = float('inf')
for c in chars:
    cnt = 0
    t = S
    while not check(t):
        u = ''
        for i in range(len(t)-1):
            if t[i] == c or t[i+1] == c:
                u += c
            else:
                u += t[i]
        cnt += 1
        t = u
    ans = min(ans, cnt)

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

s = input()
if len(set(list(s)))==1:
    print(0)
    exit()
    
c = dict(Counter(s))
res = []
for k,v in c.items():
    tmp_s = k + s + k
    idx_num = [e for e,w  in enumerate(list(tmp_s)) if w==k]
    lst = []
    for j in range(1, len(idx_num)):
        lst.append(idx_num[j] - idx_num[j-1])
    tmp = max(lst)
    res.append(tmp)
print(max(min(res)-1, 0))

Submission status: Accepted

Solution code:
S = input()
N = len(S)

from collections import Counter
chars = [chr(i) for i in range(ord('a'), ord('z')+1)]

def check(arr):
    e = arr[0]
    return len(arr) == arr.count(e)


ans = float('inf')
for c in chars:
    SL = list(S)
    cnt = 0
    while not check(SL):
        for i, si in enumerate(SL):
            if i == 0:
                continue
            if si == c:
                SL[i-1] = c
        SL = SL[:-1]
        cnt += 1
        # print(SL)
    ans = min(ans, cnt)

print(ans)

Submission status: Accepted

Solution code:
s=input()
n=len(s)
ans=n
for i in range(97, 97+26):
    x=chr(i)
    cnt=s.count(x)
    if cnt==n:
        ans=0
        break
    if cnt!=0:
        t=list(s)
        for j in range(n-1):
            u=[""]*(n-1-j)
            for k in range(n-1-j):
                if t[k]==x or t[k+1]==x:
                    u[k]=x
                else:
                    u[k]=t[k]
            if u.count(x)==n-1-j:
                ans=min(ans,j+1)
                break
            else:
                t=u
print(ans)

Submission status: Accepted

Solution code:
os = input()
import  collections
c = collections.Counter(os)
res = 99999
for k in c:
    s = os
    cnt = 0
    while True:
        # 一文字になったなら
        if len(collections.Counter(s)) == 1:
            break
        ss = ""
        for i in range(len(s) - 1):
            if s[i] == k or s[i+1] == k:
                ss += k
            else:
                ss += s[i]
        cnt += 1
        s = ss
    res = min(res, cnt)
print(res)

Submission status: Accepted

Solution code:
alpha = "abcdefghijklmnopqrstuvwxyz"
s = list(input())
from copy import deepcopy
ans = 10**9
for c in s:
    s_c = deepcopy(s)
    counter = 0
    while set(s_c) != {c}:
        counter += 1
        new = []
        for i in range(len(s_c)-1):
            if s_c[i] == c or s_c[i+1] == c:
                new.append(c)
            else:
                new.append(0)
        s_c = new
    ans = min(ans, counter)
print(ans)

Submission status: Accepted

Solution code:
S = list(input())

s = set(S)

ans = 1000
for w in s:
    old = S.copy()
    cnt = 0
    while True:
        if len(set(old)) == 1:
            ans = min(ans, cnt)
            break
        new = []
        cnt += 1
        for i in range(len(old) - 1):
            if old[i] == w or old[i + 1] == w:
                new.append(w)
            else:
                new.append(old[i])

        old = new.copy()

print(ans)

Submission status: Accepted

Solution code:
s = input()
a = 100
for i in s:
    c = 0
    s_t = s
    s_ = s
    while i * len(s_t) != s_:
        c += 1
        s_ = ""
        for j in range(len(s_t) - 1):
            if s_t[j] == i or s_t[j+1] == i: s_ += i
            else: s_ += s_t[j]
        s_t = s_
    a = min(a, c)
print(a)

Submission status: Accepted

Solution code:
S=input()
ans = len(S)
for a in [chr(i+ord("a"))for i in range(26)]:
    if a not in S:
        continue
    I = [-1]+[i for i,s in enumerate(S) if s==a]+[len(S)]
    ans=min(ans, max(j-i-1 for i,j in zip(I,I[1:])))
print(ans)

Submission status: Accepted

Solution code:
s = input()
r = 101
for i in set(s):
    r = min(r, max(list(map(len, s.split(i)))))
print(r)

Submission status: Accepted

Solution code:
S = input()
type = sorted(set(S))
count = [0] * len(type)

for i,t in enumerate(type):
  sen = S
  con = 0
  while True:
    tmp = ""
    if len(set(sen)) == 1:
      count[i] = con
      break
    for j in range(len(sen) - 1):
      if (sen[j] == t) or (sen[j + 1] == t):
        tmp += t
      else:
        tmp += sen[j]
    con += 1
    sen = tmp

print(min(count))

Submission status: Accepted

Solution code:
s = input()
import collections
ans=10**9
for cand in set(s):
    cnt=0
    a=0
    for tmp in s:
        if tmp!=cand:
            cnt+=1
        else:
            if a<cnt:
                a=cnt
            cnt=0
    if a<cnt:
        a=cnt
    if ans>a:
        ans=a

print(ans)

Submission status: Accepted

Solution code:
S = input()
lens = len(S)

A = {}
for s in S:
    if s not in A:
        A[s] = 1
    else:
        A[s] += 1

B = [(a, A[a]) for a in A]
B.sort(reverse=True, key=lambda x: x[1])
# print(B)

if len(B) == 1:
    print(0)
    exit()

max_num = B[0][1]

if max_num == 1:
    print(lens // 2)
    exit()

ans = 100
for i in range(len(B)):
    b = B[i][0]
    count = 0
    sub_ans = 0
    for s in S:
        if s != b:
            count += 1
        else:
            sub_ans = max(sub_ans, count)
            count = 0
    else:
        sub_ans = max(sub_ans, count)
    ans = min(sub_ans, ans)
print(ans)

Submission status: Accepted

Solution code:
import copy
s = list(input())
ans = len(s) // 2
if ("".join(s) == s[0]*len(s)):
  ans = 0
for i in range(97, 97+26):
  c = chr(i)
  c_s = copy.copy(s)
  cnt = 0
  for j in range(len(s)-1):
    cnt += 1
    for k in range(len(s)-1-j):
      if (c_s[k] == c or c_s[k+1] == c):
        c_s[k] = c
    l = len(s)-1-j
    if (c*l == "".join(c_s[:l])):
      ans = min(ans,cnt)
print(ans)

Submission status: Accepted

Solution code:
s = raw_input()

ans = len(s)
for c in range(26):
  c = chr(ord("a")+c)
  t = s
  n = 0
  while True:
    if t==c*len(t):
      break
    x = ""
    for i in range(len(t)-1):
      if t[i+1]==c:
        x += t[i+1]
      else:
        x += t[i]
    t = x
    n += 1
  ans = min(ans, n)
print ans

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

s = read().decode().rstrip()

ans = len(s)


for i in range(97, 97+26):
    c = chr(i)
    cnt = 0
    acc = s
    while len(acc) != acc.count(c):
        s_ = ""
        for j, _ in enumerate(acc):
            if j == len(acc)-1:
                break
            if acc[j:j+2].count(c) > 0:
                s_ += c
            else:
                s_ += acc[j]
        cnt += 1
        acc = s_
    ans = min(ans, cnt)

print(ans)

Submission status: Accepted

Solution code:
s = input()
c = 'abcdefghijklmnopqrstuvwxyz'

ans = len(s)

if len(set(s)) == 1:
    print("0")
    exit(0)

for j in range(len(c)):
    cnt = 0
    t = ''
    if c[j] in s:
        m = s
        while(1):
            t = ''
            for i in range(len(m)-1):
                if m[i] == c[j]:
                    t = t + c[j]
                elif m[i+1] == c[j]:
                    t = t + c[j]
                else:
                    t = t + m[i]
            m = t
            cnt += 1
            if len(set(t)) == 1:
                break
        ans = min(cnt, ans)

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

def solve(s):
    cs = Counter(s)
    m = len(s)
    for c in cs:
        news = list(s)
        size = len(s)
        count = 0
        while len(Counter(news[:size])) > 1:
            for i in range(size - 1):
                if news[i] != c[0] and news[i + 1] == c[0]:
                    news[i] = c[0]
            size -= 1
            count += 1
        if count < m:
            m = count
    return m

_s = input()
print(solve(_s))

Submission status: Accepted

Solution code:
from collections import Counter

def solve(s):
    cs = Counter(s).most_common()
    m = len(s)
    for c in cs:
        news = list(s)
        size = len(s)
        count = 0
        while len(Counter(news[:size]).most_common()) > 1:
            for i in range(size - 1):
                if news[i] != c[0] and news[i + 1] == c[0]:
                    news[i] = c[0]
            size -= 1
            count += 1
        if count < m:
            m = count
    return m

_s = input()
print(solve(_s))

Submission status: Accepted

Solution code:
from collections import Counter
s = input()
sc = Counter(s)
ans = float('inf')
for i in sc.keys():
    s2 = s
    while len(Counter(s2).keys()) != 1:
        for j in range(len(s2)-1):
            if s2[j+1] == i:
                s2 = s2[:j] + s2[j+1]+ s2[j+1:]
        s2 = s2[:-1]
    ans = min(ans,len(s)-len(s2))
print(ans)

Submission status: Accepted

Solution code:
S = input()

N = len(S)
dist = [[N for i in range(N)] for j in range(26)]
dist[ord(S[-1]) - 97][-1] = 0
for i in range(26):
    for j in reversed(range(N - 1)):
        if ord(S[j]) == 97 + i:
            dist[i][j] = 0
        else:
            dist[i][j] = dist[i][j + 1] + 1

ans = N - 1
for i in range(26):
    for j in range(1, N + 1):
        ans = min(ans, max(N - j, max(dist[i][0:j])))
print(ans)