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
stdin = sys.stdin

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

s = ns()
n = len(s)
chars = set(s)

ans = 10**18

for ci in chars:
    cur = s
    if len(set(s)) == 1 and s[0] == ci:
        ans = 0
        break
    
    cnt = 1
    for ln in range(n):
        nex = ""
        for idx in range(len(cur)-1):
            if cur[idx] == ci or cur[idx+1] == ci:
                nex += ci
            else:
                nex += cur[idx]
                
        if len(set(nex)) == 1 and nex[0] == ci:
            break
        
        cnt += 1
        cur = nex
        
    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
    cnt = 0
    s = S
    while any(a!=c for a in s):
        t = ''
        for a,b in zip(s,s[1:]):
            if c==a or c==b:
                t += c
            else:
                t += a
        s = t
        cnt += 1
    ans = min(ans, cnt)
print(ans)

Submission status: Accepted

Solution code:
s=input()
ans=float('inf')
for c in set(s):
    ans=min(ans,max(map(len,s.split(c))))
print(ans)

Submission status: Accepted

Solution code:
s=input()
res=[]
for x in set(s):
    t=s
    while(len(set(t))>1):
        y=""
        for i in range(len(t)-1):
            if t[i]==x:
                y+=x
            elif t[i+1]==x:
                y+=x
            else:
                y+=t[i]
        t=y
    res.append(len(t))
print(len(s)-max(res))

Submission status: Accepted

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

ans = 1000
candidates = set(ss)
for s in candidates:
    count = 0
    t = list(ss)
    while True:
        if len(set(t))==1:
            ans = min(ans, count)
            break
        t = [t[i] if t[i+1] != s else t[i+1] for i in range(len(t)-1)]
        count += 1
print(ans)

Submission status: Accepted

Solution code:
S = input()

min_cost = 10**18
for c in set(S):
    cost = max(map(len, S.split(c)))
    min_cost = min(min_cost, cost)

print(min_cost)

Submission status: Accepted

Solution code:
s=list(input())
p=[chr(ord("a")+i)for i in range(26)]
q=100
if len(list(set(s)))==1:
    print(0)
else:
    for i in range(26):
        t=p[i]
        d=s[:]
        c=0
        while len(d)>0:
            c+=1
            for j in range(len(d)-1):
                if d[j]==t or d[j+1]==t:
                    d[j]=t
            else:
                d.pop(-1)
                if len(list(set(d)))==1 and d[0]==t:
                    break
        q=min(q,c)
    print(q)

Submission status: Accepted

Solution code:
s = input()
n = len(s)
alset = set()
mojiretsu = s
chuumoku = ""
cnt = 0
ans = float("INF")
for i in range(n):
    if s[i] not in alset:
        chuumoku = s[i]
        alset.add(chuumoku)
        while len(set(mojiretsu)) != 1:
            cpmojiretsu = mojiretsu
            mojiretsu = ""
            for j in range(len(cpmojiretsu) - 1):
                if cpmojiretsu[j] == chuumoku or cpmojiretsu[j + 1] == chuumoku:
                    mojiretsu += chuumoku
                else:
                    mojiretsu += s[j]
            cnt += 1
        if ans > cnt:
            ans = cnt
        cnt = 0
        mojiretsu = s
print(ans)

Submission status: Accepted

Solution code:
import string


def solve(S):
    ans = 1000
    for v in string.ascii_lowercase:
        tans = 0
        s = S
        while s.count(v) < len(s):
            tans += 1
            ns = []
            for i in range(len(s) - 1):
                if s[i] == v or s[i + 1] == v:
                    ns.append(v)
                else:
                    ns.append(s[i])
            s = "".join(ns)
        ans = min(ans, tans)

    return ans

if __name__ == "__main__":
    print(solve(input()))

Submission status: Accepted

Solution code:
from collections import Counter


def solve(s, ch):
    ret = 0
    dist = -1
    n = len(s)
    l = [0]*n
    for i in reversed(range(n)):
        if s[i] == ch:
            dist = 0
        elif dist == -1:
            l[i] = dist
        else:
            dist += 1
            l[i] = dist
    if max(l) > l.count(-1):
        ret = max(l)
    else:
        ret = l.count(-1)
    return ret


s = input()
C = Counter(s)
ans = len(s)+2
for i, v in C.most_common():
    ans = min(ans, solve(s, i))
print(ans)

Submission status: Accepted

Solution code:
S = input()

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

Submission status: Accepted

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

import sys, re
from collections import deque, defaultdict, Counter
from math import sqrt, hypot, factorial, pi, sin, cos, radians
if sys.version_info.minor >= 5: from math import gcd
else: from fractions import gcd 
from heapq import heappop, heappush, heapify, heappushpop
from bisect import bisect_left, bisect_right
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from functools import reduce, partial
from fractions import Fraction
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def ceil(a, b=1): return int(-(-a // b))
def round(x): return int((x*2+1) // 2)
def fermat(x, y, MOD): return x * pow(y, MOD-2, MOD) % MOD
def lcm(x, y): return (x * y) // gcd(x, y)
def lcm_list(nums): return reduce(lcm, nums, 1)
def gcd_list(nums): return reduce(gcd, nums, nums[0])
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

S = input()
N = len(S)

c = Counter(S)
ans = INF
# 出現した文字は1つずつ試す
for k, v in c.items():
    S2 = list(S)
    others = len(S2) - S.count(k)
    cnt = 0
    # 他の文字がなくなるまで処理する
    while others > 0:
        for i in range(len(S2)):
            if i == 0: continue
            # 1つ後ろの文字を変える
            if S2[i] == k:
                if S2[i-1] != k:
                    S2[i-1] = k
                    others -= 1
        if S2[-1] != k:
            others -= 1
        # 削る文字は最後尾でいいと思う
        S2.pop(-1)
        cnt += 1
    # 一番短い処理数で済んだものが答え
    ans = min(cnt, ans)
print(ans)

Submission status: Accepted

Solution code:
def check(array, c):
    for i in array:
        if i != c:
            return False
    return True

def generate(s, c):
    ret = s[:-1]
    for i in range(len(s)-1):
        if s[i] == c or s[i+1] == c:
            ret[i] = c
    return ret

s = input()
# cで侵食
ans = float("inf")
for c in set(list(s)):
    use = list(s)
    ans_tmp = 0
    while not check(use, c):
        use = generate(use, c)
        # print("".join(use))
        ans_tmp += 1
    ans = min(ans, ans_tmp)
print(ans)

Submission status: Accepted

Solution code:
s = list(input())

mina = float('inf')
for c in range(97, 123):
    c = chr(c)
    if c not in s:
        continue
    maxa = 0
    tmpa = 0
    for i in range(len(s)):
        if s[i] == c:
            maxa = max(maxa, tmpa)
            tmpa = 0
        else:
            tmpa += 1
    else:
        maxa = max(maxa, tmpa)
    mina = min(mina, maxa)
print(mina)

Submission status: Accepted

Solution code:
import string

s = input()
n = len(s)
ans = n

for chara in set(s):
    left_max = 0
    right_max = 0
    ri = 0
    temp = 0
    for index, c in enumerate(s):
        if c == chara:
            left_max = max(temp, left_max)
            temp = 0
            ri = index
        else:
            temp += 1
    right_max = n - ri - 1
    ans = min(max(right_max, left_max), ans)
print(ans)

Submission status: Accepted

Solution code:
import string

s = input()
n = len(s)
ans = n
for chara in string.ascii_lowercase:
    left_max = 0
    right_max = 0
    ri = 0
    temp = 0
    for index, c in enumerate(s):
        if c == chara:
            left_max = max(temp, left_max)
            temp = 0
            ri = index
        else:
            temp += 1
    right_max = n - ri - 1
    ans = min(max(right_max, left_max), ans)
print(ans)

Submission status: Accepted

Solution code:
s = input()
len_s = len(s)
alp =  [0]*26
for i,j in enumerate(s):
  alp_index = ord(j) - ord("a")
  if alp[alp_index] == 0:
    alp[alp_index] = [i]
  else:
    alp[alp_index].append(i)
max_k = []
for i in alp:
  if i != 0:
    i.append(len_s)
    k = [i[0]]
    for j,l in enumerate(i[1:]):
      k.append(i[j+1]-i[j]-1)
    max_k.append(max(k))
     
print(min(max_k))

Submission status: Accepted

Solution code:
s = input()

al = list(set(s))

al_ans = 100

for ma in al:

    now = 0
    ans = 0

    for i in range(len(s)):
        if s[i]==ma:
            if ans<now:
                ans = now
            now = 0
        else:
            now = now+1
    else:
        if ans<now:
            ans = now
            
    if ans<al_ans:
        al_ans = ans
    
print(al_ans)

Submission status: Accepted

Solution code:
# coding:utf-8

from collections import Counter


def inpl(): return list(map(int, input().split()))


S = input()
C = Counter(S)
if len(S) == 1:
    print(0)
    exit()

ans = 100
for key in C.keys():
    prev_S = list(S)
    cnt = 0
    t = len(S) - 1
    while t > 0:
        if all([1 if s == key else 0 for s in prev_S]):
            ans = min(ans, cnt)
            break
        SS = []
        for i in range(t):
            if key in prev_S[i: i + 2]:
                SS.append(key)
            else:
                SS.append(prev_S[i])
        cnt += 1
        t -= 1
        prev_S = SS[:]
    # print(cnt, prev_S)

print(ans)

Submission status: Accepted

Solution code:

s=input()
c=[]
for i in range(len(s)):
    if not s[i] in c:
        c.append(s[i])
count_max_list = []
for a in c:
    count=0
    count_max = 0
    for i in range(len(s)):
        if a==s[i]:
            count_max=max(count,count_max)
            count=0
        else:
            count+=1
    count_max=max(count,count_max)
    count_max_list.append(count_max)
print(min(count_max_list))

Submission status: Accepted

Solution code:
s = input()
ans = 100
count = 0
a = []

for i in range(97, 97+26):
    for j in range(len(s)):
        if s[j] != chr(i):
            count += 1
        else:
            a.append(count)
            count = 0

    a.append(count)
    maax = max(a)
    if ans > maax:
        ans = maax
    count = 0
    a.clear()

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

inputs = list(input())

count = 10 ** 15
kinds = len(set(inputs))

def cal(inputs, item):
    re = ''
    for index in range(len(inputs)-1):
        if inputs[index] == item or inputs[index+1] == item:
            re += item
        else:
            re += inputs[index]
    return re

if kinds == 1:
    count = 0
else:
    set_inputs = set(inputs)
    for set_input in set_inputs:
        item = inputs[:]
        total = 0
        while len(set(item)) != 1:
            item = list(cal(item, set_input))
            total += 1
        count = min(count, total)

print(count)

Submission status: Accepted

Solution code:
def itoa(i):
    alphabet = ['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']
    return alphabet[i]
s = input()
score_list = []
for i in range(26):
    c = itoa(i)
    prev = -1
    score = 0
    flag=False
    for j in range(len(s)):
        if c==s[j]:
            score=max(j-prev-1,score)
            flag=True
            prev=j
    score = max(j-prev,score)
    if flag==False:
        score=10**3
    score_list.append(score)

print(min(score_list))

Submission status: Accepted

Solution code:
s = input()
res = 10**9
# sをすべてalpにしたい
for alp in set(list(s)):
    t = s
    cnt = 0
    while t!=t[0]*len(t):
        new_t = ''
        for i in range(len(t)-1):
            if alp in (t[i], t[i+1]):
                new_t += alp
            else:
                new_t += '!'
        t = new_t
        cnt += 1
    res = min(res, cnt)
print(res)

Submission status: Accepted

Solution code:
S = input()
ans = min(max(map(len, S.split(c)))
          for c in [chr(i + 97) for i in range(26)]
          if c in S)
print(ans)

Submission status: Accepted

Solution code:
S = input()
ans = len(S)
for c in [chr(i + 97) for i in range(26)]:
    if c not in S:
        continue
    ma = max(map(len, S.split(c)))
    ans = min(ans, ma)
print(ans)

Submission status: Accepted

Solution code:
S = input()
ans = len(S)
for tgt in set(S):
    T = S
    while len(T) != T.count(tgt):
        U = ''
        for ab in zip(T, T[1:]):
            U += tgt if tgt in ab else ab[0]
        T = U
    ans = min(ans, len(S) - len(T))
print(ans)

Submission status: Accepted

Solution code:
import collections

S = input()

dic = collections.Counter(S)

ans = len(S)

for k in dic.keys():
    tmp = S
    tmp_ans = 0
    while True:
        ch = ''
        lens = len(tmp)

        for i,s in enumerate(tmp):
            if ch == '' or ch == s:
                ch = s
            else:
                break

        else:
            break

        for i in range(len(tmp)-1):
            if tmp[i+1] == k:
                tmp = tmp[:i] + k + tmp[i+1:]

        tmp = tmp[:-1]
        tmp_ans += 1


    ans = min(ans,tmp_ans)

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
s = input()
S = set(s)
A = []

for i in S:
    a = 0
    p = s.split(i)
    #print(p)
    for j in range(len(p)):
        if len(p[j]) > a:
            a = len(p[j])
    A.append(a)
#print(A)
print(min(A))

Submission status: Accepted

Solution code:
from collections import Counter
s=input()
S=set(s)
A=[]
for i in S:
    a=0
    My=s.split(i)
    for j in range(len(My)):
        if len(My[j])>a:
            a=len(My[j])
    A.append(a)
print(min(A))

Submission status: Accepted

Solution code:
s = list(input())
s_ord = [ord(e) for e in s]
s_ord_ele = sorted(list(set(s_ord)))
# 1回の操作でできること: 語尾を削る,各文字を前にコピー
def operate(list1,str_ord,count):
    if len(list(set(list1))) == 1:
        return count
    for i in range(len(list1) - 1):
        if list1[i+1] == str_ord:
            list1[i] = list1[i+1]
    del list1[-1]
    return operate(list1,str_ord,count+1)
min_count = 100
for e in s_ord_ele:
    count = operate(s_ord[:],e,0)
    if count < min_count:
        min_count = count
print(min_count)

Submission status: Accepted

Solution code:
s = input()
ans = 1e9
for alpha in "abcdefghijklmnopqrstuvwxyz":
    cur = list(s)
    cnt = 0
    while len(set(cur))>1:
        # print(alpha, cnt, cur)
        for i in range(1,len(cur)):
            if cur[i]==alpha:
                cur[i-1]=alpha
        cur.pop()
        cnt += 1
    ans = min(ans, cnt)
print(ans)

Submission status: Accepted

Solution code:
import string

S = input()
ret = 1000

for c in string.ascii_letters:
    if c not in S:
        continue

    k = 0
    t = S
    nt = ""

    while t.count(c) != len(t):
        k += 1
        nt = ""
        for i in range(len(t) - 1):
            if t[i] == c or t[i + 1] == c:
                nt += c
            else:
                nt += t[i]
        t = nt

    ret = min(ret, k)

print(ret)

Submission status: Accepted

Solution code:
n = input()
# 1回の処理で、1文字減、連続部分1増
# 最悪でも「len(n)/2」回で終了

# 全ての文字に対し
# それぞれで統一する場合を全部試しても良さそう

score = 1000
left = 0
for i in range(len(n)):
    alpha = n[i] # i文字目で統一する場合を考える
    _left = 0
    left = 0
    for j in range(len(n)):
        if n[j] != alpha:
            _left += 1
        else:
            left = max(left, _left)
            _left = 0
    left = max(left, _left)
    score = min(score, left)
print(score)

Submission status: Accepted

Solution code:
s = input()
N = len(s)
table = {}
for i in range(N):
    table[s[i]] = table.get(s[i], [])
    table[s[i]] += [i]
ans = 100
for k, nums in table.items():
    ans = min(ans, max([nums[0], N - 1 - nums[-1]] +
                       [(nums[i + 1] - nums[i] - 1) for i in range(len(nums) - 1)]))
print(ans)

Submission status: Accepted

Solution code:
s=input()
d=[set(i) for i in s]
ans=0
for i in range(101):
    if len(d)==1: break
    tmp=d[0]
    for  j in range(1,len(d)):
        tmp=tmp&d[j]

    if len(tmp)>=1: break
    for j in range(len(d)-1):
        d[j]=d[j]|d[j+1]
    d.pop()
    ans+=1
print(ans)

Submission status: Accepted

Solution code:
s = input()
ans = 100
for i in s:
    dis = 0
    td = 0
    for j in s:
        if i == j:
            dis = max(dis, td)
            td = 0
        else:
            td += 1
        dis = max(dis, td)
    ans = min(ans, dis)
print(ans)

Submission status: Accepted

Solution code:
import sys


def next_str() -> str:
    result = ""
    while True:
        tmp = sys.stdin.read(1)
        if tmp.strip() != "":
            result += tmp
        elif tmp != '\r':
            break
    return result


def main() -> None:
    s = next_str()
    dic = [set() for _ in range(len(s))]

    flg = False

    for i in range(len(s)):
        dic[i].add(s[i])

    while True:
        for c in range(ord('a'), ord('z') + 1):
            for i in range(len(dic)):
                if chr(c) not in dic[i]:
                    break
            else:
                flg = True

        for i in range(len(dic) - 1):
            dic[i] |= dic[i + 1]

        if flg: break
        dic = dic[:-1]

    print(len(s) - len(dic))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S = input()

ans = len(S)
for i in range(26):
    c = chr(ord('a') + i)
    if not c in S: continue
    if all([c==a for a in S]):
        print(0)
        exit()
    arr = list(S)
    tmp = 0
    while 1:
        arr2 = []
        diff = False
        tmp += 1
        for a,b in zip(arr,arr[1:]):
            if a==c or b==c:
                arr2.append(c)
            else:
                arr2.append(a)
                diff = True
        if not diff:
            break
        arr = arr2
    ans = min(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
d = set()
s = list(input())
n = len(s)
for c in s:
    d.add(c)

res = 10**5
for c in d:
    cnt = 0
    ss = list(s)
    nn = n
    # print(ss,nn,c)
    while True:
        if len(set(ss))==1:
            break
        cnt += 1
        for i in range(nn-1):
            if ss[i+1]==c:
                ss[i] = c
        ss.pop()
        nn -= 1
    res = min(res, cnt)
print(res)

Submission status: Accepted

Solution code:

def main():
    buf = input()
    s = buf
    characters = []
    for i in s:
        if not i in characters:
            characters.append(i)
    min_iter_count = 999
    for i in characters:
        iter_count = 0
        t = s
        while True:
            flag = False
            new_t = ""
            for j in range(len(t) - 1):
                if t[j] == i:
                    new_t += i
                elif t[j+1] == i:
                    new_t += i
                    flag = True
                else:
                    new_t += t[j]
                    flag = True
            if t[-1] != i:
                flag = True
            t = new_t
            if flag:
                iter_count += 1
            else:
                break
        if iter_count < min_iter_count:
            min_iter_count = iter_count
    print(min_iter_count)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
S = len(s)
Words = {}
for i in range(len(s)):
    if s[i] not in Words:
        Words[s[i]] = [j for j, x in enumerate(s) if x == s[i]]
Ans = float("inf")
for w in Words:
    Indexes = Words[w]
    change = Indexes[0]
    for i in range(1, len(Indexes)):
        change = max(change, Indexes[i] - Indexes[i-1] - 1)
    change = max(change, S - Indexes[-1] - 1)
    Ans = min(Ans, change)
print(Ans)

Submission status: Accepted

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

    dic = {}
    for i in range(len(s)):
        if s[i] in dic:
            dic[s[i]].append(i)
        else:
            dic[s[i]] = [i]

    ans = 101
    for k in dic:
        dic[k].append(-1)
        dic[k].append(len(s))
        dic[k].sort()

        mx = -1
        for j in range(len(dic[k])-1):
            mx = max(mx, dic[k][j+1]-dic[k][j]-1)

        ans = min(ans, mx)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

str = list("abcdefghijklmnopqrstuvwxyz")
ans = 1e9

for ch in str:
  if not ch in S:
    continue
  
  T = copy.copy(S)

  L = len(T)
  flg = True
  for i in range(L):
    if T[i] != ch:
      flg = False
      break
      
  if flg:
    print(0)
    exit(0)
    
  for cnt in range(1,100):
    flg = True
    for i in range(L-1):
      if T[i+1] == ch:
        T[i] = ch
      elif T[i] == ch:
        pass
      else:
        flg = False
        
    L -= 1
    #print(T[:L],cnt)
    if flg:
      #print(ch,cnt,ans)
      ans = min(ans, cnt)
      break
      
print(ans)

Submission status: Accepted

Solution code:
S = input()
print(min([len(max(S.split(s), key=lambda x: len(x))) for s in set(S)]))

Submission status: Accepted

Solution code:
S = input()
if len(set(S)) == 1:
    print(0)
    exit()
ans = len(S)
for i in range(26):
    seq = [s for s in S]
    p = chr(ord('a')+i)
    for j in range(len(S)-1):
        for k in range(len(S)-j-1):
            if seq[k+1] == p:
                seq[k] = seq[k+1]
        for k in range(len(S)-j-1):
            if seq[k] != p:
                break
        else:
            ans = min(ans, j+1)
            break
print(ans)

Submission status: Accepted

Solution code:
S = input()
P = list(set(S))
ANS = []
for i in range(len(P)) :
    T = list(S.split(P[i]))
    U = []
    for j in range(len(T)) :
        U.append(len(T[j]))
    ANS.append(max(U))
print(min(ANS))

Submission status: Accepted

Solution code:
S = input()
ANS = []
for i in range(ord("a"),ord("z")+1) :
    p = chr(i)
    T = list(S.split(p))
    U = []
    for j in range(len(T)) :
        U.append(len(T[j]))
    ANS.append(max(U))
print(min(ANS))

Submission status: Accepted

Solution code:
s = input()

ans = [0 for i in range(ord('z')-ord('a')+1)]
for i in range(ord('a'),ord('z')+1):
    c = chr(i)
    s2 = s.split(c)
    ans[i-ord('a')] = max([len(ss) for ss in s2])
print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
s = input()
x = Counter(s).most_common()
ans = 100
for p in x:
    ch = p[0]
    t = list(s)
    num = 0
    while set(t) != set(ch):
        num += 1
        temp = tuple(t)
        k = len(t)
        for i in range(k-1):
            if temp[i+1] == ch:
                t[i] = ch
        t = t[:-1]
    ans = min(num, ans)
print(ans)

Submission status: Accepted

Solution code:
N = input()

from collections import Counter

counter = Counter(N)
answer = float("inf")

for c in counter:

    margin = 0

    items = list(N.split(c))

    margin = 0
    for item in items:
        margin = max(margin, len(item))

    answer = min(answer, margin)

print(answer)

Submission status: Accepted

Solution code:
N = input()

from collections import Counter

counter = Counter(N)
answer = float("inf")

for c in counter:

    margin = 0

    items = list(N.split(c))
    first = items.pop(0)
    last = items.pop()
    margin = max(len(first), len(last))

    for item in items:
        margin = max(margin, len(item))

    answer = min(answer, margin)

print(answer)

Submission status: Accepted

Solution code:
s=list(input())
l=[chr(i) for i in range(97,97+26)]
lc=[]
for j in l:
  if j in s:
    c=0
    t=s
    while len(set(t))!=1:
      u=[]
      for k in range(len(t)-1):
        if t[k]==j or t[k+1]==j:u.append(j)
        else:u.append(t[k])
      t=u
      c+=1
    lc.append(c)
print(min(lc))

Submission status: Accepted

Solution code:
ss = input()

ans = []
for c in 'abcdefghijklmnopqrstuvwxyz':
    ansC = 0
    num = 0
    for s in ss:
        if s != c:
            num += 1
        else:
            ansC = max(ansC, num)
            num = 0
    ansC = max(ansC, num)
    ans.append(ansC)

print(min(ans))

Submission status: Accepted

Solution code:
s=raw_input()
result=len(s)
for i in range(0, 26):
	cur_result=0
	ch=chr(ord("a")+i)
	s+=ch
	cur=-1
	while True:
		next=s.find(ch, cur+1)
		if next==-1:
			break
		cur_result=max(cur_result, next-cur-1)
		cur=next
	s=s[:-1]
	result=min(result, cur_result)
print result

Submission status: Accepted

Solution code:
s_ = input()
answ = float("inf")
for i in s_:
    s = s_
    ans = 0
    while len(set(s)) != 1:
        a = ""
        for j in range(len(s) - 1):
            if s[j] == i or s[j + 1] == i:
                a += i
            else:
                a += s[j]
        ans += 1
        s = a
    answ = min(answ, ans)
print(answ)

Submission status: Accepted

Solution code:
ans_dict = {}
s = str(input())
list_s = list(s)
list_al = list("abcdefghijklmnopqrstuvwxyz")
for i in range(26):
    ans_dict[list_al[i]] = [-1]

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

for i in range(26):
    ans_dict[list_al[i]].append(len(list_s))
for key, value in ans_dict.items():
    ans_dict[key] = max([(value[i] - value[i-1]) for i in range(1,len(value))]) - 1

print(min(ans_dict.values()))

Submission status: Accepted

Solution code:
def c(l, x):
    return [i for i, _x in enumerate(l) if _x == x]
s = list(input())
ass = 1000000
for i in range(len(s)):
    r = c(s,s[i])
    ans = 0
    for i in range(len(r)-1):
        ans = max(ans,r[i+1]-r[i]-1)
    ans = max(r[0],ans,len(s)-1-r[-1])
    
    ass = min(ass,ans)
    #print(ass,r)
if len(list(set(s))) == 0:
    print(0)
else:
    print(ass)

Submission status: Accepted

Solution code:
s = input()

from collections import Counter

count = Counter(s)

ans = float('inf')

for c in count:
    
    m_l = 0

    items = list(s.split(c))

    first = items.pop(0)
    last = items.pop()

    m_l = max(len(first),len(last))

    for item in items:
        m_l = max(m_l,len(item))

    ans = min(ans,m_l)

print(ans)

Submission status: Accepted

Solution code:
st = list(str(input()))
s = [st[i] for i in range(len(st))]
ans = []
lis = []
for k in range(len(s)):
  lis = st
  while lis != [s[k] for _ in range(len(lis))]:
    cou = []
    for i in range(len(lis)-1):
      if lis[i+1] == s[k] or lis[i] == s[k]:
        cou.append(s[k])
      else:
        cou.append("X")
    lis = cou
  ans.append(len(lis))
print(len(s)-max(ans))

Submission status: Accepted

Solution code:
s = input()

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

res = float('inf')
for c in alphabet:
    tmp = 0
    t = s
    while t!=t[0]*len(t):
        u = ''
        for d,e in zip(t[::-1],t[::-1][1:]):
            if d==c or e==c:
                u += c
            else:
                u += d
        tmp += 1
        t = u[::-1]
    res = min(res,tmp)
print(res)

Submission status: Accepted

Solution code:
def ope(s,c):
    ret = ""
    for i in range(len(s)-1):
        if s[i] == c or s[i+1] == c:
            ret += c
        else:
            ret += s[i]
    return ret

def is_filled(s,c):
    for i in s:
        if i != c:
            return False
    else:
        return True

def main():
    s = input()
    ans = 114514
    for i in range(97,97+26):
        c = chr(i)
        if not c in s: continue
        si = s

        turn = 0
        while not is_filled(si,c):
            si = ope(si,c)
            turn += 1
        
        ans = min(ans,turn)
    print(ans)

        
main()

Submission status: Accepted

Solution code:
from copy import deepcopy

s = input()
ans = len(s)-1
for i in range(26):
    tc = chr(ord("a")+i)
    tv = 0
    ts = deepcopy(s)
    for j in range(len(s)):
        if ts.count(tc) == len(ts):
            ans = min(ans, tv)
            break
        nxt = ""
        for k in range(len(ts)-1):
            if ts[k] == tc or ts[k+1] == tc:
                nxt += tc
            else:
                nxt += ts[k]
        tv += 1
        ts = deepcopy(nxt)
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())

S = input()
L = len(S)

def cal(s): return ord(s)-97

bits = [0]*L
for i,s in enumerate(S):
	bits[i] = 1<<cal(s)

ans = 0
while True:
	#全部に立ってるものがあるか
	for x in range(26):
		flag = True
		for bit in bits:
			if not (bit>>x) & 1:
				flag = False
				break
		if flag:
			print(ans)
			sys.exit()

	#更新
	for i in range(L-1):
		bits[i] |= bits[i+1]
	bits.pop(-1)
	L -= 1
	ans += 1

Submission status: Accepted

Solution code:
s = input()
ans = float("inf")
for i in range(len(s)):
  ls = [c for c in s]
  k = ls[i]
  if ls.count(k) == len(ls):
    ans = 0
    break
  for l in range(len(s)):
    ls1 =[]
    for j in range(len(ls)-1):
      if (ls[j] == k or ls[j+1] == k):
        ls1.append(k)
      else:
        ls1.append(0)
    ls = ls1
    if ls.count(k) == len(ls):
      ans = min(ans,l+1)
      break
print(ans)

Submission status: Accepted

Solution code:
s = input()
n = 0
ans = 10**10
for k in 'qwertyuiopasdfghjklzxcvbnm':
    l = [i if i == k else 0 for i in s]
    a = 0
    while l.count(k)!=len(l):
        for i in range(len(l)-1):
            if l[i]==k or l[i+1]==k:
                l[i] = k
        a += 1
        l.pop(-1)
    ans = min(ans,a)
    n += 1
print(ans)