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:
import sys
sys.setrecursionlimit(100000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *
from math import *
from copy import *

s = input()
atoz = [chr(i) for i in range(97,97+26)]
ans = 10000
for c in atoz:
    cnt = 0
    t = s
    while 1:
        if len(Counter(t)) == 1:
            ans = min(ans, cnt)
            break
        td = ''
        for i in range(len(t)-1):
            if t[i] == c or t[i+1] == c:
                td += c
            else:
                td += t[i]
        t = td
        cnt += 1
print(ans)

Submission status: Accepted

Solution code:
s = input()
s_set = set(s)
def shrink(r,s):
    s = list(s)

    t = 0
    while True:

        if len(set(s)) == 1:
            break
        else:
            t += 1

        new_s = []
        for i in range(len(s)-1):
            if s[i] == r or s[i+1] == r:
                new_s.append(r)
            else:
                new_s.append(s[i])
        s = new_s
    return t

ans_list = [ shrink(i,s) for i in s_set]
print(min(ans_list))

Submission status: Accepted

Solution code:
# AGC016A - Shrinking
# exhaustive search
def main():
    S = input().rstrip()
    T = set(S)
    ans = float("inf")
    for t in T:
        cnt = 0
        x = S
        while len(set(x)) != 1:
            cnt += 1
            tmp = ""
            for i in range(len(x) - 1):
                tmp += t if x[i] == t else x[i + 1]
            x = tmp
        ans = min(ans, cnt)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from sys import stdin
s= (stdin.readline().rstrip())
f = lambda a, b: abs(a-b-1)
diff = lambda ls: map(f, ls[1:], ls)

ans = 100
for i in set(s):
    ans = min(ans,max([len(j) for j in s.split(i)]))
print(ans)

Submission status: Accepted

Solution code:
s=input()
print(min(max(map(len,s.split(t)))for t in s))

Submission status: Accepted

Solution code:
S=list(input())
nmin=len(S)
for c in set(S):
    arr=S[:]
    for n in range(len(S)):
        if all([cc==c for cc in arr]):
            nmin=min(nmin, n)
            break
        for i in range(len(arr)-1):
            if arr[i]!=c and arr[i+1]==c:
                arr[i]=c
        arr=arr[:-1]
print(nmin)

Submission status: Accepted

Solution code:
s=input()
sets=set(s)
ans=[]
for i in sets:
    cnt=0
    temp=list(s)
    if len(set(temp))==1:
        ans.append(0)
    else:
        while(True):
            temp2=[]
            for j in range(len(temp)-1):
                if temp[j]==i or temp[j+1]==i:
                    temp2.append(i)
                else:
                    temp2.append(None)
            cnt+=1
            if len(set(temp2))==1:
                ans.append(cnt)
                break
            temp=temp2
print(min(ans))

Submission status: Accepted

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

dic = {}
for i, s in enumerate(S):
    if not s in dic.keys():
        dic[s] = [i]
    else:
        dic[s].append(i)

all = True
for i in range(N-1):
    if S[i] != S[i+1]:
        all = False


ans = N

for m in dic.keys():
    if all:
        ans = 0
        break
    q = list(S)
    c = 0
    allm = False
    while not allm:
        n = len(q)
        qq = []
        allm = True
        for i in range(n-1):
            if q[i] == m or q[i+1] == m:
                qq.append(m)
            else:
                allm = False
                qq.append(q[i])
        c += 1
        q = qq
    ans = min(ans, c)

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
s = input()
di = defaultdict(int)
ans = float('inf')
for i in s:
    di[i]+=1
if(len(di)==1):
    print(0)
else:
    for i in di:
        cnt = 0
        now_s = s
        for _ in range(len(s)):
            next_s = ''
            flag = True
            for ind,ss in enumerate(now_s[:-1]):
                if(ss==i or now_s[ind+1]==i):
                    next_s+=i
                else:
                    flag = False
                    next_s+=ss
            cnt+=1
            if(flag):
                break
            else:
                now_s=next_s
        ans = min(cnt,ans)
    print(ans)

Submission status: Accepted

Solution code:
s = input()
print( min(max(len(j) for j in s.split(i)) for i in set(s)) )

Submission status: Accepted

Solution code:
S=input()

setS=set(S)

ans=101
for s in setS:
    c=max(map(len,S.split(s)))
    ans=min(ans,c)

print(ans)

Submission status: Accepted

Solution code:
s=input()
a=list(set(s))
l=[]
for i in a:
  b=s+i
  m=0
  c=0
  for j in range(len(b)):
    c+=1
    if b[j]==i:
      if c>m:
        m=c
      c=0
  l.append(m)
print(min(l)-1)

Submission status: Accepted

Solution code:
S  = [inp for inp in input()]
dic = {}
for s in S:
    if s in dic:
        dic[s] += 1
    else:
        dic[s] = 1
ans = float('inf')
N = len(S)
for alphabet in dic.keys():
    tempN = N
    copyS = S.copy()
    count = 0
    while True:
        if copyS.count(alphabet) == tempN:
            break
        count += 1
        for i in range(tempN-1):
            copyS[i] = copyS[i] if copyS[i] == alphabet else copyS[i+1]
        copyS.pop(-1)
        tempN -= 1
    if count < ans:
        ans = count
print(ans)

Submission status: Accepted

Solution code:
s=input()
ans=len(s)
for i in "abcdefghijklmnopqrstuvwxyz":
  t=a=0
  for j in s:
    if j==i:a=max(a,t);t=0
    else:t+=1
  a=max(a,t)
  ans=min(ans,a)
print(ans)

Submission status: Accepted

Solution code:
s = input()

c = 0
while True:
    l = len(s)-c
    key = list(s[:c+1])
    for i in range(1,l):
        new_key = []
        for k in key:
            if k in s[i:i+c+1]:
                new_key.append(k)
        key = new_key
        if key == []:
            break
    if key != []:
        break
    c += 1
print(c)

Submission status: Accepted

Solution code:
def main():
    s = input()
    prev = [0] * 26
    space = [0] * 26
    for i, c in enumerate(s):
        x = ord(c) - ord('a')
        space[x] = max(space[x], i - prev[x])
        prev[x] = i + 1
    for i in range(26):
        space[i] = max(space[i], len(s) - prev[i])
    ans = min(space)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
ans = 9999
S = input()
cands = list(set(list(S)))
for c in cands:
    temp = max(list(map(len,S.split((c)))))
    ans = min(ans,temp)
print(ans)

Submission status: Accepted

Solution code:
def convert(t, c):
    res = 0
    while len(set(t)) > 1:
        n = len(t)
        s = list(t)[:-1]
        for i in range(n-1):
            if (t[i] == c) or (t[i+1] == c):
                s[i] = c
        res += 1
        t = "".join(s)
    return res
def solve(t):
    ans = len(t)
    for c in set(t):
        ans = min(ans, convert(t, c))
    return ans
s = input()
print(solve(s))

Submission status: Accepted

Solution code:
import sys
from collections import Counter, deque, defaultdict
from itertools import accumulate, permutations, combinations, takewhile, compress, cycle
from functools import reduce
from math import ceil, floor, log10, log2, factorial
from pprint import pprint

sys.setrecursionlimit(1000000)
# MOD = 10 ** 9 + 7
# N = int(input())
# A = [int(x) for x in input().split()]
# V = [[0] * 100 for _ in range(100)]
# A = [int(input()) for _ in range(N)]

S = input()


def f(c):
    s = list(S)
    cnt = 0
    while s.count(c) != len(s):
        newS = [None] * (len(s) - 1)
        for i in range(len(s) - 1):
            if s[i] == c or s[i + 1] == c:
                newS[i] = c
            else:
                newS[i] = s[i]
        s = newS
        cnt += 1
    return cnt


ans = float('inf')
for s in set(S):
    ans = min(ans, f(s))

print(ans)

Submission status: Accepted

Solution code:
def shrink(s, char):
    while len(set(s)) > 1:
        temp = ''
        for i in range(len(s) - 1):
            if s[i] == char or s[i+1] == char:
                temp += char
            else:
                temp += s[i]
        s = temp
    return s

S = input()
ans = 10 ** 9
for c in set(S):
    ans = min(ans, len(S) - len(shrink(S, c)))
print(ans)

Submission status: Accepted

Solution code:

s = input()

strs = set(s)


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

ans = 1000
tmp = 0
for moji in strs:
  s1 = s
  s2 = [""] * (len(s)-1)
  tmp = 0
  flag = True
  while flag == True:
    for i in range(len(s2)):
      if s1[i] == moji or s1[i+1] == moji:
        s2[i] = moji
      else:
        s2[i] = s1[i]
      
    tmp += 1
    s1 = s2
    s2 = [""] * (len(s1)-1)

    if len(set(s1)) == 1:
      flag = False
  ans = min(ans, tmp)   
 
print(ans)

Submission status: Accepted

Solution code:
S = input()
l = len(S)
D = dict()
for i in range(l):
    if S[i] not in D:
        D[S[i]] = []
    D[S[i]].append(i)
res = 1000
for v in D.values():
    v = [-1]+v+[l]
    lv = len(v)
    tmp = 0
    for i in range(1,lv):
        tmp = max(tmp,v[i]-v[i-1]-1)
    res = min(res,tmp)
print(res)

Submission status: Accepted

Solution code:
s=input()
n=len(s)
ans=100000
l=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
s+="a"
for i in l:
    s=s[0:n]+i
    c=0
    p=0
    for j in range(n+1):
        if s[j]==i:
            p=max(c,p)
            c=0
        else:
            c+=1
    ans=min(ans,p)
print(ans)

Submission status: Accepted

Solution code:
s = input().rstrip()
chars = set(s)

ans = 1000
for char in chars:
    max_left = 0
    val = 0
    for i, c in enumerate(s):
        if c is not char:
            max_left += 1
        else:
            val = max(val, max_left)
            max_left = 0
    val = max(val, max_left)
    ans = min(ans, val)
print(ans)

Submission status: Accepted

Solution code:
s = str(input())
a = list(s)
alpha = set(s)
temp = ""
temp2 = ""
ans = 0
answers = []
if(len(alpha) > 1):
    for letter in alpha:
        temp = s
        ans = 0;
        while(True):
            ans += 1
            for i in range(len(temp)-1):
                if temp[i] == letter or temp[i+1] == letter:
                    temp2 += letter
                else:
                    temp2 += temp[i]
            temp = temp2
            temp2 = ""
            lettercount = 0;
            for i in range(len(temp)):
                if temp[i] == letter:
                    lettercount += 1
            if lettercount == len(temp):
                break
        answers.append(ans)
    print(min(answers))
else:
    print(0)

Submission status: Accepted

Solution code:
#from numpy import*
#from scipy import*
from collections import* #defaultdict Counter deque
from fractions import gcd
from functools import* #reduce
from itertools import* #permutations("AB",repeat=2) combinations("AB",2) product("AB",2)
from operator import mul
from bisect import* #bisect_left bisect_right
from heapq import* #heapify heappop heappushpop
from math import factorial
from copy import deepcopy
import sys
#input=sys.stdin.readline
sys.setrecursionlimit(10**6)

#enumerate input

def main():
    s=input()
    print(min(max(map(len,s.split(i)))for i in s))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
"""
与えられた長さNのtをt'にする方法
各iについて、t'のi文字目はtのi,i+1文字目のどちらかである

単一文字のt'に持っていく最小操作回数

文字間差分の最大値が最も小さい文字が強い
"""

S = input()
# S = "whbrjpjyhsrywlqjxdbrbaomnw"
Sset = set(S)
ans = 1000
for s in Sset:
    string  = s+S+s
    diff = 0
    tmp = 0
    for i,st in enumerate(string): 
        if s == st:
            diff = max(diff, i-tmp) #差のmax
            tmp = i
    ans = min(ans,diff)
print(ans-1)

Submission status: Accepted

Solution code:
s = input()

ans = 101
for i in range(26):
    a = chr(i+97)
    txt = s
    count = 0
    if txt.count(a) == len(txt):
        ans = 0
        break
    for j in range(len(s)-1):
        newtext = ""
        count += 1
        for k in range(len(txt)-1):
            if txt[k] == a or txt[k+1] == a:
                newtext += a
            else:
                newtext += txt[k]
        if newtext.count(a) == len(newtext):
            anstemp = count
            ans = min(anstemp, ans)
            break
        else:
            txt = newtext
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin

# n = int(stdin.readline().rstrip())
s = stdin.readline().rstrip()
# A, B, C = [int(x) for x in stdin.readline().rstrip().split()]
# data = [stdin.readline().rstrip().split() for _ in range(n)]

ans = len(s)
for c in range(ord('a'), ord('z')+1):
    cnt, cntMax = 0, 0
    for i in range(0, len(s)):
        if s[i] == chr(c):
            cntMax = max(cnt, cntMax)
            cnt = 0
            continue
        cnt += 1
    cntMax = max(cnt, cntMax)
    ans = min(ans, cntMax)
print(ans, '\n')
# print(*data, sep='\n')

Submission status: Accepted

Solution code:
from collections import Counter
s=input()
cs=Counter(s).most_common()
if len(cs)==1:print(0)
else:
    a=float("inf")
    for i,_ in cs:
        si=s[:]
        x=0
        while len(set(si))>1:
            sic=""
            for j in range(len(si)-1):
                if si[j]==i or si[j+1]==i:sic+=i
                else:sic+=si[j]
            si=sic.replace(i*2,i)
            x+=1
        a=min(a,x)
    print(a)

Submission status: Accepted

Solution code:
s=input()
ans=50
for i in "qwertyuiopasdfghjklzxcvbnm":
    t=0
    a=0
    for c in s:
        if(i==c):
            a=max(a,t)
            t=0
        else:
            t+=1
    a=max(a,t)
    ans=min(ans,a)
print(ans)

Submission status: Accepted

Solution code:
s = input()
l = list(s)
N = len(s)
n = len(s)
ans = 0

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

for ll in l:
    S = s
    n = N
    while n > 0:
        tmp = ''
        for i in range(n - 1):
            if S[i] == ll or S[i + 1] == ll:
                tmp += ll
            else:
                tmp += S[i]
        if len(set(tmp)) == 1:
            ans = max(ans, len(tmp))
            break
        n -= 1
        S = tmp

print(N - ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
A - Shrinking
https://atcoder.jp/contests/agc016/tasks/agc016_a

"""
import sys


def solve(s):
    ans = []
    for ch in set(s):
        t = list(s)
        x = 0
        while len(set(t)) > 1:
            t = [ch if a == ch or b == ch else a for a, b in zip(t, t[1:])]
            x += 1
        ans.append(x)
    return min(ans)


def main(args):
    s = input()
    ans = solve(s)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

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

for i in range(26):
  c = chr(97+i)
  m = 0
  b = -1
  #p = []
  for j in range(n):
    if s[j] == c:
      #p.append(j)
      m = max(j-b,m)
      b = j
  #print(c,p)
  m = max(n-b,m)
  r = min(m,r)
print(r-1)

Submission status: Accepted

Solution code:
from collections import Counter

S = list(input())
C = Counter(S)

o = []

for a in C.keys():
    j = 0
    m = 0
    for b in S:
        if a == b:
            j = 0
        else:
            j += 1
            m = max(m,j)
    o.append(m)

print(min(o))

Submission status: Accepted

Solution code:
S = [ ord(i)-ord('a') for i in input() ]

ms = 100000000000
for i in range(26):
  if i not in S:
    continue
  V = [ 1 if (i==j) else 0 for j in S ]
  
  s = 0
  for c in range(1000):
    if ( len(V)==sum(V) ):
      s = c
      break
    Vn = [0]*( len(V)-1 )
    for j in range( len(V)-1 ):
      Vn[j]=max(V[j],V[j+1])
    V = Vn
  
  ms = min(s,ms)
print (ms)

Submission status: Accepted

Solution code:
s = input()
s = list(s)
ans = 1 << 99
for c in range(ord('a'), ord('z') + 1):
    c = chr(c)
    t = s.copy()
    while not all(c == cc for cc in t):
        for i in range(len(t)):
            if c in t[i:i+2]:
                t[i] = c
        t.pop()
    ans = min(ans, len(s) - len(t))
print(ans)

Submission status: Accepted

Solution code:
import collections
s = input()
anss = collections.defaultdict(int)
ans = 100
alp = 'abcdefghijklmnopqrstuvwxyz'
for i in alp:
    cnt = 0
    for j in s:
        if i == j:
            anss[i] = max(cnt, anss[i])
            cnt = 0
        else:
            cnt += 1
    anss[i] = max(cnt, anss[i])
    
for c in anss.values():
    if ans > c:
        ans = c
print(ans)

Submission status: Accepted

Solution code:

s=input()
def change(s,x):
    if s.count(x)==len(s):
        return 0
    t=s
    u=''
    for i in range(1,len(s)):
        for j in range(len(t)-1):
            if t[j]==x or t[j+1]==x:
                u+=x
            else:
                u+=t[j]
        if u.count(x)==len(u):
            return i
        t=u
        u=''
m=[]
for i in s:
    m.append(change(s,i))
print(min(m))

Submission status: Accepted

Solution code:
import collections
s = input()
anss = collections.defaultdict(int)
ans = 100
for i in s:
    if i not in anss:
        cnt = 0
        for j in s:
            if i == j:
                anss[i] = max(cnt, anss[i])
                cnt = 0
            else:
                cnt += 1
        anss[i] = max(cnt, anss[i])
        
for c in anss.values():
    if ans > c:
        ans = c
print(ans)

Submission status: Accepted

Solution code:
S = input()
print(min(max(map(len, S.split(s))) for s in S))

Submission status: Accepted

Solution code:
S = input()

mi = 1e10
for c in [chr(ord('a') + i) for i in range(26)]:
    count = 0
    T = S[:]
    while len(T) > T.count(c):
        U = ""
        for i in range(len(T) - 1):
            if c in T[i:i+2]:
                U += c
            else:
                U += T[i]
        T = U
        count += 1
    mi = min(mi, count)

print(mi)

Submission status: Accepted

Solution code:
S=input()
N=100
for i in range(26):
	N = min(N, max([ len(x) for x in S.split(chr(97+i)) ]))
print(N)

Submission status: Accepted

Solution code:
s = input()

l = list(set(list(s)))

ans = 100

for x in l:
    a = [-1]
    for i in range(len(s)):
        if s[i]==x:
            a.append(i)
    a.append(len(s))
    b = 0
    for j in range(len(a)-1):
        b = max(b,a[j+1]-a[j]-1)
    ans = min(ans,b)

print(ans)

Submission status: Accepted

Solution code:
s=input()
S=set(s)
ans=float("inf")
for x in S:
  count=0
  t=s
  n=len(set(t))
  while n>1:
    count+=1
    tt=""
    for i in range(len(t)-1):
      if t[i]==x or t[i+1]==x:
        tt+=x
      else:
        tt+=t[i]
    t=tt
    n=len(set(t))
  ans=min(ans,count)
print(ans)

Submission status: Accepted

Solution code:
S = input()

chars = set(S)

res = len(S)
for char in chars:
    max_c = 0
    c = 0
    for s in S:
        if s != char:
            c += 1
        else:
            max_c = max(c, max_c)
            c = 0
    max_c = max(c, max_c)
    res = min(res, max_c)

print(res)

Submission status: Accepted

Solution code:
check='abcdefghijklmnopqrstuvwxyz'
s=input()
l=len(s)
pos=[[0] for _ in range(26)]
for i in range(l):
  for j in range(26):
    if s[i]==check[j]:
      pos[j].append(i+1)
ans=10**9
for i in range(26):
  tmp=0
  for j in range(len(pos[i])-1):
    tmp=max(tmp,pos[i][j+1]-pos[i][j]-1)
  tmp=max(tmp,l-pos[i][-1])
  ans=min(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:
q = input()
p = set(q)

ans = 100
for i in p:
    count = 0
    S = q
    while len(set(S)) != 1:
        count += 1
        N = len(S)
        t = ''
        for j in range(N-1):
            if S[j] == i or S[j+1] == i:
                t += i
            else:
                t += S[j]
        S = t
    ans = min(ans,count)
print(ans)

Submission status: Accepted

Solution code:
S = input()
p = set(S)

ans = 100
for i in p:
    mc = max([len(w) for w in S.split(i)])
    ans = min(ans,mc)
print(ans)

Submission status: Accepted

Solution code:
import itertools
import math
from collections import deque
from collections import defaultdict
from itertools import permutations
import heapq
import bisect
from collections import Counter
from itertools import*
from bisect import bisect_left,bisect_right
from collections import Counter
inf = 10**10
from functools import reduce

s = input()
ans = len(s)
for i in set(s):
    t = s.split(i)
    c = 0
    for j in t:
        c = max(c,len(j))
    ans = min(ans,c)
print(ans)

Submission status: Accepted

Solution code:
s = input()
ans = len(s)
for v in set(s):
    cur = 0
    vstep = 0
    for i in range(len(s)):
        if s[i] != v:
            cur += 1
        else:
            vstep = max(cur,vstep)
            cur = 0
    vstep = max(cur,vstep)
    ans = min(ans,vstep)
print(ans)

Submission status: Accepted

Solution code:
s = input()
ans = len(s)
for i in set(s):
    t = s.split(i)
    c = 0
    for j in t:
        c = max(c,len(j))
    ans = min(ans,c)
print(ans)

Submission status: Accepted

Solution code:
S = input()

chars = set()
for s in S:
    chars.add(s)

res = len(S)
for char in chars:
    max_c = 0
    c = 0
    for s in S:
        if s != char:
            c += 1
        else:
            max_c = max(c, max_c)
            c = 0
    max_c = max(c, max_c)
    res = min(res, max_c)

print(res)

Submission status: Accepted

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

answer = 100
for c in characters:
  if c in s:
    mc = max([len(w) for w in s.split(c)])
    answer = min(answer, mc)
print(answer)

Submission status: Accepted

Solution code:
S = input()
n = len(S)
d = [str(chr(i+97)) for i in range(26)]
ans = float('inf')
for dd in d:
    s = S
    t = ''
    m = 0
    while True:
        if s.count(dd) == len(s):
            break
        for i in range(len(s)-1):
            if s[i] == dd or s[i+1] == dd:
                t = t + dd
            else:
                t = t + s[i]
        s = t
        t = ''
        m += 1
    ans = min(ans,m)
print(ans)

Submission status: Accepted

Solution code:
s = input()
se = set("".join(s))
ans = 10**10
for m in se:
    t = s
    tans = 0
    for i in range(len(s)):
        if len(set("".join(t))) == 1 and m in t:
            break
        tt = ""
        for j in range(len(t)-1):
            if t[j] == m or t[j+1] == m:
                tt += m
            else:
                tt += t[j]
        tans += 1
        t = tt
    ans = min(ans , tans)
print(ans)

Submission status: Accepted

Solution code:
s = input()
n = len(s)
a = list(set(list(s)))
ans = 1000
for x in a:
    t = 0
    l = r = 0
    while l < n:
        while r < n and s[r] != x:
            r += 1
        t = max(t, r - l)
        l = r + 1
        r = l
    ans = min(ans, t)
print(ans)

Submission status: Accepted

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

for i in range(1,N+1):   
    s = {x for x in S}
    for j in range(N+1-i):
        s &= {x for x in S[j:j+i]}
        if s==set():
            break
    else:
        break   
print(i-1)

Submission status: Accepted

Solution code:
s = input()
L = []
for i in range(len(s)):
  if not s[i] in L:
    L.append(s[i])
ans = float('inf')
if len(L) == 1:
  print(0)
else:
  for i in range(len(L)):
    k = s
    sub = 0
    flag = False
    while flag == False:
      cur = ''
      cnt = 0
      for j in range(len(k)-2,-1,-1):
        if cnt == len(cur):
          if k[j] == L[i]:
            cur = L[i]*2 + cur
          else:
            cur = k[j] + cur
        else:
          if k[j] == L[i]:
            cur = L[i] + cur
          else:
            None
        cnt += 1
      check = L[i]*len(cur)
      sub += 1
      if check == cur:
        if sub <= ans:
          ans = sub
        flag = True
      else:
        k = cur
  print(ans)

Submission status: Accepted

Solution code:
from string import ascii_lowercase
s=input()
ans=100
for c in ascii_lowercase:
    ans=min(ans,max([len(x)for x in s.split(c)]))
print(ans)

Submission status: Accepted

Solution code:
s = list(input())
ans = 10**5
for i in set(list(s)):
  q = s[:]
  cnte = 0
  while len(set(q)) > 1:
    new = [""]*(len(q)-1)
    for j in range(len(q)-1):
      if q[j] == i or q[j+1] == i:
        new[j] = i
      else:
        new[j] = q[j]
    q = new
    cnte += 1
  if cnte < ans:
    ans = cnte
print(ans)

Submission status: Accepted

Solution code:
import copy
S = input()
a = "abcdefghijklmnopqrstuvwxyz"
A = list(a)
ans = []
for a in A:
    tmp = copy.copy(S)
    cnt = 0
    while len(list(set(tmp))) > 1:
        tmp1 = []
        cnt += 1
        for i in range(len(tmp) - 1):
            if tmp[i] == a:
                tmp1.append(a)
            elif tmp[i+1] == a:
                tmp1.append(a)
            else:
                tmp1.append(tmp[i])
        tmp = copy.copy(tmp1)
    ans.append(cnt)
print(min(ans))

Submission status: Accepted

Solution code:
S = input()
L = len(S)
ans = float('inf')
for s in [chr(i) for i in range(97, 97+26)]:
    mx = 0
    cnt = 0
    for i in range(L):
        if S[i] == s:
            mx = max(mx,cnt)
            cnt = 0
        else:
            cnt += 1
    
    val = max(cnt , mx)
    ans = min(ans,val)

print(ans)

Submission status: Accepted

Solution code:
s = input()
chars = set(s)
ans = len(s)

for c in chars:
    max_partial_char_length = max(len(_) for _ in s.split(c))
    ans = min(ans, max_partial_char_length)

print(ans)

Submission status: Accepted

Solution code:
s = input()
ans = 10**5
def f(s,w):
    cnt = 0
    k = s.count(w)
    length = len(s)
    while True:
        if k==length:
            break
        s_=''
        for i in range(len(s)-1):
            if s[i+1]==w:
                s_+=w
            else:
                s_+=s[i]
        length-=1
        k = s_.count(w)
        s = s_
        cnt+=1
    return cnt
for s_ in s:
    ans = min(ans,f(s,s_))
print(ans)

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
from collections import Counter

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


def main():
    s = SI()
    char_cnt = Counter(s)

    res = INF
    for c in char_cnt.keys():
        r = s[:]
        cnt = 0
        while len(set(r)) != 1:
            t = ''
            for ss in zip(r[:-1], r[1:]):
                if c in ss:
                    t += c
                else:
                    t += ss[0]
            cnt += 1
            r = t
            # print(r)
        res = min(res, cnt)

    return res


print(main())

Submission status: Accepted

Solution code:
s = input()
L = []
for i in s:
  if i not in L:
    L.append(i)

Ans = 100
for i in L:
  cnt = 0
  ma = 0
  for j in s:
    if i != j:
      cnt += 1
    else:
      ma = max(ma, cnt)
      cnt = 0
  ma = max(ma, cnt)
  Ans = min(Ans, ma)

print(Ans)

Submission status: Accepted

Solution code:
s = input()
s_list = list(s)
minis = []
for k in set(s):
  a = [i for i, x in enumerate(s_list) if x == k]
  mini = max(a[0], 0)
  for m in range(len(a) - 1):
    mini = max(a[m+1] - a[m] - 1, mini)
  mini = max(len(s) - a[-1] - 1, mini)
  minis.append(mini)
print(min(minis))

Submission status: Accepted

Solution code:
L = [chr(i) for i in range(97, 123)]
s = list(input())
N = len(s)

ans = float("inf")
for i in L:
    c = 0
    X = s
    while True:
        if X.count(i) == N - c:
            break
        else:
            Y = []
            for j in range(N - c - 1):
                if X[j] == i or X[j + 1] == i:
                    Y.append(i)
                else:
                    Y.append(".")
            X = Y
        c += 1

    if c < ans:
        ans = c

print(ans)

Submission status: Accepted

Solution code:
S=input()
E=list(set(S))
print(min(max(len(s) for s in S.split(e)) for e in E))

Submission status: Accepted

Solution code:
s=input();print(min(max(map(len,s.split(t)))for t in s))

Submission status: Accepted

Solution code:
from collections import Counter
S = input()
A = Counter(list(S)).most_common()

ans = float('inf')
for a in A:
    posi = -1
    interval = 0
    for i in range(len(S)):
        if S[i] == a[0]:
            interval = max(interval, i - posi - 1)
            posi = i
        if i == len(S) - 1:
            interval = max(interval, i - posi)
            posi = i
    #print(a[0], interval)
    ans = min(ans, interval)
        
print(ans)

Submission status: Accepted

Solution code:
s = input()
m = set(s)

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

ans = float('inf')
for k in m:
    cnt = 0
    tt = s
    while True:
        cnt += 1
        t = ''
        for i in range(len(tt)-1):
            if tt[i] == k or tt[i+1] == k:
                t += k
            else:
                t += tt[i+1]
        tt = t

        if len(set(tt)) == 1:
            break
    ans = min(ans,cnt)       
print(ans)

Submission status: Accepted

Solution code:
import copy
S=list(input())

ans=1000

A=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
Li=[]
li=[]
for i in range(26):
    li=copy.deepcopy(S)
    if li.count(A[i])==len(li):
        print(0)
        exit()
    else:
        Li=copy.deepcopy(li)
        li=[]
        for k in range(len(S)):
            for j in range(len(Li)-1):
                if Li[j]==A[i] or Li[j+1]==A[i]:
                    li.append(A[i])
                else:
                    li.append("1")
            #print(li)
            if li.count(A[i])==len(li):
                if ans>len(S)-len(li):
                    ans=len(S)-len(li)
                break
            else:
                Li=copy.deepcopy(li)
                li=[]
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


def main():
    s = input()

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

    alphabets = [chr(ord('a') + i) for i in range(26)]
    ans = float('inf')

    for alphabet in alphabets:
        t = s
        count = 0

        while len(t) >= 2:
            t_dash = ''

            for i in range(len(t) - 1):
                if t[i] == alphabet or t[i + 1] == alphabet:
                    t_dash += alphabet
                else:
                    t_dash += t[i]

            count += 1
            t = t_dash

            if len(set(t_dash)) == 1:
                break

        ans = min(ans, count)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input().strip()
sset = set(s)
ans = pow(100, 3)
for ss in sset:
  tmps = s
  c = 0
  while tmps.count(ss) != len(tmps):
    _tmps = ''
    for ps, ns in zip(tmps, tmps[1:]):
      if ps == ss or ns == ss:
        _tmps += ss
      else:
        _tmps += ps
    tmps = _tmps
    c += 1
  if ans > c:
    ans = c
print(ans)