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 .

Solution (Python):
t = input()+"@"
n = len(t)
alph = "abcdefghijklmnopqrstuvwxyz"

ans = len(t)

for char in alph:
    tmp_ans = 0
    tmp = 0
    for i in range(n):
        if i == n-1:
            tmp_ans = max(tmp_ans, tmp)
        if t[i] == char:
            tmp_ans = max(tmp_ans, tmp)
            tmp = 0
        else:
            tmp += 1
    ans = min(ans, tmp_ans)
print(ans)

Solution (Python):
from string import ascii_lowercase
D = {k:i for i, k in enumerate(ascii_lowercase)}

s = input()
n = len(s)
lst = [[] for _ in range(26)]

for i in range(n):
    lst[D[s[i]]].append(i)


ans = n//2
for i in range(26):
    nums = lst[i]
    if len(nums) > 0:
        tmp = lst[i][0]
        for j in range(len(nums)-1):
            tmp = max(tmp, lst[i][j+1] - lst[i][j] - 1)
        tmp = max(tmp, n - lst[i][-1] - 1)
        ans = min(ans, tmp)

print(ans)

Solution (Python):
s = list(input())

x = ord('a')
n = len(s)
ans = n
for i in range(26):
    z = chr(x + i)
    t = [] + s
    for j in range(n):
        if t.count(z) == n:
            ans = min(j, ans)
        for k in range(len(s) - 1):
            if t[k] == z or t[k+1] == z:
                t[k] = z
        t[n-1-j] = z

print(ans)

Solution (Python):
s = input()

ans = 100
for i in "qwertyuiopasdfghjklzxcvbnm":
    t = s
    cnt = 0
    while len(set(t)) != 1:
        tmp = ""
        for j in range(len(t) - 1):
            if t[j] == i or t[j + 1] == i:
                tmp += i
            else:
                tmp += t[j]
        t = tmp
        cnt += 1
    ans = min(ans, cnt)
print(ans)

Solution (Python):




s = input()
n = len(s)

cand = list(set(s))
ans = n

def simulate(t):
    cnt = 0
    now = s
    while len(set(now)) != 1:
        ne = ""
        for i in range(len(now)-1):
            if t in (now[i], now[i+1]):
                ne += t
            else:
                ne += "@"
        cnt += 1
        now = ne
    return cnt

for ch in cand:
    ans = min(ans, simulate(ch))
print(ans)

Solution (Python):
from collections import Counter

s=input()
c=Counter(s)

test_s=c.keys()
ans=100
for i in test_s:
 ss=i+s+i
 inx=[j for j,x in enumerate(ss) if x==i]
 d_max=0
 for k in range(len(inx)-1):
  d_max=max(d_max,inx[k+1]-inx[k]-1)
 ans=min(ans,d_max)

print(ans)

Solution (Python):
from collections import defaultdict
s = input()
n = len(s)

alp = defaultdict(list)
for i, a in enumerate(s):
    alp[a].append(i)

ans = 10 ** 9
for ind in alp.values():
    ind = [-1]+ind+[n]
    operate = -10**9
    for i, j in zip(ind[:-1], ind[1:]):
        operate = max(operate, j-i-1)

    ans = min(operate, ans)

print(ans)

Solution (Python):
from collections import Counter

s = input()

a = list(Counter(s).keys())   

ans = [0] * len(a)
for i in range(len(a)):
    c = 0
    for j in range(len(s)):
        if s[j] != a[i]:
            c += 1
        else:
            ans[i] = max(ans[i], c)
            c = 0
        if j == len(s)-1:
            ans[i] = max(ans[i], c)
            
print(min(ans))

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

for i in range(N):
    l,r=0,i+1 
    
    t=set(s[l:r])
    
    while r<N:
        l,r=l+1,r+1
        
        t&=set(s[l:r])
        
        if not t:            
            break
    if t:        
        break

print(i)

Solution (Python):
s=input()
trial=set(s)
minval=len(s)
for t in trial:
  childs=list(s.split(t))
  longest=0
  for child in childs:
    longest=max(longest,len(child))
  minval=min(minval,longest)
print(minval)

Solution (Python):
s = list(input())
sets = set(s)

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

cnt = 10 ** 9
for i in range(len(sets)):
    find = list(sets)[i]
    tmps = s
    tmpCnt = 0
    while True:
        tmp = ''
        for j in range(len(tmps) - 1, 0, -1):
            if s[j - 1] == find:
                tmp += tmps[j - 1]
            else:
                tmp += tmps[j]
        tmpCnt += 1
        tmps = tmp[::-1]
        if tmps.count(find) == len(tmps):
            cnt = min(cnt, tmpCnt)
            break

print(cnt)

Solution (Python):

S = input()
ABC = [chr(i) for i in range(ord('a'),ord('z')+1)]
ans = len(S)
for abc in ABC:
    cnt = 0
    max_cnt = 0
    for s in S:
        if s == abc:
            max_cnt = max(max_cnt,cnt)
            cnt = 0
        else:
            cnt += 1
    max_cnt = max(max_cnt,cnt)
    ans = min(ans,max_cnt)
print(ans)

Solution (Python):
S = input()
N = len(S)
ans = float('inf')
for i in range(97,123):
    if S == chr(i)*N:
        ans = 0
        break
    prev = S
    for j in range(1,N):
        string = ''
        flag = True
        for k in range(N-j):
            if prev[k]==chr(i) or prev[k+1]==chr(i):
                string += chr(i)
            else:
                string += prev[k]
                flag = False
        if flag:
            ans = min(ans,j)
            break
        prev = string
print(ans)

Solution (Python):
alphabet = "abcdefghijklmnopqrstuvwxyz"

s = input()
ans = 100
for target in alphabet:
    if target not in s:
        continue

    indexes = []
    for i in range(len(s)):
        if s[i] == target:
            indexes.append(i)
    tmp = 0
    tmp = max(indexes[0], (len(s)-1) - indexes[-1])

    for i in range(len(indexes)-1):
        tmp = max(tmp, (indexes[i+1]-1) - indexes[i])

    ans = min(ans, tmp)

print(ans)

Solution (Python):
S = input()

c = list(set(list(S)))
res = min(max(len(si) for si in S.split(ci)) for ci in c)
print(res)

Solution (Python):
import os
import sys

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7



S = sys.stdin.buffer.readline().decode().rstrip()
S = np.array(list(S), dtype='U1')

ans = INF
for c in S:
    D = np.zeros(len(S), dtype=int)
    cnt = 0
    for i in range(len(S)):
        if S[i] == c:
            cnt = 0
        else:
            cnt += 1
        D[i] = max(D[i], cnt)
    cnt = 0
    for i in reversed(range(len(S))):
        if S[i] == c:
            cnt = 0
        else:
            cnt += 1
        D[i] = max(D[i], cnt)
    ans = min(ans, D.max())
print(ans)

Solution (Python):

import string
s = input()
result = float('inf')
for last in string.ascii_lowercase:
    t = list(s)
    while not all(c == last for c in t):
        for i in range(len(t)):
            if last in t[i : i + 2]:
                t[i] = last
        t.pop()
    result = min(result, len(s) - len(t))
print(result)

Solution (Python):

s = input()

alp = "abcdefghijklmnopqrstuvwxyz"

ans = float("inf")
for al in range(26):

    sc = s

    for j in range(len(s)):

        dic = {}
        for k in sc:
            dic[k] = 1

        if len(dic) == 1:
            
            ans = min(ans,j)
            break

        now = ""
        for k in range(len(s)-j-1):

            if sc[k] == alp[al] or sc[k+1] == alp[al]:
                now += alp[al]
            else:
                now += sc[k]


        for j in now:
            dic[j] = 1

        if len(dic) == 1:
            ans = min(ans,j)

        sc = now

print (ans)

Solution (Python):
import string
s=input()
n=len(s)
ans=100
l=string.ascii_lowercase
for c in l:
    cnt=0
    ts=s[:]
    while True:
        if len(set(ts))==1:
            break
        cnt+=1
        for i in range(n-cnt):
            if ts[i]!=c:
                ts=ts[:i]+ts[i+1]+ts[i+1:]
        ts=ts[:-1]
    ans=min(ans,cnt)
print(ans)

Solution (Python):
S = input()

def test(s, count):
    group = set(S[:count+1])
    b = False
    for g in group:
        if all([g in S[i:i+count+1] for i in range(len(S)-count)]):
            b = True
    return b     


up = 51 
low = -1 
while up > low + 1:
    mid = (up + low) // 2
    if test(S, mid):
        up = mid
    else:
        low = mid
print(up)

Solution (Python):

import collections

s = list(input())
c = collections.Counter(s)
n = len(s)
check_word = []
if len(c) == n:
    ans = n // 2
else:
    common = c.most_common()
    cnt_list = []
    for i in range(len(c)):
        if common[i][1] == 1:
            break
        else:
            now_word = common[i][0]
            
            check_word.append(now_word)
            locate = [i for i, x in enumerate(s) if x == now_word]
            if 0 not in locate:
                locate.insert(0, -1)
            if n-1 not in locate:
                locate.append(n)
            
            diff_locate = [0] * (len(locate) - 1)
            for l in range(len(locate) - 1):
                diff_locate[l] = locate[l+1] - locate[l]
            
            cnt_list.append(max(diff_locate) - 1)
            
            
    if n % 2 == 1:
        if s[n // 2] in check_word:
            pass
        else:
            cnt_list.append(n // 2 )
    else:
        if s[n // 2] in check_word:
            pass
        if s[n // 2 - 1] in check_word:
            pass
        else:
            cnt_list.append(n // 2)
    ans = min(cnt_list)
print(ans)

Solution (Python):
s = list(input())
s_set = list(set(s))
length = len(s)

indexes = []
for a in s_set:
    tmp = [-1]
    for i, v in enumerate(s):
        if v == a:
            tmp.append(i)
    tmp.append(length)
    indexes.append(tmp)

distance = []
for i in indexes:
    tmp = []
    for j in range(len(i) - 1):
        tmp.append(i[j+1] - i[j] - 1)
    distance.append(max(tmp))

print(min(distance))

Solution (Python):
s=input()
set_s=list(set(s))
ans=999999999999999
for i in range(len(set_s)):
    si=set_s[i]
    t=list(s)

    si_ans=0
    while(len(set(t))>1):
        tdash=[]
        for j in range(len(t)-1):
            if(t[j]==si or t[j+1]==si):
                tdash.append(si)
            else:
                tdash.append(t[j])
        t=tdash

        si_ans+=1
    if(ans > si_ans):
        ans=si_ans
print(ans)

Solution (Python):
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()

Solution (Python):
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()

Solution (Python):
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)

Solution (Python):
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): 
    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
    
    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)

Solution (Python):
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()

Solution (Python):
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()

Solution (Python):
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)

Solution (Python):
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)

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


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
        
        ans1+=1
    ans=min(ans,ans1)
print(ans)

Solution (Python):
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)

Solution (Python):
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()

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

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

print(min(ans))

Solution (Python):
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)

Solution (Python):
from collections import Counter

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

print(min(s))

Solution (Python):
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)



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)

Solution (Python):


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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

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

Solution (Python):
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
    
    
    
  else:
    
    if len(tmp) > 0:
      print(length-1)
      exit()
print(0)

Solution (Python):
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:
      
      dp[i][ord(ss)-96]+=1
    for k in range(1,27):
      if dp[i][k]==N-i+1:
        
        print(i-1)
        exit()

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


if __name__ == "__main__":
    resolve()

Solution (Python):
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()

Solution (Python):
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)

Solution (Python):
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()

Solution (Python):
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()

Solution (Python):
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()

Solution (Python):
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))

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

Solution (Python):
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)

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):

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


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

def B():

    return


def C():

    return


def D():

    return


def E():

    return


def F():

    return


if __name__ == "__main__":
    A()

Solution (Python):

import os,re,sys,operator,math,heapq,string
from collections import Counter,deque


from sys import stdin,setrecursionlimit


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
        
        tmp.pop()
    ans=min(ans,cnt)
print(ans)

Solution (Python):
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())

Solution (Python):
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)

Solution (Python):

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



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

if __name__ == '__main__':
    main()

Solution (Python):
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())

Solution (Python):
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)

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):
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()

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

if __name__ == "__main__":
    main()

Solution (Python):
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)