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:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache


#############
# Constants #
#############

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

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
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

#####MakeDivisors######
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)
  return divisors

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

AtoZ = "abcdefghijklmnopqrstuvwxyz"
S = S()
N = len(S)
MIN = INF
for l in AtoZ:
  indexes = [i for i, x in enumerate(S) if x == l]
  indexes = [-1]+indexes+[N]
  dif = [indexes[i+1]-indexes[i] for i in range(len(indexes)-1)]
  MIN = min(MIN,max(dif)-1)
print(MIN)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10**6)

def handling(string, letter):
    global cnt
    cnt += 1; nxt = []
    for i in range(len(string)-1):
        s1, s2 = string[i], string[i+1]
        if s1 == letter:
            nxt.append(s1)
        elif s2 == letter:
            nxt.append(s2)
        else:
            nxt.append(s1)
    if len(set(nxt)) != 1:
        handling(nxt, letter)

def main():
    global cnt
    s = list(input())
    sletter = set(s)
    if len(sletter) == 1:
        print(0)
        return
    ans = 101
    for l in sletter:
        cnt = 0
        t = s.copy()
        handling(t, l)
        ans = min(ans, cnt)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
s=input()

n=10000000
for i in range(26):
    lis=[-1]
    for c in range(len(s)):
        if s[c]==chr(ord("a")+i):
            lis.append(c)
    if len(lis)>1:
        ni=0
        for j in range(len(lis)-1):
            ni=max(ni,lis[j+1]-lis[j]-1)
        ni=max(ni,len(s)-1-lis[len(lis)-1])
        n=min(n,ni)

print(n)

Submission status: Accepted

Solution code:
s = input()
al = 'abcdefghijklmnopqrstuvwxyz'
cou = 10**7
for i in range(26):
  s1 = s
  s2 = s
  w = al[i]
  cou_now = 0
  while len(set(s2))!=1:
   # print(s2)
    cou_now += 1
    s1 = str("".join(s2))
    s2 = []
    for j in range(len(s1)-1):
      if s1[j]==w or s1[j+1]==w:
        s2.append(w)
      else:
        s2.append(s1[j])
  cou = min(cou,cou_now)
print(cou)

Submission status: Accepted

Solution code:
S = input()

if len(set(S)) == 1:
    print(0)
else:
    alpha = list(set(S))
    cnt = float("inf")
    for word in alpha:
        t = S[:]
        for i in range(1, len(S)):
            tmp = ""
            for j in range(len(t)-1):
                if t[j] == word or t[j+1] == word:
                    tmp += word
                else:
                    tmp += t[j]
            if len(set(tmp)) == 1:
                cnt = min(cnt, i)
                break
            t = tmp[:]
    print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

input_methods=['clipboard','file','key']
using_method=1
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007

#+++++

def is_all(list_s, c):
	for cs in list_s:
		if cs != c:
			return False
	return True

def dd(s, c):
	t=list(s)
	p=0
	while not is_all(t, c):
		nt=['']*(len(t)-1)
		for i, _ in enumerate(nt):
			if t[i]==c or t[i+1]==c:
				nt[i]=c
			else:
				nt[i]=t[i]
		t=nt
		p+=1
	return p
	
	

def main():
	#a = int(input())
	#b , c = tin()
	s = input()
	ret = mod
	for c in list(set(list(s))):
		#pa(c)
		r = dd(s, c)
		ret = min(ret, r)
	print(ret)
	
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

Solution code:
def main():
    s = input()
    l = len(s)
    alp = set(s)

    ans = float('inf')
    for char in alp:
        sp = max(map(len, s.split(char)))
        ans = min(ans, sp)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
from string import ascii_lowercase as a2z

input = sys.stdin.readline


def main():
    S = input().rstrip()

    ans = 100
    for al in a2z:
        if al not in S:
            continue
        i = 0
        max_cost = 0
        for j, s in enumerate(S + al):
            if s == al:
                cost = j - i
                if cost > max_cost:
                    max_cost = cost
                i = j + 1
        if max_cost < ans:
            ans = max_cost

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
string = input()
alpha = "abcdefghijklmnopqrstuvwxyz"
ans = 100
for a in alpha:
    tmp = 0
    ans_tmp = 0
    for s in string:
        if s == a:
            ans_tmp = max(ans_tmp, tmp)
            tmp = 0
        else:
            tmp += 1
    ans_tmp = max(ans_tmp, tmp)
    ans = min(ans, ans_tmp)
print(ans)

Submission status: Accepted

Solution code:
s = input()
exitLower = list(set(list(s)))
m = len(s)

for alp in exitLower:
    SL = list(s)
    cnt = 0
    while len(set(SL)) > 1:
        for i in range(len(SL)-1):
            if SL[i+1] == alp:
                SL[i] = alp
        SL.pop()
        cnt += 1
    m = min(m,cnt)

print(m)

Submission status: Accepted

Solution code:
S = input()
A = list(set(S))
minv = len(S) // 2
for a in A:
	maxv = 0
	v = 0
	for c in S:
		if a == c:
			v = 0
		else:
			v += 1
			maxv = max(maxv, v)
	minv = min(minv, maxv)
print(minv)

Submission status: Accepted

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

# 求めなくてはいけないアルファベットは、sに含まれているものだけ
abc_kind = set(s)
ans = n
for x in abc_kind:
    # 何も考えず、シミュレーションする
    # xを残すように操作していく
    cnt = 0
    t = s
    while len(t) != t.count(x):
        t = ''.join([x if x in (a, b) else a for a, b in zip(t, t[1:])])
        cnt += 1
    if cnt < ans:
        ans = cnt
print(ans)

Submission status: Accepted

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


for i in range(n):
    # i回の操作でそれぞれのアルファベットについて
    # 全て同じ文字にできるかどうか
    table = [[0]*(n-i) for _ in range(26)]

    for j in range(n):
        x = s[j]
        x = ord(x)-ord('a')
        L = max(0, j-i)
        if table[x][L]:
            L += 1
        R = min(j+1, L+i+1, n-i)
        for k in range(L, R):
            table[x][k] = 1

    if any(0 not in row for row in table):
        print(i)
        break

Submission status: Accepted

Solution code:
#A
import copy

s=list(input())
times=[0]*26

ans=float('inf')


for i in s:
  temp=copy.deepcopy(s)
  tempans=0
  while True:
    #print(i,temp.count(i), len(temp))
    if temp.count(i)==len(temp):
      ans=min(ans,tempans)
      break
    else:
      for j in range(len(temp)-1):
        if temp[j+1]==i:
          temp[j]=i
      tempans+=1
      del temp[-1]
      #print(temp,tempans)
      
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

s = list(input())
n = len(s)

cnt = defaultdict(list)

for i in range(n):
    cnt[s[i]].append(i)
ans = float("inf")
for k, l in cnt.items():
    res = l[0]
    for i in range(1, len(l)):
        res = max(res, l[i] - l[i-1] - 1)
    res = max(res, n - l[-1] - 1)
    ans = min(ans, res)

print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    S = input().strip()

    ans = len(S)
    for i in range(len(S)):
        tmp = 0
        ma = 0
        for j in range(i + 1):
            if S[i] == S[j]:
                ma = max(ma, tmp)
                tmp = 0
            else:
                tmp += 1
        ans = min(ans, max(ma, len(S) - i - 1))

    print(ans)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S = input()
res = len(S)
kind_all = set(S)
for i in kind_all:
    temp = S.split(i)
    cnt = 0
    for j in temp:
        cnt = max(cnt,len(j))
    res = min(res,cnt)
print(res)

Submission status: Accepted

Solution code:
import collections

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

lm = list()
for i in list(c.keys()):
    l = [-1]
    for j in range(len(s)):
        if s[j] == i:
            l.append(j)
    l.append(len(s))
    lm.append(max([l[k+1]-l[k]-1 for k in range(len(l)-1)]))

print(min(lm))

Submission status: Accepted

Solution code:
s = input()
ans = 10 ** 10
for i in range(26):
    c = chr(ord("a") + i)
    s_c = list(s)
    cnt = 0
    while len(set(s_c)) != 1:
        t = ""
        for i in range(len(s_c) - 1):
            if s_c[i + 1] == c:
                t += c
            else:
                t += s_c[i]
        cnt += 1
        s_c = t
    ans = min(ans, cnt)

print(ans)

Submission status: Accepted

Solution code:
s = input()
ans = 10 ** 10
for i in range(26):
    c = chr(ord("a") + i)
    s_c = list(s)
    cnt = 0
    while len(set(s_c)) != 1:
        t = ""
        for i in range(len(s_c) - 1):
            if s_c[i] == c or s_c[i + 1] == c:
                t += c
            else:
                t += s_c[i]
        cnt += 1
        s_c = t
    ans = min(ans, cnt)

print(ans)

Submission status: Accepted

Solution code:
s = input()
# 97=122
ans = 100
for i in range(97, 123):
    c = chr(i)
    # シュミレーションする
    t = s
    while True:
        if t.count(c) == len(t):
            ans = min(ans, len(s) - len(t))
            break
        new = ""
        for j in range(len(t) - 1):
            if t[j] == c or t[j + 1] == c:
                new += c
            else:
                new += t[j]
        t = new
print(ans)

Submission status: Accepted

Solution code:
import sys

# import re
import math
import collections
# import decimal
import bisect
import itertools
import fractions
# import functools
import copy
import heapq
import decimal
# import statistics
import queue

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    s = list(input())

    l = len(s)
    ans = l // 2

    for i in range(97, 97 + 26):
        word = chr(i)
        cnt = 0
        tmp_max = -1
        for j in range(l):
            if s[j] != word:
                cnt += 1
            else:
                tmp_max = max(tmp_max, cnt)
                cnt = 0
        ans = min(ans, max(tmp_max, cnt))

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s=input()
l=[chr(i) for i in range(ord("a"),ord("z")+1)]
a=100
for i in l:
    t=0
    c=0
    for j in s:
        if j!=i:
            c+=1
        else:
            if t<c:
                t=c
            c=0
    t=max(t,c)
    if a>t:
        a=t
print(a)

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    import math
    #from math import gcd

    #inf = 10**17
    #mod = 10**9 + 7

    s = input().rstrip()
    ss = set(s)

    def test(s, ss):
        res = 0
        while len(set(s)) > 1:
            temp = ''
            for i in range(len(s)-1):
                if s[i] == ss or s[i+1] == ss:
                    temp += ss
                else:
                    temp += s[i]
            s = temp[:]
            res += 1

        return res

    res = 10000000000
    for i in ss:
        res = min(res, test(s, i))
    print(res)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
ans = 100
for i in range(26):
    cur = 0
    c = chr(ord('a') + i)
    t = s
    if(not c in t): continue
    t2 = ""
    while(t.count(c) != len(t)):
        for j in range(len(t) - 1):
            if(t[j] == c or t[j + 1] == c):
                t2 += c
            else:
                t2 += 'A'
        cur += 1
        t = t2
        t2 = ""
    ans = min(cur, ans)
print(ans)

Submission status: Accepted

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

ans = len(S) - 1

# -1,1
for s in uni_s:
  L = len(S)
  T = list(S)
  all_same = [s == t for t in S]
  cnt = 0
  while not all(all_same):
    cnt += 1
    newT = [False] * (L-1)
    for i in range(L):
      t = T[i]
      if s == t:
        if i < L-1:
          newT[i] = t
        if 0 <= i-1:
          newT[i-1] = t
      else:
        if i < L-1 and not newT[i]:
          newT[i] = t
        if 0 <= i-1 and not newT[i-1]:
          newT[i-1] = t
    
    all_same = [s==t for t in newT]
    T = newT
    L = len(T)
  ans = min(ans,cnt)

print(ans)

Submission status: Accepted

Solution code:
s = input()
from string import ascii_lowercase as abc
ans = 1000
for i in abc:
    ans = min(ans, max(list(map(len, s.split(i)))))
print(ans)

Submission status: Accepted

Solution code:
s = input()
r = 100;
for x in range(97, 97+26):
  cnt = 0
  m = 0
  for c in s:
    if c == chr(x):
      m = max(m, cnt)
      cnt = 0
    else:
      cnt += 1
  m = max(m, cnt)
  r = min(r, m)
print(r)

Submission status: Accepted

Solution code:
S = str(input())
ans = 10 ** 9 + 7
for i in range(ord('a'), ord('z')+1):
    ans = min(ans, max([len(s) for s in S.split(chr(i))]))

print(ans)

Submission status: Accepted

Solution code:
import copy
s = input()
ans=10000
for i in range(26):
    now = chr(97+i)
    a = copy.copy(s)
   # print(a)
    count=0
    while len(set(a))!=1:
        a_now=""
        for i in range(len(a)-1):
            if a[i] == now or a[i+1] == now:
                a_now += now
            else:
                a_now += a[i]
        #print(a_now)
        a = copy.copy(a_now)
        count+=1
    #print(count,chr(97+i))
    ans=min(ans,count)
print(ans)

Submission status: Accepted

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

S = readline().rstrip()

ans = 10 ** 10
for i in range(ord("a"),ord("z") + 1):
  ans = min(ans, max(map(len,S.split(chr(i)))))
  
print(ans)

Submission status: Accepted

Solution code:
# ある文字を起点としたとき、ある文字同士に挟まれた区間の最大値が操作回数
# jackal -> aに挟まれた最大区間は2
# 100文字で、アルファベット26種類なので全通り試す

import sys
readline = sys.stdin.readline

S = readline().rstrip()

a = ord("a")
z = ord("z")

ans = 10 ** 10
for i in range(a,z + 1):
  arr = S.split(chr(i))
  maxlen = 0
  for a in arr:
    if len(a) > maxlen:
      maxlen = len(a)
  if maxlen < ans:
    ans = maxlen
  
print(ans)

Submission status: Accepted

Solution code:
s = input()
ans = len(s)
for c in set(s):
    new_s = s
    while len(set(new_s)) > 1:
        tmp = ""
        for i in range(len(new_s) - 1):
            tmp += c if new_s[i] == c or new_s[i + 1] == c else new_s[i]
        new_s = tmp
    ans = min(ans, len(s) - len(new_s))
print(ans)

Submission status: Accepted

Solution code:
import math
#import numpy as np
import queue
from collections import deque,defaultdict
import heapq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    s = input()
    st = set(s)
    mi = 10**18
    for i in st:
        ma = 0
        nr = 0
        for j in s:
            if j == i:
                if ma < nr:
                    ma = nr
                nr = 0
            else:
                nr += 1
        if nr > ma:
            ma = nr
        if mi > ma:
            mi = ma
    print(mi)

    return

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter
S = list(input())
abc = [chr(ord('a') + i) for i in range(26)]

ans = 100000
for s in abc:
    result = S
    count = 0
    while len(set(result)) > 1:
        count += 1
        tmp = ["dd"] * (len(result) - 1)
        for i in range(len(result)-1):
            if result[i] == s or result[i+1] == s:
                tmp[i] = s
            else:
                tmp[i] = result[i]
        result = tmp
    ans = min(ans, count)

print(ans)

Submission status: Accepted

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

ans = float('inf')
for l in s:
    cnl = 0
    anl = 0
    for s_ in s:
        if l == s_:
            anl = max(anl, cnl)
            cnl = 0
        else:
            cnl += 1
    anl = max(anl, cnl)
    ans = min(ans, anl)

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
s = input()
c = Counter(s)
ans = float("inf")
for i in c.keys():
    t = list(s)
    while t != [i]*len(t):
        u = []
        for j in range(len(t)-1):
            if t[j] == i or t[j+1] == i:
                u.append(i)
                continue
            u.append(t[j])
        t = u
    ans = min(ans,len(s)-len(t))
print(ans)

Submission status: Accepted

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
import math
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
from collections import deque

inf = 10 ** 15
mod = 10 ** 9 + 7

s = input()
#アルファベット
al=[chr(ord('a') + i) for i in range(26)]
ans = inf
for c in al:
    pre = 0
    maxpre = 0
    for now in s:
        if c == now:
            maxpre = max(pre, maxpre)
            pre = 0
        else:
            pre += 1
    maxpre = max(pre, maxpre)
    ans = min(ans, maxpre)
print(ans)

Submission status: Accepted

Solution code:
s = input()
def f(s,c):
    result = 0
    while len(set(s)) != 1:
        t = ""
        for i in range(len(s)-1):
            #print(s)
            if s[i] == c or s[i+1] == c:
                #print(s[i],s[i+1],c)
                t += c
            else:
                t += s[i]
        #print(t)
        s = t
        result += 1
    return result
result = float('inf')
for c in set(s):
    result = min(result, f(s,c))
print(result)

Submission status: Accepted

Solution code:
s = input();
a = "abcdefghijklmnopqrstuvwxyz"
b = [0]*26
c = [0]*26
for i in s:
  for j in range(26):
    if i == a[j]:
      c[j] = max(c[j],b[j])
      b[j] = 0
    else:
      b[j] += 1
print(min([max(i,j) for i,j in zip(b,c)]))

Submission status: Accepted

Solution code:
s = input()
s_str = set(list(s))
ans = 101
for char in s_str:
    t = s
    count = 0
    while len(set(list(t))) > 1:
        next_t = ''        
        for i in range(len(t) - 1):
            if (t[i] == char) | (t[i + 1] == char):
                next_t += char
            else:
                next_t += t[i]
        t = next_t
        count += 1
    ans = min(ans, count)
print(ans)

Submission status: Accepted

Solution code:
alphabetlist=['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=input()
for i in range(1,len(s)):
    if s[i]!=s[0]:
        break
else:
    print(0)
    exit()

ans=100
for moji in alphabetlist:
    count=0
    test=s
    while test:
        new=""
        flag=True
        for i in range(0,len(test)-1):
            if test[i]!=moji and test[i+1]!=moji:
                new+=test[i]
                flag=False
            else:
                new+=moji
        count+=1
        test=new
        if flag:
            break
    ans=min(ans,count)

print(ans)

Submission status: Accepted

Solution code:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
#======================================================#
def main():
    s = IS()
    cnt = len(s)
    for c in set(s):
        dist = 0
        max_d = 0
        for si in s:
            if c != si:
                dist += 1
            else:
                max_d = max(max_d, dist)
                dist = 0
        cnt = min(cnt, max(max_d, dist))
    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
## dp ##
def DD2(d1,d2,init=0): return [[init]*d2 for _ in range(d1)]
def DD3(d1,d2,d3,init=0): return [DD2(d2,d3,init) for _ in range(d1)]
## math ##
def divc(x,y) -> int: return -(-x//y)
def divf(x,y) -> int: return x//y
def gcd(x,y):
    while y: x,y = y,x%y
    return x
def lcm(x,y): return x*y//gcd(x,y)
def enumerate_divs(n):
    """Return a tuple list of divisor of n"""
    return [(i,n//i) for i in range(1,int(n**0.5)+1) if n%i==0]
def get_primes(n=10**3):
    """Return a list of prime numbers n or less"""
    is_prime = [True]*(n+1)
    is_prime[0] = is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i*2, n+1, i): is_prime[j] = False
    return [i for i in range(n+1) if is_prime[i]]
def prime_factor(n):
    """Return a list of prime factorization numbers of n"""
    res = []
    for i in range(2,int(n**0.5)+1):
        while n%i==0: res.append(i); n //= i
    if n != 1: res.append(n)
    return res
## const ##
MOD=10**9+7
## libs ##
import itertools as it
import functools as ft
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left
import string
#======================================================#
def main():
    s = IS()
    n = len(s)
    sc = set(s)
    ans = 10**9
    for c in string.ascii_lowercase:
        if c not in sc: continue
        cnt = 0
        t = s
        for l in range(n):
            u = ''
            if set(t) == {c}: break
            for i in range(len(t)-1):
                if t[i] == c or t[i+1] == c:
                    u += c
                else:
                    u += t[i]
            cnt += 1
            t = u
        ans = min(cnt, ans)
    print(ans)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
a = [[-1] for i in range(26)]

for i in range(len(s)):
    a[ord(s[i])-ord('a')].append(i)

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

ans = float('inf')
for i in range(26):
    ma = 0
    for j in range(1,len(a[i])):
        dis = a[i][j]-a[i][j-1]-1
        ma = max(dis,ma)
    ans = min(ma,ans)

print(ans)

Submission status: Accepted

Solution code:
S = input()
N = len(S)
s_set = set(S)
if len(s_set) == 1:
  print(0)
  quit()

ans = 100

def change_to_s(s):
  flag = True
  T = S
  while flag:
    _T = ''
    flag = False
    for i in range(len(T)-1):
      if s in [T[i], T[i+1]]:
        _T += s
      else:
        _T += T[i]
        flag = True
    T = _T
  return N - len(_T), _T

for s in s_set:
  n, t = change_to_s(s)
  ans = min(ans, n)

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
s = input()
c = Counter(s)
ans = float("inf")
for i in c.keys():
    s2 = list(s)
    cnt = 0
    while s2:
        if len(set(s2)) == 1:
            break
        for j in range(len(s2) - 1):
            if s2[j] == i or s2[j + 1] == i:
                s2[j] = i
            else:
                continue
        s2 = s2[:-1]
        cnt += 1
    ans = min(ans, cnt)
print(ans)

Submission status: Accepted

Solution code:
s = input().strip()
l = len(s)

def f(s,c,l,v=1):
    n = 0
    for i in range(l-1):
        if s[i] != c:
            if s[i+1] == c:
                s = s[:i] + c + s[i+1:]
                n += 1
        else:
            n += 1
    # print(s[:-1],v)
    if n < l-1:
        return f(s[:-1], c, l-1, v+1)
    else:
        return v

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

min = l 
for i in set(s):
    x = f(s, i, l)
    if min > x:
        min = x
print(min)

Submission status: Accepted

Solution code:
from collections import Counter

s = input()
s_len = len(s)
c = Counter(s)
ans = float('inf')
for k in c.keys():
    tmp = list(s)
    cnt = 0
    while True:
        if len(set(tmp)) == 1:
            break
        for i in range(len(tmp)-1):
            if tmp[i] == k:
                tmp[i] = k
            elif tmp[i+1] == k:
                tmp[i] = k
            else:
                continue
        else:
            tmp = tmp[:-1]
            cnt += 1
    ans = min(ans,cnt)
print(ans)

Submission status: Accepted

Solution code:
s = input()
set_s = set(s)
ss = s[:]
t = []
_min = 10**7

for j in set_s:
    ss = s[:]
    t = []
    cnt = 0
    while len(set(ss)) != 1:
        for i in range(len(ss) - 1):
            if ss[i] == j or ss[i+1] == j:
                t.append(j)
            else:
                t.append(ss[i+1])
        ss = t[:]
        t.clear()
        cnt += 1
    
    _min = min(_min,cnt)    
print(_min)

Submission status: Accepted

Solution code:
s = input()


sl = list(s)


ans = 101

for i in range(26):
    cur_char = chr(ord('a') + i)

    if not cur_char in sl:
        continue

    sl_cp = sl.copy()

    while not len(set(sl_cp)) == 1:

        for i in range(len(sl_cp)-1):
            if sl_cp[i+1] == cur_char:
                sl_cp[i] = cur_char

        sl_cp.pop()

    ans = min(ans, len(sl) - len(sl_cp))

print(ans)

Submission status: Accepted

Solution code:
s = str(input())
S = list(set(s))
d = {}
for v in S:
    x = s
    cnt = 0
    while x.count(v) < len(x):
        new_x = [0] * (len(x) - 1)
        for i in range(len(x) - 1):
            if x[i] == v or x[i + 1] == v:
                new_x[i] = v
        cnt += 1
        x = new_x
    d[v] = cnt
print(min(d.values()))

Submission status: Accepted

Solution code:
s = list(input())
al = [chr(i) for i in range(97, 97+26)]
ans = []

for a in al:
    c = 0
    if s.count(a) == 0:
        continue
    temp = s.copy()
    while not all(i == a for i in temp):
        c += 1
        n_temp = temp.copy()
        for n, j in enumerate(temp):
            if n == 0 and j == a:
                continue
            elif j == a:
                n_temp[n-1] = a
        temp = n_temp[:len(temp)-1]
    ans.append(c)
print(min(ans))

Submission status: Accepted

Solution code:
s = input()
ans = 10**18
for i in range(97,97+26):
  x = chr(i)
  ls = []
  tt = 0
  for j in s:
    if j != x:
      tt += 1
    else:
      ls.append(tt)
      tt = 0
  ls.append(tt)
  ans = min(ans,max(ls))
print(ans)

Submission status: Accepted

Solution code:
import copy
S = list(map(str, input().rstrip()))

vari = set(S)

ans = float("inf")

for v in vari:
    s = copy.deepcopy(S)
    while len(s) != s.count(v):
        for i in range(1, len(s)):
            if s[i] == v:
                s[i - 1] = v
        s = s[:-1]
    ans = min(ans, len(S) - len(s))

print(ans)