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
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
import string

def main():
    s = input().strip()
    n = len(s)
    m = {}
    for c in string.ascii_lowercase:
        t = list(s[:])
        count = 0
        while True:
            if len(set(t)) == 1:
                m[c] = count
                break
            for i in range(len(t) - 1):
                if t[i] == c or t[i+1] == c:
                    t[i] = c
            t = t[:len(t) - 1]
            count += 1
    print(min(m.values()))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    s = input()

    cs = [chr(i) for i in range(ord("a"), ord("z") + 1)]

    ans = len(s)
    for c in cs:
        cnt = 0
        mx = 0
        for e in s:
            if e == c:
                mx = max(mx, cnt)
                cnt = 0
            else:
                cnt += 1

        mx = max(mx, cnt)

        ans = min(ans, mx)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import string
s = input()
ans = 101
for last in string.ascii_lowercase:
    t = list(s)
    while len(set(t)) != 1:
        for i in range(len(t)):
            if last in t[i:i+2]:
                t[i] = last
        t.pop()
    ans = min(ans, len(s)-len(t))
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
import sys,bisect,math,itertools,string,queue
from heapq import heappop, heappush
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

def check(S): #Sが単一の文字かどうか
    n = len(S)
    for i in range(n-1):
        if S[i] != S[i+1]:
            return False
    return True
S = input()
n = len(S)
T = S
res = float('inf')
for i in range(97,97+26+1):
    S = T
    if not chr(i) in S:
        continue
    # print(chr(i))
    cnt = 0
    while True:
        if check(S):
            break
        tmp = ''
        for j in range(len(S)-1):
            if S[j] == chr(i) or S[j+1] == chr(i):
                tmp += chr(i)
            else:
                tmp += S[j]
        S = tmp
        cnt += 1
    res = min(res, cnt)
    
print(res)

Submission status: Accepted

Solution code:
import copy
def main():
    s=list(input())
    n=len(s)
    ans=n
    p=0
    look=[chr(i) for i in range(97, 97+26)]
    for i in look:
        t=copy.copy(s)
        cnt=0
        check=0
        while True:
            key=0
            for j in range(len(t)-1):
                if t[j]==i:
                    continue
                else:
                    check=1
                    if t[j+1]==i:
                        t[j]=i
                    key=1
                    p=1
            if check==0:
                ans=0
                break
            if len(t)>0 and t[len(t)-1]!=i:
                key=1
                check=1
            t.pop(len(t)-1)
            if key==0:
                break
            cnt+=1
            if t==[]:
                break
        if p!=0 and cnt!=0:
            ans=min(ans,cnt)
    print(ans)    
main()

Submission status: Accepted

Solution code:
def F(s, target):
    cnt = 0
    while s.count(target) != len(s):
        cnt += 1
        s = ''.join(y if x != target else x for x, y in zip(s[:-1], s[1:]))
    return cnt

def main():
    s = input()
    ans = min(F(s,t) for t in set(s))
    print(ans)
    

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S=input()
Dict={}

for s in range(len(S)):
    if(Dict.get(S[s],None)==None):
        Dict[S[s]]=1
    else:
        Dict[S[s]]+=1
MIN=10*10

for s in Dict:
    count=0
    tMAX=0
    for s2 in range(len(S)):
        if(S[s2]==s):
            tMAX=max(tMAX,count)
            count=0
        else:
            count+=1
    tMAX=max(tMAX,count)    
    MIN=min(MIN,tMAX)

print(MIN)

Submission status: Accepted

Solution code:
l=list(input())
s=set(l)
ans=1000000000
for i in s:
    k=l
    c=0
    while len(set(k))>1:
        tmp=['0']*(len(k)-1)
        for j in range(len(k)-1):
            if k[j]==i or k[j+1]==i:
                tmp[j]=i
        k=tmp
        c+=1
    ans=min(ans,c)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
s=str(input())
ss=s
a=Counter(s)
a=sorted(a.items(), key=lambda x: x[1])
a=a[::-1]
#print(list(a))

ans=float('inf')
for j in range(len(a)):
    s=ss
    ans1=0
    while len(set(s))!=1:
        t=''
        for i in range(len(s)-1):
            if s[i]==a[j][0] or s[i+1]==a[j][0]:
                t+=a[j][0]
            else:
                t+=s[i]
        s=t
        #print(s)
        ans1+=1
    ans=min(ans,ans1)
print(ans)

Submission status: Accepted

Solution code:
from string import ascii_lowercase
from sys import stdin

S = stdin.readline().rstrip()
ans = 100

for a in ascii_lowercase:
    tmp = max(len(sub_str) for sub_str in S.split(a))
    if tmp < ans:
        ans = tmp
print(ans)

Submission status: Accepted

Solution code:
import collections
def solve():
  s = input()
  c = collections.Counter(s)
  query = 'abcdefghijklmnopqrstuvwxyz'
  ans = 10**10
  for i in range(len(query)):
    target = query[i]
    if c[target] == 0:
      continue
    else:
      s_copy = s
      ct = 0
      while len(set(s_copy)) > 1:
        n = len(s_copy)
        for j in range(n-1):
          if s_copy[j+1] == target:
            s_copy = s_copy[:j] + target + s_copy[j+1:]
        s_copy = s_copy[0:n-1]
        ct += 1
    ans = min(ans,ct)
  print(ans)
solve()

Submission status: Accepted

Solution code:
s = input()
ans = []

for ws in s:
    ws = s.split(ws)
    ans.append(max(map(len, ws)))

print(min(ans))

Submission status: Accepted

Solution code:
s = list(input())
t = []
ans = 10**9 + 10

if s.count(s[0]) == len(s):
    print(0)
    exit()

for i in set(s):
    c = i
    j = 0
    n = s
    t = []
    while j >= 0:
        for k in range(1, len(n)):
            if c == n[k-1] and c != n[k]:
                t.append(c)
            elif c != n[k-1] and c == n[k]:
                t.append(c)
            elif c != n[k-1] != n[k]:
                t.append(n[k-1])
            else:
                t.append(n[k])
        if t.count(c) == len(t):
            cnt = j + 1
            if cnt < ans:
                ans = cnt
            break
        else:
            n = t
            t = []
        j += 1

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

s = input()
x = Counter(s).keys()
s = [max(map(len, s.split(i))) for i in x]

print(min(s))

Submission status: Accepted

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

A = [chr(i) for i in range(ord('a'), ord('z')+1)]
B = {}
for i in range(len(A)):
    B[A[i]] = i

C = [[-1] for i in range(len(A))]

for i in range(n):
    C[B[s[i]]].append(i)
for i in range(len(C)):
    C[i].append(n)

# print(C)

ans = float("inf")

for i in range(len(C)):
    ans0 = -float("inf")
    for j in range(len(C[i])-1):
        ans0 = max(ans0, C[i][j+1] - C[i][j])
    ans = min(ans, ans0)

print(ans-1)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

s = input()
s_list = list(s)
min_count = float("inf")

for char in set(s_list):
    w_list = s_list[:]
    after_list = []

    count = 0
    while len(set(w_list)) != 1:
        after_list = []
        for i in range(len(w_list) - 1):
            if w_list[i + 1] == char:
                after_list += [char]
            else:
                after_list += w_list[i]
        count += 1
        w_list = after_list

    if count < min_count:
        min_count = count
        ans = "".join(w_list)

ans = min_count
print(ans)

Submission status: Accepted

Solution code:
import string

alphabets = string.ascii_lowercase

S = input()

res = float("inf")

for i, a in enumerate(alphabets):
    cnt = 0
    T = list(S)
    flag = True
    while len(set(T)) != 1 and flag:
        cnt += 1
        flag = False
        for j in range(len(T) - 1):
            if T[j] == a or T[j + 1] == a:
                T[j] = a
                flag = True
        T = T[:-1]
    if flag:
        res = min(res, cnt)

print(res)

Submission status: Accepted

Solution code:
s=input()
ans=10**30
for i in range(len(s)):
	p=[-1]
	for j in range(len(s)):
		if s[j]==s[i]:
			p.append(j)
	p.append(len(s))
	m=-1
	for j in range(len(p)-1):
		m=max(m,p[j+1]-p[j]-1)
	ans=min(m,ans)
print(ans)

Submission status: Accepted

Solution code:
s = list(input())
ss = set(s)
ans = 100
for i in ss:
  cnt = 0
  nexts = s[:]
  while len(set(nexts)) > 1:
    nexts = [i if i in nexts[j:j+2] else nexts[j] for j in range(len(nexts)-1)]
    cnt += 1
  ans = min(ans, cnt)
print(ans)

Submission status: Accepted

Solution code:
S = input()

ans = float('inf')
for goal in S:
    dist = [float('inf')] * len(S)
    right = float('inf')
    for i in range(len(S))[:: -1]:
        if S[i] == goal:
            right = i
        dist[i] = min(dist[i], abs(i - right))

    cost = float('inf')
    for i in range(len(S)):
        cost = min(cost, max(i, max(dist[:len(S) - i])))
    ans = min(ans, cost)

print(ans)

Submission status: Accepted

Solution code:
S=input();print(min(max(map(len,S.split(c)))for c in"abcdefghijklmnopqrstuvwxyz"))

Submission status: Accepted

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

for length in range(N-1):
  tmp = set([i for i in s])
  for left in range(N-length+1):
    right = left + length
    tmp = set(s[left:right]) & tmp
    #print(left,right)
    #print(tmp)
    #print("--------")
  else:
    #print(length, tmp)
    if len(tmp) > 0:
      print(length-1)
      exit()
print(0)

Submission status: Accepted

Solution code:
S=input()
N=len(S)
dp=[[0 for i in range(27)] for j in range(N+1)]

if N==1:
  print(0)
  exit()

for i in range(1,N):
  for j in range(N-i+1):
    s=list(set(S[j:j+i]))
    for ss in s:
      #print(ss,ord(ss)-96)
      dp[i][ord(ss)-96]+=1
    for k in range(1,27):
      if dp[i][k]==N-i+1:
        #1
        print(i-1)
        exit()

Submission status: Accepted

Solution code:
def resolve():
    s = input()
    print(min([max(map(len, s.split(c))) for c in s]))


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
def resolve():
    s = input()
    rs = []
    for c in s:
        ws = s.split(c)
        rs.append(max(map(len, ws)))
    print(min(rs))
        

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
s = input()
cs = set(s)
rs = []
for c in cs:
  ws = s.split(c)
  r = max(map(len, ws))
  rs.append(r)
r = min(rs)
print(r)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline

def main():
    S = input().strip()
    mi = len(S)
    for c in set(S):
        s = list(S)
        n = 0
        while len(set(s)) > 1:
            for i in range(len(s)-1):
                if s[i+1] == c:
                    s[i] = c
            s.pop()
            n += 1
        mi = min(mi, n)
    print(mi)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
import collections

def main():
    S = input().strip()
    mi = len(S)
    for c in collections.Counter(S).keys():
        s = list(S)
        n = 0
        while len(collections.Counter(s).keys()) > 1:
            for i in range(len(s)-1):
                if s[i+1] == c:
                    s[i] = c
            s.pop()
            n += 1
        mi = min(mi, n)
    print(mi)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
import collections

def main():
    S = input().strip()
    mi = len(S)
    for c in collections.Counter(S).keys():
        s = list(S)
        n = 0
        while 1:
            if len(collections.Counter(s).keys()) <= 1:
                break
            for j in range(len(s)-1):
                if s[j+1] == c:
                    s[j] = c
            s.pop()
            n += 1
        mi = min(mi, n)
    print(mi)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import copy
s = input()
N = len(s)
chr_set = set([c for c in s])
if len(chr_set) == 1:
    print(0)
    exit()

ans = []
for c in chr_set:
    count = 0
    t = copy.deepcopy(s)
    loop_len = N
    while True:
        tmp_t = ""
        for i in range(loop_len-1):
            if t[i] == c:
                tmp_t += c
            elif t[i+1] == c:
                tmp_t += c
            else:
                tmp_t += t[i]
        count += 1
        if len(set(tmp_t)) == 1:
            ans.append(count)
            break
        t = tmp_t
        loop_len -= 1

print(min(ans))

Submission status: Accepted

Solution code:
s = input()
print(min(max(len(p) for p in s.split(l)) for l in set(list(s))))

Submission status: Accepted

Solution code:
s=list(input())
ans=len(s)
for i in range(26):
    c=chr(ord('a')+i)
    if c not in s:
        continue
    tmp=s
    cnt=0
    while any(a!=c for a in tmp):
        cnt+=1
        ss=''
        for a,b in zip(tmp,tmp[1:]):
            if a==c or b==c:
                ss+=c
            else:
                ss+=a
        tmp=ss
    ans=min(ans,cnt)
print(ans)

Submission status: Accepted

Solution code:
S=input()
a=[]
for s in set(list(S)):
  w=0
  for p in S.split(s):
    w=max(w,len(p))
  a.append(w)
print(min(a))

Submission status: Accepted

Solution code:
import collections

s=list(input())
N=len(s)

if s.count(s[0])==len(s):
    print(0)
    exit()

s_set=set(s)


if len(s_set)==N:
    print((N+1)//2)
    exit()

ans=N
for r in s_set:
    mx=1
    c=0

    for i in range(N):
        if s[i]!=r :
            c+=1
        else:
            mx=max(c,mx)
            c=0
    mx=max(c,mx)
    ans=min(ans,mx)
print(ans)

Submission status: Accepted

Solution code:
S=list(input())

ans=len(S)
for s in set(S):
  T=S
  tmp=0
  for i in range(len(S)):
    if len(set(T))==1:
      ans=min(ans,tmp)
      break
    tmp+=1
    T=[s if a==s else b for a,b in zip(T[0:],T[1:])]
print(ans)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    s = S()
    n = len(s)
    f = defaultdict(lambda : float("inf"))
    for i in range(n):
        for j in range(n):
            if j < i:
                f[(i,s[j])] = min(f[(i,s[j])],n-j-1)
            else:
                f[(i,s[j])] = min(f[(i,s[j])],j-i)
    ans = float("inf")
    for a in s:
        m = 0
        for i in range(n):
            m = max(m, f[(i,a)])
        if m < ans:
            ans = m
    print(ans)
    return
#B
def B():

    return

#C
def C():

    return

#D
def D():

    return

#E
def E():

    return

#F
def F():

    return

#Solve
if __name__ == "__main__":
    A()

Submission status: Accepted

Solution code:
#import pysnooper
import os,re,sys,operator,math,heapq,string
from collections import Counter,deque
#from operator import itemgetter
#from itertools import accumulate,combinations,groupby,combinations_with_replacement
from sys import stdin,setrecursionlimit
#from copy import deepcopy

setrecursionlimit(10**6)
input=stdin.readline

s=list(input().rstrip())
ans=10000
for i in set(s):
    tmp=deque(s[:])
    cnt=0
    while len(set(tmp))!=1:
        cnt+=1
        for j in range(len(tmp)-1):
            if tmp[j+1]==i:
                tmp[j]=tmp[j+1]
            else:
                continue
        #print(tmp)
        tmp.pop()
    ans=min(ans,cnt)
print(ans)

Submission status: Accepted

Solution code:
import collections
def main():
    s = input()
    d = collections.defaultdict(list)
    chars = set(list(s))
    for c in chars:
        d[c].append(-1)
    for i in range(len(s)):
        d[s[i]].append(i)
    for c in chars:
        d[c].append(len(s))
    minmax_dist = float('inf')
    for c in chars:
        max_dist = 0
        for i in range(len(d[c])-1):
            max_dist = max(max_dist, d[c][i+1] - d[c][i]-1)
        minmax_dist = min(minmax_dist, max_dist)
    return minmax_dist


if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
S = input()
N = len(S)
C = set(S)
ans = N-1
for c in C:
    T = list(S)
    tmp = 0
    for i in range(N):
        if len(set(T))==1 : 
            ans = min(ans,tmp)
            break
        tmp += 1
        T = [c if a == c else b for a,b in zip(T[:-1],T[1:])]
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, fractions, itertools, collections, copy, heapq


def solve(s: str):
    mn = 1e18
    for i in range(ord('a'), ord('z')+1):
        t = list(s)
        counter = 0
        while len(set(t)) > 1:
            for j, ss in enumerate(t[:-1]):
                if chr(i) == t[j+1]:
                    t[j] = t[j+1]
            t = t[:-1]
            counter += 1
        mn = min(counter, mn)
    print(mn)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

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 I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  s=S()

  a=list(set(s))

  ans=[]
  for x in a:
    c=0
    t=list(s)
    while True:
      if len(set(t))==1:
        break

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

      c+=1

    ans.append(c)

  return min(ans)

print(main())

Submission status: Accepted

Solution code:
from collections import defaultdict
S = input()
dict = defaultdict(int)
for s in S:
    dict[s] += 1

if len(dict.keys()) == 1:
    print(0)
    exit()

ans = 101
for c in dict.keys():
    T = S[:]
    TN = []
    flag = True
    check = True
    cnt = 0
    while check:
        for i in range(len(T)-1):
            if c == T[i] or c == T[i+1]:
                TN.append(c)
            else:
                TN.append(T[i])
                flag = False
        cnt += 1
        if flag:
            break

        T = TN[:]
        TN = []
        flag = True
    if cnt < ans:
        ans = cnt
print(ans)

Submission status: Accepted

Solution code:
s = input()
def is_one_char(str):
    if len(set([c for c in str])) == 1:
        return True
    else:
        return False

candidate = set([c for c in s])
counts = []
for alpha in candidate:
    newstr = ""
    oldstr = s
    flag = is_one_char(oldstr)
    count = 0
    while(not flag):
        for i in range(len(oldstr) - 1):
            if oldstr[i] == alpha or oldstr[i+1] == alpha:
                newstr += alpha
            else:
                newstr += oldstr[i]
        flag = is_one_char(newstr)
        count = len(s) - len(newstr)
        oldstr = newstr
        newstr = ""
    counts.append(count)
print(min(counts))

Submission status: Accepted

Solution code:
from collections import Counter
s=list(input())
c=Counter(s)
ans=10**8
for i in c.keys():
    sss=s
    cnt=0
    while True:
        if len(set(sss))==1:
            break
        else:
            ss=[]
            for j in range(len(sss)-1):
                if sss[j]==i or sss[j+1]==i:
                    ss.append(i)
                else:
                    ss.append(s[j])
            cnt+=1
            if ss[0]==i and len(set(ss))==1:
                break
            else:
                pass
        sss=ss
    ans=min(ans,cnt)
print(ans)

Submission status: Accepted

Solution code:
def main():
    s = input().strip()
    N = len(s)
    check = [False] * N
    ans = N
    for i in range(N):
        if not check[i]:
            now = s[i]
            check[i] = True
            max_ = N-1-i
            count = 0
            for j in range(i+1):
                if s[j] != now:
                    count += 1
                else:
                    check[j] = True
                    max_ = max(max_, count)
                    count = 0
            ans = min(ans, max_)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    s = input().strip()
    N = len(s)
    check = [False] * N
    ans = N
    for i in range(N):
        now = s[i] # now: 検索対象
        # print("="*10)
        # print(now)
        max_ = N-1-i
        count = 0
        for j in range(i+1):
            if s[j] != now:
                count += 1
                # print(j, s[j], count)
            else:
                # print(j, s[j], count)
                max_ = max(max_, count)
                count = 0
        ans = min(ans, max_)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict
d = defaultdict(list)
S = input()
for i in range(len(S)):
    d[S[i]].append(i)
ans = 100
for e in d.values():
    e = [-1] + e + [len(S)]
    m = 0
    for j in range(len(e) - 1):
        m = max(m, e[j+1] - e[j] - 1)
    ans = min(ans, m)
print(ans)

Submission status: Accepted

Solution code:
s = list(input())
ls = list("abcdefghijklmnopqrstuvwxyz")
m_cnt = 10 ** 9
for t in ls:
    if t in s:
        ss = s.copy()
        cnt = 0
        while True:
            sss = [""] * (len(ss) - 1)
            b = True
            for i in range(len(sss)):
                if ss[i] == t or ss[i+1] == t:
                    sss[i] = t
                else:
                    sss[i] = ss[i]
                    b = False
            if b:
                m_cnt = min(m_cnt, cnt if cnt == 0 else cnt + 1)
                break
            else:
                ss = sss.copy()
                cnt += 1
print(m_cnt)

Submission status: Accepted

Solution code:
import collections

def simu(s,i):
    temp = s
    counter = 0
    while len(collections.Counter(temp)) != 1:
        another = []
        for k in range(len(temp)-1):
            if temp[k] == i or temp[k+1] == i:
                another.append(i)
            else:
                another.append(temp[k])
        temp = another
        counter += 1
    return counter

s = list(input())
c = collections.Counter(s)
mini = 10000
for i in c:
    a = simu(s,i)
    if mini > a:
        mini = a
print(mini)

Submission status: Accepted

Solution code:
w=input();print(min(max(len(p)for p in w.split(s))for s in w))

Submission status: Accepted

Solution code:
w=input()
a=[]
for s in set(list(w)):
	m=0
	for p in w.split(s):
		m=max(m,len(p))
	a.append(m)
print(min(a))

Submission status: Accepted

Solution code:

S = list(input())
N = len(S)
import collections
import sys
C = collections.Counter(S)
Ckey = C.keys()
ans = 10 ** 9 + 7
if len(Ckey) == 1:
    print(0)
    sys.exit()
for s in Ckey:
    post = S[:]
    for i in range(1,N):
        #test = ''.join(post)
        #print(s,i,test,ans)
        flag = True
        tmp = ['0'] * (N-i)
        for j in range(N-i):
            if post[j] == s or post[j+1] == s:
                tmp[j] = s
            else:
                tmp[j] = post[j]
                flag = False
        post = tmp[:]
        if (flag):
            break
    ans = min(ans,i)
print(ans)

Submission status: Accepted

Solution code:
s = list(input())
N = len(s)
if N == 1:
    print (0)
    exit()

ans = 99
for i in range(1, N): #残す文字数
    tmp = 0 #残す文字以外の最大連続数
    count = 0 #残す文字以外が何文字続いているかをカウント
    for j in range(i):
        if s[j] == s[i]:
            tmp = max(tmp, count)
            count = 0
        else:
            count += 1
    tmp = max(tmp, count)
    ans = min(ans, max(tmp, N - i - 1))

print (ans)

Submission status: Accepted

Solution code:
# t = "abcdefghijklmnopqrstuvwxyz"
s = input()
t = set(list(s))
ans = 1000
for i in t:
    S = s
    c = 0
    while len(set(S))!=1:
        c+=1
        ts = ""
        for  j,k in zip(S,S[1:]):
            if j == i or k == i:
                ts+=i
            else:
                ts+=k
        S = ts
    ans = min(ans,c)
    # print(c)

print(ans)

Submission status: Accepted

Solution code:

t = "abcdefghijklmnopqrstuvwxyz"
s = input()
ans = 1000
for i in t:
    S = s
    c = 0
    while len(set(S))!=1:
        c+=1
        ts = ""
        for  j,k in zip(S,S[1:]):
            if j == i or k == i:
                ts+=i
            else:
                ts+=k
        S = ts
    ans = min(ans,c)
    # print(c)

print(ans)

Submission status: Accepted

Solution code:
s = input()
char_set = set(list(s))

ans = len(s)

for c in char_set:
    tmp = s
    cnt = 0
    while any(a != c for a in tmp):
        cnt += 1
        _tmp = ''
        for a, b in zip(tmp, tmp[1:]):
            if a == c or b == c:
                _tmp += c
            else:
                _tmp += b
        tmp = _tmp
    ans = min(ans, cnt)

print(ans)

Submission status: Accepted

Solution code:
S = input()

ans = len(S)
for i in range(26):
    c = chr(ord('a') + i)
    if c not in S: continue
    tmp = S
    cnt = 0
    while any(a!=c for a in tmp):
        cnt += 1
        _tmp = ''
        for a,b in zip(tmp,tmp[1:]):
            if a == c or b == c:
                _tmp += c
            else:
                _tmp += a
        tmp = _tmp
    ans = min(ans, cnt)
print(ans)

Submission status: Accepted

Solution code:
s=input()
se=set(list(s))
x=len(s)
for i in se:
  a=b=0
  for j in s:
    if i==j:a=0
    else:a+=1;b=max(a,b)
  x=min(x,b)
print(x)

Submission status: Accepted

Solution code:
import collections
s = list(input())
#print(s)
c = collections.Counter(s)
l = list(c.items())
l = sorted(l, key = lambda x : x[1], reverse=True)
#print(l)

h = []
tmp = l[0][1]

for i in range(len(l)):
          h.append(l[i][0])


#print(h)

ans = []

for i in range(len(h)):
    tmp = [_ for _, x in enumerate(s) if x == h[i]]
#    print(h[i])
#    print(tmp)
    lll = []
    for j in range(len(tmp)):
        if j == 0:
            lll.append(tmp[j])
        else:
            lll.append(tmp[j] - tmp[j-1] - 1)
    lll.append(len(s) - tmp[len(tmp) - 1] - 1)
    ans.append(max(lll))
#    print(lll)


#print(ans)
print(min(ans))

Submission status: Accepted

Solution code:
s=input()
l=len(s)
a=[1]*(l+1)
c=[]
x=0
while(x<=l-1):
  if(a[x]==1):
    a[x]=0
    k=0
    b=[x]
    t=s+s[x]
    for i in range(x+1,l+1):
      if(t[x]==t[i]):
        b.append(k)
        a[i]=0
        k=0
      else:
        k+=1
    c.append(max(b))
  x+=1
print(min(c))

Submission status: Accepted

Solution code:
s = input()
abc = []
for i in range(len(s)):
    if not s[i] in abc:
        abc.append(s[i])
ans = []
for c in abc:
    count = 0
    x = s
    y = ""
    while True:
        if x.count(x[0]) == len(x):
            break
        for i in range(len(x)-1):
            if x[i] == c or x[i+1] == c:
                y+=c
            else:
                y+=x[i]
        x = y
        y = ""
        count += 1
    ans.append(count)
print(min(ans))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#AGC16 A

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7

s = sys.stdin.readline()
s = s[:-1]
if len(Counter(s)) == 1:
    print(0)
    quit()
n = len(s)
ans = float('inf')
for i in range(n):
    x = s[i]
    t = s
    cnt = 0
    while 1:
        t_ = ''
        for j in range(len(t)-1):
            if t[j] == x or t[j+1] == x:
                t_ += x
            else:
                t_ += t[j]
        t = t_
        cnt += 1
        if len(Counter(t)) == 1:
            ans = min(ans,cnt)
            break
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

S = input().rstrip()

def F(S,target):
    cnt = 0
    while S.count(target) != len(S):
        cnt += 1
        S = ''.join(y if x != target else x for x,y in zip(S[:-1],S[1:]))
    return cnt

answer = min(F(S,t) for t in set(S))
print(answer)

Submission status: Accepted

Solution code:
# 2019/08/09
#だめなやつ

from collections import Counter
s=list(input())
n=len(s)
S=set(s)
"""
if len(s)==len(S):
    print(n//2)
    exit()"""
if s.count(s[0])==len(s):
    print(0)
    exit()


res=float('inf')
for e in S:
    r=0
    l=0
    cnt=0
    
    while l<=r<n:
        if s[r]==e:
            if l==0:
                cnt=max(r-l,cnt)
            else:
                cnt=max(r-l-1,cnt)
            l=r
        r+=1
    if n==r:cnt=max(r-l-1,cnt)
    if cnt==0:continue
    res=min(res,cnt)
if res==float('inf'):
    print(0)
else:print(res)

Submission status: Accepted

Solution code:
s=input()

numdict={}
c=set(s)
for i in c:
    numdict[i]=[]

for i in range(len(s)):
    numdict[s[i]].append(i)

cnt=len(s)
for i in c:
    x=0
    #print(i)
    x=max(numdict[i][0],len(s)-numdict[i][-1]-1)
    for j in range(len(numdict[i])-1):
        x=max(x,numdict[i][j+1]-numdict[i][j]-1)
        #print(x)
    #print("final",x)
    cnt=min(cnt,x)
print(cnt)