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 = input()
ans = 1000
for c in 'abcdefghijklmnopqrstuvwxyz':
    t = s
    if c not in t:
        continue
    tmp = 0
    while len(set(t)) > 1:
        res = ''
        for i in range(len(t)-1):
            if t[i] == c or t[i+1] == c:
                res += c
            else:
                res += '0'
        t = res
        tmp += 1
    ans = min(ans, tmp)

print(ans)

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

sys.setrecursionlimit(10 ** 7)

ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

from collections import Counter

s = ns()
c = Counter(s)

ans = inf
for i in c.keys():
    ns = i + s + i
    j = 1
    cur = 0
    res = 0
    while j < len(ns):
        if ns[j] == i:
            res = max(res, j - cur - 1)
            cur = j
        j += 1
    ans = min(ans, res)

print(ans)

Submission status: Accepted

Solution code:
s = input()
n = len(s)
chk = [[0] for i in range(n)]
cc = set()
d = {}
cnt = 0
for i,si in enumerate(s):
  if not si in cc:
    d[si] = cnt
    cnt += 1
    cc.add(si)
  chk[d[si]].append(i+1)
#print(chk)
ans = n//2
for j in chk:
  p = 0
  j = j + [n+1]
  for k in range(len(j)-1):
    p = max(p,j[k+1]-j[k]-1)
    if p >= ans:
      break
    if k == len(j)-2:
      ans = min(p,ans)
print(ans)

Submission status: Accepted

Solution code:
def solve():
    S = input()
    ans = float('inf')

    for ch in set(S):
        dist = list(map(lambda x: len(x), S.split(ch)))
        ans = min(max(dist),ans)

    print(ans)

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import collections
s = input()
dic = collections.defaultdict(list)
for i in range(len(s)):
    dic[s[i]].append(i)
ans = 10000
for key in dic.keys():
    ls = [-1] + dic[key] + [len(s)]
    diff = []
    for i in range(len(ls)-1):
        diff.append(ls[i+1] - ls[i]-1)
    #print(diff)
    ans = min(max(diff),ans)
print(ans)

Submission status: Accepted

Solution code:
s = input()
a_z = [chr(i) for i in range(97, 97 + 26)]
ans = len(s)
for alp in a_z:
    cnt = 0
    step = 0
    for char in s:
        if char == alp:
            cnt = max(cnt, step)
            step = 0
        else:
            step += 1
    ans = min(max(cnt, step), ans)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter, deque

s = input()
n = len(s)
d = deque([{char} for char in s])
c = Counter(s)

if s[0] * n == s:
    print(0)
    exit()

for i in range(1, n):
    for j in range(n - i):
        char_j = s[j]
        char_right = s[j + i]
        set_j = d[j]
        if char_right not in set_j:
            set_j.add(char_right)
            c[char_right] += 1
    rightest = d.pop()
    for char in rightest:
        c[char] -= 1
    if max(c.values()) == n - i:
        print(i)
        exit()
print('oops, something wrong')

Submission status: Accepted

Solution code:
s=input()
ans=1000
import string
ss=string.ascii_lowercase
for c in list(ss):
    temp=s
    p=0
    nex=""
    while len(set(list(temp)))!=1:
        for i in range(len(temp)-1):
            if c in (temp[i],temp[i+1]):
                nex+=c
            else:nex+=temp[i]
        p+=1
        temp=nex
        nex=""
    ans=min(p,ans)
print(ans)

Submission status: Accepted

Solution code:
s = input()
se = set(list(s))
# print('se', se)

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


def f(v):
    s_list = s.split(v)
    s_list = [len(i) for i in s_list if i]
    return max(s_list)


ans = 1000
for i in se:
    v = f(i)
    ans = min(ans, v)
print(ans)
# serval 6,1
# srvvl  5,2
#        4,3

#  → svvv → vvv

# jackal
# aacaa

# jaajjj
# aaajj
# aaaj
# aaa

# jajjjjjjjajjjjjjjjjj
# aajjjjjjaajjjjjjjjj
# aajjjjjaaajjjjjjjj
# aajjjjaaaajjjjjjj
# ...

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

arr=list(ss())
mat=list(Counter(arr))
ans=10**100
for i in mat:
    tmp=deepcopy(arr)
    p=0
    while len(Counter(tmp))!=1:
        aa=[]
        for j in range(len(tmp)-1):
            if tmp[j] ==i or tmp[j+1]==i:
                aa.append(i)
            else:
                aa.append(tmp[j])
        tmp=deepcopy(aa)
        p+=1
    ans=min(ans,p)
print(ans)

Submission status: Accepted

Solution code:
S = input()
alphabet = [[-1] for a in range(26)]
for n,c in enumerate(S):
    s = ord(c) - 97
    alphabet[s].append(n)

ans = float("inf")
for A in alphabet:
    A.append(len(S))
    cur = max([abs(A[l] - A[l+1]) for l in range(len(A)-1)])
    ans = min(cur,ans)
print(max(ans -1 , 0))

Submission status: Accepted

Solution code:
s = list(input())
ans = len(s)
for c in set(s):
    dist = 0
    dists = []
    for d in s:
        if c != d:
            dist += 1
        else:
            dists.append(dist)
            dist = 0
    ans = min(ans, max(dists + [dist]))
print(ans)

Submission status: Accepted

Solution code:
s = list(input())
ans = len(s)
for c in s:
    dist = 0
    dists = []
    for d in s:
        if c != d:
            dist += 1
        else:
            dists.append(dist)
            dist = 0
    ans = min(ans, max(dists + [dist]))
print(ans)

Submission status: Accepted

Solution code:
from copy import copy
from string import ascii_lowercase


s = list(input())
ans = len(s)
for c in ascii_lowercase:
    t = copy(s)
    for i in range(len(s), 0, -1):
        if t == [c] * i:
            ans = min(ans, len(s) - i)
            break
        u = copy(t[:-1])
        for j in range(i - 1):
            if t[j] == c or t[j + 1] == c:
                u[j] = c
            else:
                u[j] = t[j]
        t = u
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# sys.stdin.readline()
import numpy as np
import math
s = input()
def chang(st,string):
    cnt = 0    
    while True:
        if len(set(list(string))) == 1:
            
            return cnt
            break
        
        else:
            stn = ""
            for i in range(len(string)-1):
                if string[i]==st or string[i+1] ==st:
                    stn+=st
                else:
                    stn+=string[i]
            else:
                cnt+=1
                string = stn
                
d = set(list(s))
ans = 100
for i in d:
    ans = min(ans , chang(i,s))
print(ans)

Submission status: Accepted

Solution code:
s=input()
ans=1000
for i in range(26):
  if chr(97+i) not in s:continue
  S=s
  cnt=0
  while(len(set(S)))!=1:
    cnt+=1
    t=""
    for j in range(len(S)-1):
      if S[j] == chr(97+i) or S[j+1] == chr(97+i):
        t+=chr(97+i)
      else:
        t+=S[j]
    S=t
  ans=min(cnt,ans)
print(ans)

Submission status: Accepted

Solution code:
S=list(input())
ans=103
for i in range(26):
  A=[-1]
  for j in range(len(S)):
    if S[j]==chr(i+97):
      A.append(j)
  A.append(j+1)
  a=0
  for j in range(len(A)-1):
    a=max(a,A[j+1]-A[j]-1)
  if a<ans:
    ans=a
print(ans)

Submission status: Accepted

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

#文字ごとに全探索
#aaa,x,aa,x,aaaaのように、xで文字列を分解したときの最長要素数が答え
char_li = set(s)
ans=float('inf')
for char in char_li:
    ans=min(ans,max([len(l) for l in s.split(char)]))

print(ans)

Submission status: Accepted

Solution code:
# A - Shrinking

s = input()
N = len(s)
ans = 1000

for i in range(N):
    count = count_max = 0
    for j in range(1,N+1):
        if s[i]==s[N-j]:
            count = 0
        else:
            count += 1
        count_max = max(count, count_max)
    ans = min(count_max, ans)

print(ans)

Submission status: Accepted

Solution code:
def check(S):
    cond=True
    for i in range(len(S)-1):
        if S[i]!=S[i+1]:
            cond=False
    return cond

def shrink(t,s):
    tt=[]
    for i in range(len(t)-1):
        if t[i+1]==s:
            tt.append(t[i+1])
        else:
            tt.append(t[i])
    return tt

S=input()
N=len(S)
ans=N

for i in range(N):
    s=S[i]
    t=list(S)
    for j in range(N):
        if check(t):
            break
        t=shrink(t,s)
    ans=min(ans,j)
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(10 ** 7)

from copy import deepcopy


def moji_list(a, b):
    # アルファベット小文字 → (97, 123)
    # アルファベット大文字 → (65, 91)
    # 半角数字 → (48, 58)
    # ひらがな → (12353, 12436)
    # カタカナ → (12449, 12532+1)
    # 全角数字 → (65296, 65306)
    return [chr(i) for i in range(a, b)]


s = list(read().rstrip().decode())
ans = float('inf')
for al in moji_list(97, 123):
    cnt = 0
    check_s = deepcopy(s)
    while len(set(check_s)) > 1:
        cnt += 1
        ss = []
        for bf, af in zip(check_s, check_s[1:]):
            ss.append(af if af == al else bf)
        check_s = ss
    ans = min(ans, cnt)
print(ans)

Submission status: Accepted

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

a = ord('a')

check = [[False for j in range(26)] for i in range(N)]

for i in range(N):
    check[i][ord(s[i])-a] = True

for j in range(26):
    same = True
    for i in range(N):
        if not check[i][j]:
            same = False
    if same:
        print(0)
        exit()

ans = 1
while True:
    for i in range(N-ans):
        for j in range(26):
            if check[i][j] or check[i+1][j]:
                check[i][j] = True
    for j in range(26):
        same = True
        for i in range(N-ans):
            if not check[i][j]:
                same = False
        if same:
            print(ans)
            exit()
    ans += 1

Submission status: Accepted

Solution code:
import numpy as np

S = input()
ss = set(S) 
mx = len(S)
for s in ss:
    m = [i for i, si in enumerate(S) if si == s ]
    m.insert(0, -1)
    m.append(len(S))
    mx = min(mx, max(np.diff(m)-1))

print(mx)

Submission status: Accepted

Solution code:
s = input()
ans = []
for i in s:
    cnt = 0
    t = s
    while t.count(i) < len(t):
        cnt += 1
        tmp = ''
        for j in range(len(t)-1):
            if t[j] != i and t[j+1] != i:
                tmp = tmp+t[j]
            else:
                tmp = tmp+i
        t = tmp
    ans.append(cnt)
print(min(ans))

Submission status: Accepted

Solution code:
s = input()
lon = len(s)
lis = list(s)
ans = 10000
#print(lis)
for i in s:
    cnt=0
    lef = []

    ind = [j for j, x in enumerate(lis) if x==i]
    pre=0
    for ma in ind:
        lef.append(ma-pre)
        pre = ma+1
    lef.append(lon-ma-1)
    ans_ten = max(lef)
    ans = min(ans,ans_ten)

print(ans)

Submission status: Accepted

Solution code:
S = input()

ans = len(S)
for i, s in enumerate(S[::-1]):
    #print(i, s, S[:-(i+1)])
    m, n = 0, 0
    for j, c in enumerate(S[:-(i+1)][::-1]):
        if c == s:
            n = max(m, n)
            m = 0
        else:
            m += 1
    n = max(m, n)
    ans = min(ans, max(i, n))
    #print(m, n, i, ans)
print(ans)

Submission status: Accepted

Solution code:
S = input()
alphabets = [chr(ord('a') + x) for x in range(26)]

ans = 10000
for c in alphabets:
    ans = min(ans, max([len(f) for f in S.split(c)]))
print(ans)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import Counter
from operator import itemgetter
def main():
    s=input()
    len_s=len(s)
    sc=list(Counter(s).items())
    sc.sort(key=itemgetter(1),reverse=True)
    if sc[0][1]==1:
        print(len_s//2)
        sys.exit()
    ans=100
    for scc in sc:
        dp=[[False]*len_s for _ in range(len_s)]
        for i,ss in enumerate(s):
            if ss==scc[0]:
                dp[0][i]=True
        dp[1][-1]=True
        for sosa in range(len_s):
            if all(dp[sosa]):
                ans=min(ans,sosa)
                break
            for i in range(len_s-1,-1,-1):
                if dp[sosa][i]==True:
                    dp[sosa+1][i]=True
                    if i>0:dp[sosa+1][i-1]=True
    print(ans)

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
s=input()
ans=10**5
for a in set(list(s)):
  t=s
  for i in range(len(s)):
    u=''
    for j in range(len(t)-1):
      if t[j+1]==a or t[j]==a:
        u+=a
      else:
        u+=t[j+1]
    t=u
    if len(set(list(u)))==1:
      ans=min(ans,i+1)
      break
if len(set(list(s)))==1:
  ans=0
print(ans)

Submission status: Accepted

Solution code:
s=list(input())
#　文字の種類ごとにどのタイミングででたか格納
x=[[] for i in range(26)]
n=len(s)
for i in range(n):
    v=ord(s[i])-97
    x[v].append(i)

for i in range(26):
    x[i]=[-1]+x[i]+[n]


ans=float('inf')
for i in range(26):
    cnt=0
    for j in range(1,len(x[i])):
        cnt=max(x[i][j]-x[i][j-1]-1,cnt)
    ans=min(cnt,ans)
print(ans)

Submission status: Accepted

Solution code:
INF = 10 ** 8
def main():
    s = input()
    word = list(set(s))
    output = INF
    for c in word:
        t = s
        cnt = 0
        while len(set(t)) > 1:
            l = []
            for i in range(len(t)-1):
                if t[i] == c or t[i+1] == c: l.append(c)
                else: l.append(t[i])
            t = ''.join(l)
            cnt += 1
        output = min(output, cnt)
    print(output)
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S = input()
ans = len(S)
for c in set(S):
    maxi = count = 0
    for s in S:
        if s != c:
            count += 1
        else:
            maxi = max(maxi, count)
            count = 0
    maxi = max(maxi, count)
    ans = min(ans, maxi)
print(ans)

Submission status: Accepted

Solution code:
import math
import fractions
#import sys
#input = sys.stdin.readline

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

def ValueToBits(x,digit):
    res = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        res[i]=now%2
        now = now >> 1
    return res

def BitsToValue(arr):
    n = len(arr)
    ans = 0
    for i in range(n):
        ans+= arr[i] * 2**i
    return ans

def ZipArray(a):
    aa = [[a[i],i]for i in range(n)]

    aa.sort(key = lambda x : x[0])
    for i in range(n):
        aa[i][0]=i+1
    aa.sort(key = lambda x : x[1])
    b=[aa[i][0] for i in range(len(a))]
    return b

def ValueToArray10(x, digit):
    ans = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        ans[digit-i-1] = now%10
        now = now //10
    return ans

def Zeros(a,b):
    if(b<=-1):
        return [0 for i in range(a)]
    else:
        return [[0 for i in range(b)] for i in range(a)]

class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i


'''
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 2
N = 10 ** 6 + 2
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

'''

def rln(s):
    return range(len(s))

#a = list(map(int, input().split()))

#################################################
#################################################
#################################################
#################################################



ss = input()
n = len(ss)

ans = 99999999999
ok = 1

for i in range(26):
    s = ss
    letter = chr(ord('a')+i)
    while(len(s)>=1):
        ok = 1
        for i in s:
            ok *= 1 if i==letter else 0
        if(ok==1):
            break
        text = ""
        for j in range(len(s)-1):
            if(s[j+1] == letter):
                text+= letter
            else:
                text+=s[j]
        s = text
    ans = min(ans,n-len(s))

print(ans)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/agc016/tasks/agc016_a

from collections import Counter
s = input()
c = Counter(s)
ans = float('inf')
for k, v in c.items():
    # if v < 2:
    #     continue
    t = [-1]
    for i in range(len(s)):
        if s[i] == k:
            t.append(i)
    t.append(len(s))
    temp = []
    for j in range(len(t) - 1):
       temp.append(t[j + 1] - t[j] - 1)
    ans = min(ans, max(temp))
print(ans)

Submission status: Accepted

Solution code:
ans = {}
t = input()
for i in range(len(t)):
    if t[i] not in ans:
        ans[t[i]] = i + 1

    for j in range(i+1, len(t)):
        if t[i] == t[j]:
            if t[i] in ans:
                ans[t[i]] = max(ans[t[i]], abs(j - i))
            else:
                ans[t[i]] = abs(j - i)
            
            break
    
        if j == len(t) - 1:
            ans[t[i]] = max(ans[t[i]], abs(j + 1 - i))


print(min(ans.values()) - 1)

Submission status: Accepted

Solution code:
S = input()
result = 10**4
for c in set(S):
    work = []
    temp = 0
    for s in S:
        if s == c:
            work.append(temp)
            temp = 0
        else:
            temp += 1
    work.append(temp)
    result = min(result, max(work))
print(result)

Submission status: Accepted

Solution code:
import numpy as np
import functools
import math
import scipy
import fractions
import itertools

def rle(s, tar):
    ans = []
    hoge = 0
    for i in range(len(s)):
        if s[i] != tar:
            hoge += 1
        else:
            ans.append(hoge)
            hoge = 0
    ans.append(hoge)
    return ans

def solve():
    s = list(input())
    ele = sorted(list(set(s)))
    le = len(ele)
    n = len(s)
    cost = [0]*le
    for i in range(le):
        cost[i] = max(rle(s, ele[i]))
    print(min(cost))
    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
from collections import Counter
s=input()
c=Counter(s).most_common()
ans=10**10
top=c[0][1]
for k,x in c:
    N=len(s)
    m=k
    cnt=0
    tmp=s
    for i in range(100):
        cn=Counter(tmp).most_common()
        if(cn[0][1]==len(tmp)):
            ans=min(ans,cnt)
            break
        l=''
        for j in range(N-1):
            if(tmp[j]==m or tmp[j+1]==m):
                l+=m
            else:
                l+=s[j]
        tmp=l
        cnt+=1
        N-=1
print(ans)

Submission status: Accepted

Solution code:
s = '_'+input()
l = len(s)
dist_lst = [0]*26
idx_lst = [0]*26
for i in range(1,l):
    c = ord(s[i])%26
    dist_lst[c] = max(dist_lst[c],i-idx_lst[c])
    idx_lst[c] = i
for c in range(26):
    dist_lst[c] = max(dist_lst[c],l-idx_lst[c])

print(min(dist_lst)-1)

Submission status: Accepted

Solution code:
s = input()
ans = len(s)
for i in range(26):
    c0 = chr(ord('a')+i)
    tmp = 0
    ma = 0
    for c in s:
        if c==c0:
            ma = max(ma, tmp)
            tmp = 0
        else:
            tmp += 1
    ma = max(ma, tmp)
    ans = min(ans, ma)
print(ans)

Submission status: Accepted

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

b = Counter(s)
c = list(b)
ssc = []
for i in range(n):
    ssc.append(s[i])

ans = 114514191933-4

if len(b) == 1:
    print(0)
    sys.exit()

for l in c:
    a = 0
    s2 = ssc
    ss = []
    for i in range(n-1):
        k = len(s2)
        for j in range(k-1):
            if s2[j] == l or s2[j+1] == l:
                ss.append(l)
            else:
                ss.append(s2[j+1])
                
        d = Counter(ss)
        if d[l] == k-1:
            ans = min(n-len(ss),ans)
            break
            
        s2 = ss
        ss = []
    
print(ans)

Submission status: Accepted

Solution code:
string=list(input())
n=len(string)
s=set()
ans=10**4

for target in string:
    if target in s:
        continue
    
    cnt=0
    tmp_max=0
    for i in string:
        if i!=target:
            cnt+=1
        else:
            tmp_max=max(tmp_max,cnt)
            cnt=0
    tmp_max=max(tmp_max,cnt)
    ans=min(ans,tmp_max)
        
print(ans)

Submission status: Accepted

Solution code:
s = list(input())

A = list(set(s))

B = []

for i in range(len(A)):
    C = []
    for j in range(len(s)):
        if A[i] == s[j]:
            C.append(j)
    C.append(-1)
    C.append(len(s))
    C = sorted(C)
    D = []
    for i in range(len(C) - 1):
        D.append(C[i+1] - C[i] - 1)
    
    B.append(max(D))
print(min(B))

Submission status: Accepted

Solution code:
s = input()
n = len(s)

chars = [chr(i) for i in range(97, 97+26)]

res = 1 << 30
for c in chars:
	cnt = 0
	t = s
	while len(set(list(t))) > 1:
		u = []
		for i in range(len(t) - 1):
			if t[i] == c or t[i + 1] == c:
				u.append(c)
			else:
				u.append(t[i])
		cnt += 1
		t = ''.join(u)
	res = min(res, cnt)

print(res)

Submission status: Accepted

Solution code:
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

s = ins()
mx = [0]*26
cur = [0]*26
for c in s:
    for j in range(26):
        d = chr(ord('a')+j)
        if c==d:
            cur[j] = 0
        else:
            cur[j] += 1
            mx[j] = max(mx[j],cur[j])
    #ddprint(f"c {c}")
    #ddprint(cur)
    #ddprint(mx)
print(min(mx))

Submission status: Accepted

Solution code:
from _collections import defaultdict
s=input()
X=defaultdict(int)
l=len(s)
for i in s:
    X[i]+=1
ANS=[]
for key in X.keys():
    a=0
    cnt=0
    for i in range(l):
        if s[i]==key:
            a=max(a,cnt)
            cnt=0
        else:
            cnt+=1
    ANS.append(max(a,l-s.rfind(key)-1))
print(min(ANS))

Submission status: Accepted

Solution code:
s = list(input())

S = set(s)
res = 0
for k in S:
	t = s[::]
	while len(set(t)) != 1:
		t.pop()
		for i in range(len(t) - 1):
			if t[i] == k or t[i + 1] == k:
				t[i] = k
			else:
				t[i] = t[i]
	ma = len(t)
	res = max(res, len(t))

print(len(s) - res)

Submission status: Accepted

Solution code:
from collections import Counter
from copy import deepcopy
def main():
    s = list( input())
    N = len(s)
    C = Counter(s)
    ans = 100
    for i in range(26):
        S = deepcopy(s)
        a = chr( ord("a") + i)
        t = N
        c = C[a]
        if c == 0:
            continue
        # print(a, t, c)
        while t > c:
            t -= 1
            for j in range(t):
                if S[j] != a and S[j+1] == a:
                    S[j] = a
                    c += 1
            if S[t] == a:
                c -= 1
        # print( a, N-t)
        if N-t < ans:
            ans = N-t
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S = str(input())
ans = 10**18
alp = list(set(S))

for i in alp:
  count = 0
  eff = 0
  for j in S:
    if i != j:
      count += 1
    else:
      eff = max(eff,count)
      count = 0
  eff = max(eff, count)
  ans = min(ans, eff)
  
print(ans)

Submission status: Accepted

Solution code:
s=list(input())
cnt=100
for i in range(26):
    x=chr(97+i)
    if x not in s:
        continue
    tmp=0
    dummy=s+[]
    while len(set(dummy))>1:
        tmp+=1
        newdum=[]
        for j in range(len(dummy)-1):
            if dummy[j]==x or dummy[j+1]==x:
                newdum.append(x)
            else:
                newdum.append(dummy[j])
        dummy=[]+newdum
    cnt=min(cnt,tmp)
print(cnt)

Submission status: Accepted

Solution code:
s=list(input())
cnt=100
for i in range(26):
    x=chr(97+i)
    if x not in s:
        continue
    tmp=0
    dummy=s+[]
    while len(set(dummy))>1:
        tmp+=1
        newdum=[]
        for j in range(len(dummy)-1):
            if dummy[j]==x or dummy[j+1]==x:
                newdum.append(x)
            else:
                newdum.append(dummy[j])
        dummy=[]+newdum
    cnt=min(cnt,tmp)
print(cnt)

Submission status: Accepted

Solution code:
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from fractions import gcd
from math import factorial, sqrt, ceil
from functools import lru_cache, reduce
INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)

# UnionFind
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# ダイクストラ
def dijkstra_heap(s, edge, n):
    #始点sから各頂点への最短距離
    d = [10**20] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

# 素因数分解
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

# 2数の最小公倍数
def lcm(x, y):
    return (x * y) // gcd(x, y)

# リストの要素の最小公倍数
def lcm_list(numbers):
    return reduce(lcm, numbers, 1)

# リストの要素の最大公約数
def gcd_list(numbers):
    return reduce(gcd, numbers)

# 素数判定
def is_prime(n):
    if n <= 1:
        return False
    p = 2
    while True:
        if p ** 2 > n:
            break
        if n % p == 0:
            return False
        p += 1
    return True


# limit以下の素数を列挙
def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []

    while True:
        prime = min(A)
        
        if prime > sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P

# 同じものを含む順列
def permutation_with_duplicates(L):

    if L == []:
        return [[]]

    else:
        ret = []

        # set（集合）型で重複を削除、ソート
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutation_with_duplicates(data):
                ret.append([i] + j)

        return ret


# ここから書き始める
s = input()
x = "abcdefghijklmnopqrstuvwxyz"
ans = INF
for i in x:
    s2 = s
    s3 = ""
    cnt = 0
    while len(set([k for k in s2])) > 1:
        for j in range(len(s2) - 1):
            if s2[j + 1] == i:
                s3 += i
            else:
                s3 += s2[j]
        cnt += 1
        s2 = s3
        # print("s2 =", s2)
        s3 = ""
    ans = min(ans, cnt)
print(ans)

Submission status: Accepted

Solution code:
import collections
s = input()
ss = set(s)
# print(ss)
ans = float('inf')
# cs = collections.Counter(s)
for c in ss:
    cnt = 0
    back = 0
    # print(c)
    for si in s:
        if c != si:
            cnt += 1
        else:
            back = max(back, cnt)
            cnt = 0
    back = max(back, cnt)
    ans = min(ans, back)
    # print(ans, back)
print(ans)
# for i in cs:
#     print(i)