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 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def roundUp(a, b):
        return -(-a // b)

    @staticmethod
    def toUpperMultiple(a, x):
        return Math.roundUp(a, x) * x

    @staticmethod
    def toLowerMultiple(a, x):
        return (a // x) * x

    @staticmethod
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

    @staticmethod
    def sign(n):
        if n == 0:
            return 0
        if n < 0:
            return -1
        return 1

    @staticmethod
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


class PriorityQueue:
    def __init__(self, l=[]):
        self.__q = l
        heapq.heapify(self.__q)
        return

    def push(self, n):
        heapq.heappush(self.__q, n)
        return

    def pop(self):
        return heapq.heappop(self.__q)


MOD = int(1e09) + 7
INF = int(1e15)


def main():
    # sys.stdin = open("sample.txt")
    S = Scanner.string()
    ans = INF
    for c in string.ascii_lowercase:
        T = S
        N = len(T)
        cnt = 0
        while T.count(c) != N:
            N -= 1
            U = [""] * (N)
            for i in range(N):
                U[i] = T[i]
                if T[i+1] == c:
                    U[i] = T[i+1]
            T = U
            cnt += 1
        ans = min(ans, cnt)
    print(ans)
    return


if __name__ == "__main__":
    main()

Submission status: Accepted

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

def main():
    S = str(input())
    ans = 10**3
    for i in list(set(S)):
        count = 0
        now = 0
        for j in S:
            if i != j:
                count += 1
            else:
                count = 0
            now = max(now,count)
        ans = min(ans,now)
    
    print(ans)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from string import ascii_lowercase

s = input()

result = len(s)

for c in ascii_lowercase:
    t = s

    count_c = t.count(c)
    if count_c == 0:
        continue

    n_operations = 0

    while len(t) > count_c:
        t_next = [None] * (len(t) - 1)
        count_c_next = 0
        for i in range(len(t_next)):
            if t[i] == c or t[i + 1] == c:
                t_next[i] = c
                count_c_next += 1
            else:
                t_next[i] = '0'
        t = t_next
        count_c = count_c_next
        n_operations += 1

    result = min(result, n_operations)

print(result)

Submission status: Accepted

Solution code:
s = list(input())
set_s = set(list(s[:]))
n = len(s)

ans = float("inf")
if len(set_s) == 1:
    print(0)
    exit()
for target in set_s:
    l = s[:]
    for i in range(n):
        for j in range(n-1-i):
            if l[j] == target or l[j+1] == target:
                l[j] = target
        l = l[:-1]
        if len(set(list(l))) == 1:
            break
    ans = min(ans, i+1)
print(ans)

Submission status: Accepted

Solution code:
S = str(input())

if len(set(S)) == 1:
  print(0)
else:
  ans = pow(10,5)
  for s in set(S):
    l = S
    num = 0 
    while len(set(l)) > 1:
      tmp = ""
      for i in range(len(l)-1):
        if l[i] == s:
          tmp += l[i]
        else:
          tmp += l[i+1]
      l = tmp
      num += 1
    ans = min(num, ans)
  print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
import string

t = stdin.readline().rstrip()
alpha = string.ascii_lowercase
mi = 10**10
if len(set(t)) == 1:
    print(0)
    exit() 
for target in alpha:
    s = t
    cnt = 0
    while True:
        cnt += 1
        sin = ""
        for i in range(len(s)-1):
            sin += s[i] if s[i] == target else s[i+1]
        s = sin
        if s.count(target) == len(s):
            break
    mi = min(mi,cnt)

print(mi)

Submission status: Accepted

Solution code:
s = list(input())

ks = set(s)
ans = 100000

for k in ks:
    count = 0
    t = s[:]
    while True:
        if len(set(t)) <= 1:
            break

        new_t = []
        for i in range(len(t)-1):
            if t[i] == k or t[i+1] == k:
                new_t.append(k)
            elif s[i+1] != k:
                new_t.append(s[i+1])
        t = new_t[:]
        count += 1
    ans = min(ans, count)

print(ans)

Submission status: Accepted

Solution code:
import sys
s = input()
best = 10**18
for _s in list(set(s)):
    cnt = 0
    _max = 0
    for x in s[::-1]:
        if x == _s:
            cnt = 0
        else:
            cnt += 1
        _max = max(_max, cnt)
    best = min(_max, best)
print(best)

Submission status: Accepted

Solution code:
s = input()

ans = float("inf")
for i in range (26):
    a = chr(ord("a")+i)
    if  s.count(a) == 0 :
        pass
    else:
        tmp = s
        c = 0
        while set(tmp) != {a}:
            t = ""
            for j in range(len(tmp)-1):
                if tmp[j] == a or tmp[j+1] == a:
                    t += a
                else:
                    t += tmp[j]
            c += 1
            tmp = t
        ans = min(c, ans)

print(ans)

Submission status: Accepted

Solution code:
s=list(input())
ans=1000
tmp=s
for i in s:
    l=[]
    cnt=0
    for j in s:
        if j!=i:
            cnt+=1
        else:
            l.append(cnt)
            cnt=0
    l.append(cnt)
    if max(l)<=ans:
        ans=max(l)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

s = input()


def convert(st, toS, cou):
    if st.count(st[0]) == len(st):
        return cou
    for i in range(len(st)):
        if i+1 < len(st) and st[i+1] == toS:
            st[i] = toS
    return convert(st[:-1], toS, cou+1)


ans = 200
for j in set(s):
    ans = min(ans, convert(list(s), j, 0))
print(ans)

Submission status: Accepted

Solution code:
s = input()
ans = float("inf")
for i in range(26):
  c = chr(ord("a")+i)
  if c not in s:
    continue
  temp = s
  count = 0
  while set(temp) != {c}:
    t = ""
    for j in range(len(temp)-1):
      if temp[j] == c or temp[j+1] == c:
        t += c
      else:
        t += "#"
    temp = t
    count += 1
  ans = min(ans, count)
print(ans)

Submission status: Accepted

Solution code:
s=input()
ans = len(s)
then = [s]
for i in s:
    cnt = 0
    while len(set(then[0])) >1:
        cnt += 1
        tmp = then.pop()
        empty=""
        for j in range(len(tmp)-1):
            if tmp[j]==i or tmp[j+1]==i:
                empty += i
            else:
                empty += tmp[j]
        then.append(empty)
    ans=min(ans,cnt)
    then=[s]
print(ans)

Submission status: Accepted

Solution code:
s = input()

C = list(set(list(map(str, s))))

ans = len(s)
for c in C:
	tmp = 0
	cnt = 0
	for i in range(len(s)):
		if s[i] == c:
			tmp = max(tmp, cnt)
			cnt = 0
		else:
			cnt += 1

	tmp = max(tmp, cnt)
	ans = min(ans, tmp)

print(ans)

Submission status: Accepted

Solution code:
def main(c,S):
    count=0
    while S.count(c)!=len(S):
        T=""
        count+=1
        for i in range(len(S)-1):
            if c in [S[i],S[i+1]]:
                T+=c
            else:
                T+=S[i]
        S=T
    return(count)


S=input()
lst=[chr(i) for i in range(ord("a"),ord("z")+1) if chr(i) in S]
print(min([main(i,S) for i in lst]))

Submission status: Accepted

Solution code:
s = input()
A = [chr(i) for i in range(97,97+26)]
ans = float('inf')
for a in A:
    count = 0
    a_count = 0
    for i in range(len(s)):
        if s[i] == a:
            a_count = max(count,a_count)
            count = 0
        else:
            count += 1
        if i == len(s)-1:
            a_count = max(count,a_count)
    ans = min(ans,a_count)
print(ans)

Submission status: Accepted

Solution code:
#coding:utf-8
S = input()
st = set(S)
ans = 1e+9
for s in st:
  ls_strg = list(S.split(s))
  mx = 0
  for strg in ls_strg:
    mx = max(mx,len(strg))
  ans = min(mx, ans)
print (ans)

Submission status: Accepted

Solution code:
s = list(input())
ans = 100
tmp = s
for i in s:
    l = []
    count = 0
    for j in s:
        if j != i:
            count += 1
        else:
            l.append(count)
            count = 0
    l.append(count)
    if max(l) <= ans:
        ans = max(l)
print(ans)

Submission status: Accepted

Solution code:
s = input()
n = len(s)
ls = [[0] for i in range(26)]
for i in range(n):
  x = ord(s[i])-97
  ls[x].append(i+1)
for i in range(26):
  ls[i].append(n+1)
ans = n
for i in range(26):
  ans = min(ans,max([ls[i][j]-ls[i][j-1] for j in range(1,len(ls[i]))]))
print(ans-1)

Submission status: Accepted

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

def chk(i):
    A = [len(a) for a in S.split(chr(i+97))]
    return max(A+[0])

print(min([chk(i) for i in range(26)]))

Submission status: Accepted

Solution code:
import collections
import copy

S = list(input())
len_s = len(S)
S_count = collections.Counter(S)

ans = {}

for key in S_count:
    if len(set(S)) == 1:
        ans[key] = 0
    else:
        new_S = copy.copy(S)
        count = 0
        while len(set(new_S)) != 1:
            count += 1
            old_S = copy.copy(new_S)
            new_S = []
            for i in range(len(old_S)-1):
                if old_S[i] == key or old_S[i+1] == key:
                    new_S.append(key)
                else:
                    new_S.append(old_S[i])

        ans[key] = count
print(min(ans.values()))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    s = S()
    d = defaultdict(list)
    for i in range(27):
        d[chr(ord("a")+i)].append(-1)
    for i in range(len(s)):
        d[s[i]].append(i)
    for i in range(27):
        d[chr(ord("a") + i)].append(len(s))
    ans = inf
    for key, value in d.items():
        tmp = 0
        for v in range(len(value) - 1):
            tmp = max(tmp, value[v + 1] - value[v]-1)
        ans = min(tmp, ans)
    print(ans)
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = input()
ans = 105
from string import ascii_lowercase
for c in ascii_lowercase:
    t = s
    anstemp = 0
    while True:
        tmp = ''
        if t.count(c) == len(t): break
        for i in range(len(t)-1):
            if c in t[i:i+2]: tmp += c
            else: tmp += t[i]
        anstemp += 1
        t = tmp
    if anstemp < ans: ans = anstemp
print(ans)

Submission status: Accepted

Solution code:
def is_all(x):
    for i in range(len(x)-1):
        if x[i]!=x[i+1]:
            return False
    return True
s=input()
n=len(s)
dic={}
for i in range(n):
    if not s[i] in dic:
        dic[i]=s[i]
a=n
for i in range(len(dic)):
    t=[s[j] for j in range(n)]
    for j in range(n-1,-1,-1):
        if t[j]==dic[i]:
            ans=n-j-1
            break
        else:
            t[j]=dic[i]
    cnt=0
    while not is_all(t):
        cnt+=1
        for j in range(1,n):
            if t[j]==dic[i]:
                t[j-1]=dic[i]
    a=min(max(cnt,ans),a)
print(a)

Submission status: Accepted

Solution code:
s = input().strip()
n = len(s)
D = {chr(i):[] for i in range(97,123)}
for i in range(97,123):
    for j in range(n):
        if s[j]==chr(i):
            D[chr(i)].append(j)
cntmin = 1000
for i in range(97,123):
    if len(D[chr(i)])==1:
        d = D[chr(i)][0]
        cntmin = min(cntmin,max(d,n-1-d))
    elif len(D[chr(i)])>1:
        A = []
        for j in range(len(D[chr(i)])):
            if j==0:
                A.append(D[chr(i)][0])
            if j<len(D[chr(i)])-1:
                A.append(D[chr(i)][j+1]-D[chr(i)][j]-1)
        A.append(n-1-D[chr(i)][-1])
        cntmin = min(cntmin,max(A))
print(cntmin)

Submission status: Accepted

Solution code:
s = input()

ans = 100
for _ in 'abcdefghijklmnopqrstuvwxyz':
    S = list(s)
    cnt = 0
    for i in range(len(S)):
        if len(set(S)) == 1:
            ans = min(ans, cnt)
            break
        T = []
        for j in range(len(S)-1):
            if S[j] == _ or S[j+1] == _:
                T.append(_)
            else:
                T.append(S[j])
        cnt += 1
        S = T

print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# 目標の文字を決める -> シミュレーション

import numpy as np

S = np.array(list(read().rstrip()))

def F(S,x):
    A = (S == x)
    t = 0
    while True:
        if np.all(A):
            return t
        A = (A[:-1] | A[1:])
        t += 1

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

Submission status: Accepted

Solution code:
def examA():
    Alpha = [chr(i) for i in range(97, 97+26)]
    S = SI(); N = len(S)
    ans = -1
    for i in range(N):
        for s in Alpha:
            cur = [s for s in S]
            for l in range(i):
                for k in range(N-1):
                    if cur[k+1]==s:
                        cur[k]=s
 #           print(cur)
            if len(Counter(cur[:N-i]))==1:
                ans = i
                break
        if ans>=0:
            break
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examA()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10000000)
INF = 1<<32


def solve(s: str):
    from collections import Counter
    c = Counter(s)

    N = len(s)
    ans = INF


    for k in c.keys():
        c = 0
        sc = s
        for j in range(N):
            ls = []
            for i in range(len(sc)-1):
                if sc[i] == k or sc[i+1] == k:
                    ls.append(k)
                else:
                    ls.append(sc[i])
            c += 1
            
            if all([j == k for j in sc]):
                break

            sc = ''.join(ls)
        ans = min(ans, c-1)

    print(ans)

    return



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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10000000)
INF = 1<<32


def solve(s: str):
    from collections import Counter
    c = Counter(s)

    N = len(s)
    ans = INF


    for k in c.keys():
        # print(k)
        c = 0
        sc = s
        for j in range(N):
            ls = []
        # print(sc, '---')
            for i in range(len(sc)-1):
                if sc[i] == k or sc[i+1] == k:
                    ls.append(k)
                else:
                    ls.append(sc[i])
                # print(ls)
            c += 1
            
            if all([j == k for j in sc]):
                break

            sc = ''.join(ls)
        ans = min(ans, c-1)

    print(ans)


    # for k in c.keys():
    #     sc = list(s)
    #     N = len(sc)
        
    #     while(not all([r == k for r in sc])):
    #         ls = []
    #         for i in range(N-1):
    #             if sc[i] == k:
    #                 ls.append(m)
    #             else:
    #                 ls.append(sc[i])
    #         print(ls)
    #         sc = ls.copy()
    #         print(sc)


    return



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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()

ret = float('inf')
for c in set(s) :
  tmp = 0
  t = s[::]
  u = ''
  while t.count(c) != len(t) :
    tmp += 1
    for i in range(len(t) - 1) :
      if t[i] == c or t[i+1] == c :
        u += c
      else :
        u += t[i]
    t = u[::]
    u = ''
  ret = min(ret, tmp)
  
print(ret)

Submission status: Accepted

Solution code:
s = input()
c = "abcdefghijklmnopqrstuvwxyz"
a = len(s)
for i in c:
  sl = s
  count = 0
  while len(sl) != sl.count(i):
    nsl = ""
    for j in range(len(sl)-1):
      if sl[j] == i or sl[j+1] == i:
        nsl += i
      else:
        nsl += sl[j]
    sl = nsl
    count += 1
  a = min(a,count)
print(a)

Submission status: Accepted

Solution code:
S = input()
if len(set(S)) == 1:
  print(0)
  exit()

ans = float('inf')
for s in set(S):  
  ini_idx = S.find(s)
  tmp_ans = ini_idx
  prv_idx = ini_idx
  while 1:
    tmp_idx = S[prv_idx+1:].find(s)
    if tmp_idx == -1:
      break
    else:
      tmp_idx += prv_idx + 1
      tmp_ans = max(tmp_ans, tmp_idx - prv_idx - 1)
      prv_idx = tmp_idx
    
  tmp_ans = max(tmp_ans, len(S) - prv_idx - 1)
  ans = min(ans,tmp_ans)
print(ans)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)
# print(lst)

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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# N, D, K = map(int,input().split())
# LR = [list(map(int,input().split())) for _ in range(D)]
# ST = [list(map(int,input().split())) for _ in range(K)]

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)

Submission status: Accepted

Solution code:
from collections import Counter

s=input()
c=Counter(s)
#test_s=[k for k,v in c.items() if v==max(c.values())]
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

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

for i in range(N):
    l,r=0,i+1 # 半開区間
    # print(s[l:r]) #DB
    t=set(s[l:r])
    # print(t)
    while r<N:
        l,r=l+1,r+1
        # print(s[l:r]) #DB       
        t&=set(s[l:r])
        # print(t)
        if not t:            
            break
    if t:        
        break

print(i)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
#AGC016 A
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
S = input()

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

Submission status: Accepted

Solution code:
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
# MOD = 998244353


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)

Submission status: Accepted

Solution code:
# temp
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)

Submission status: Accepted

Solution code:

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:
            #print (sc)
            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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# AGC016
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]
            ##print(now_word)
            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)
            #print(locate)
            diff_locate = [0] * (len(locate) - 1)
            for l in range(len(locate) - 1):
                diff_locate[l] = locate[l+1] - locate[l]
            ##print(diff_locate)
            cnt_list.append(max(diff_locate) - 1)
            ##print(cnt_list)
            ##print("\n")
    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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
s=input()
set_s=list(set(s))
ans=999999999999999
for i in range(len(set_s)):
    si=set_s[i]
    t=list(s)
#    print(t)
    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
#        print(t)
        si_ans+=1
    if(ans > si_ans):
        ans=si_ans
print(ans)