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:
def resolve():
    '''
    code here
    '''
    import collections
    s = input()

    N = len(s)

    unique_num_dict = collections.Counter(s)
    most_common = unique_num_dict.most_common()[0]

    if len(unique_num_dict) == 1:
        print(0)
    elif most_common[1] == 1:
        print(N//2)
    else:
        s_set = set(s)
        temp_min_num = 101
        for item in s_set:
            temp_max_num = max([len(item) for item in s.split(item)])
            temp_min_num = min(temp_min_num, temp_max_num)
        print(temp_min_num)
            

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[24]:


from collections import Counter


# In[34]:


s = input()


# In[36]:


mylist = [x for x,c in sorted(Counter(s).items(), key=lambda x: x[1], reverse=True)]
ans = len(s)
for w in mylist:
    myword = s
    cnt = 0
    while len(set(myword)) > 1 and cnt <= ans:
        cnt += 1
        tmp = []
        for i in range(len(myword)-1):
            if myword[i] == w or myword[i+1] == w:
                tmp.append(w)
            else:
                tmp.append(myword[i])
        myword = tmp
    ans = min(cnt, ans)
#     print(myword)
print(ans)


# In[ ]:

Submission status: Accepted

Solution code:
s = input()

def f(s, al):
    cnt = 0
    while True:
        if len(s) == 0:
            return cnt

        if len(set(s)) == 1 and s[0] == al:
            return cnt

        t = ""
        for i in range(len(s)-1):
            if al in s[i:i+2]:
                t += al
            else:
                t += s[i]
        
        s = t
        cnt += 1

res = 100
for i in range(26):
    res = min(f(s, chr(ord("a")+i)), res)

print(res)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

s = input()
S = set(s)

ans = INF
for x in S:
	cnt = 0
	t = s
	while 1 < len(set(t)):
		l = ""
		for i in range(1, len(t)):
			if t[i] == x or t[i-1] == x:
				l += x
			else:
				l += t[i]
		t = l
		cnt += 1
	ans = min(ans, cnt)

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
from collections import deque
import itertools
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
ans = float("inf")
s = input()
for i in s:
	t = s.split(i)
	l = -1
	for j in t:
		l = max(l,len(j))
	ans = min(ans,l)
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

def main():
    S = list(input())
    ans = float('inf')
    for i in range(97,123):
        tmp = deque(S)
        cnt = 0
        s = chr(i)
        while len(tmp) > tmp.count(s):
            for j in range(len(tmp)):
                if tmp[j] == s:
                    continue
                if j + 1 < len(tmp) and tmp[j+1] == s:
                    tmp[j] = s
            tmp.pop()        
            cnt += 1
        ans = min(ans, cnt)
    print(ans)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
s = list(input())
result = []
for i in set(s):
    x = [0]
    y = 0
    for j in range(len(s)):
        if s[j] == i:
            x.append(y)
            y = 0
        else:
            y += 1
    x.append(y)
    if s[-1] == i:
        result.append(max(x))
    else:
        result.append(x[-1]+max(0, max(x[:-1])-x[-1]))
print(min(result))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import defaultdict
from itertools import accumulate


def read():
    s = input().strip()
    return s,


def solve(s):
    z = defaultdict(list)
    for i in range(len(s)):
        z[s[i]].append(i)
    ans = 99999
    for k, v in z.items():
        prev = -1
        diffmax = 0
        for i in v:
            diffmax = max(diffmax, i-prev-1)
            prev = i
        diffmax = max(diffmax, len(s)-v[-1]-1)
        ans = min(ans, diffmax)
    return ans


if __name__ == '__main__':
    inputs = read()
    print("%s" % solve(*inputs))

Submission status: Accepted

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


s = input()
def sub(c):
    ss = s[:]
    ans = 0
    while True:
        if all(c==ss[i] for i in range(len(ss))):
            break
        ans += 1
        tmp = []
        for i in range(len(ss)-1):
            if c in (ss[i], ss[i+1]):
                tmp.append(c)
            else:
                tmp.append(ss[i])
        ss = "".join(tmp)
    return ans
ans = float("inf")
for c in set(s):
    ans = min(ans, sub(c))
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

S = input()

cnt = Counter(S).most_common()

#print(cnt)

cand = [chr(i) for i in range(97, 97+26)]

anss = float('inf')
for c in cand:
    alp = c
    ans = 0
    tmp = 0
    for s in S:
        if s == alp:
            ans = max(ans, tmp)
            tmp = 0
        else:
            tmp += 1
    if tmp:
        ans = max(ans, tmp)
    anss = min(anss, ans)
print(anss)

Submission status: Accepted

Solution code:
s=input()


from collections import Counter

c=Counter(s)
if len(list(c.keys()))==1:
    print(0)
    exit(0)


cnt=101
targets=list(c.keys())

for t in targets:
    tmp_cnt=0
    prev=s
    minus=1
    while True:
        tmp=''
        tmp_cnt+=1
        for i in range(len(s)-minus):
            if prev[i]==t or prev[i+1]==t:
                tmp+=t
            else:
                tmp+=prev[i]

        tmp_c=Counter(tmp)
        if len(list(tmp_c.keys()))==1:
            break
        else:
            minus+=1
            prev=tmp

    cnt=min(cnt,tmp_cnt)

print(cnt)

Submission status: Accepted

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

min_cnt = float('inf')

for c in string.ascii_lowercase:
    if c not in s:
        continue
    cnt = 0
    s_ = s[:]
    while s_:
        m = len(s_) - 1
        is_same = True
        for d in s_:
            if d != c:
                is_same = False
                break
        if is_same:
            min_cnt = min(min_cnt, n - m-1)
        s_next = [''] * m
        for i in range(m):
            if s_[i] == c or s_[i + 1] == c:
                s_next[i] = c
            else:
                s_next[i] = s_[i]
        s_ = s_next

print(min_cnt)

Submission status: Accepted

Solution code:
from collections import Counter
import sys

sys.setrecursionlimit(10 ** 6)

mod = 1000000007
inf = int(1e18)

dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]


def inverse(a):
    return pow(a, mod - 2, mod)


def usearch(x, a):
    lft = 0
    rgt = len(a) + 1
    while rgt - lft > 1:
        mid = (rgt + lft) // 2
        if a[mid] <= x:
            lft = mid
        else:
            rgt = mid
    return lft

def solve(a, s):
    b = []
    for i in range(len(s)):
        if s[i] == a:
            b.append(i)
    if len(b) == 0:
        return len(s) - 1
    ans = 0
    for i in range(len(b)-1):
        ans = max(ans, (b[i+1] - b[i] - 1))
    ans = max(ans, b[0])
    ans = max(ans, len(s) - b[-1] - 1)
    return ans

def main():
    s = input()
    ans = len(s)
    for i in range(26):
        ans = min(ans, solve(chr(ord('a') + i), s))
    print(ans)
main()

Submission status: Accepted

Solution code:
S = input().strip()

ans = float("inf")

for i in range(26):
    tmp = 0
    current = 0
    for s in S:
        if s == chr(ord('a') + i):
            tmp = max(tmp, current)
            current = 0
        else:
            current += 1

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

print(ans)

Submission status: Accepted

Solution code:
S = input()
ans = len(S)
for i in range(26):
	c = chr(ord('a') + i)
	t = list(S)
	while not all(a==t[0] for a in t):
		u = []
		for a,b in zip(t,t[1:]):
			if a==c or b==c:
				u.append(c)
			else:
				u.append(a)
		t = u
	tmp = len(S) - len(t)
	ans = min(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
string = input()
start = len(string)
targets = set(string)
ans = float('inf')
for a in targets:
    prev = string
    while not all(c == a for c in prev):
        new = ''
        for idx in range(len(prev) - 1):
            if a in (prev[idx], prev[idx+1]):
                new += a
            else:
                new += prev[idx]
        prev = new
    score = start - len(prev)
    ans = min(ans, score)

print(ans)

Submission status: Accepted

Solution code:
s=input()
ans=100
for i in [chr(97+i)for i in range(26)]:
    a=b=0
    for j in s:
        if i!=j:
            a+=1
        else:
            a=0
        b=max(b,a)
    ans=min(ans,b)
print(ans)

Submission status: Accepted

Solution code:
import bisect,collections,copy,heapq,itertools,math,string
import sys
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())
 
s = input()
ans = math.inf
for x in s:
    t = s.split(x)
    tmp = 0
    for i in range(len(t)):
        tmp = max(tmp, len(t[i]))
    ans = min(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
import bisect,collections,copy,heapq,itertools,math,string
import sys
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

s = input()
ans = math.inf
for x in s:
    t = s.split(x)
    tmp = 0
    for i in range(len(t)):
        tmp = max(tmp, len(t[i]))
    ans = min(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
import string
def func(a):
    a = a[::-1]
    return a[::-1]
s=input()
s = func(s)
ans=10**9+1
for value in set(s):
    enumerater_value=value+s+value
    var2=0
    var=0
    for i,key in enumerate(enumerater_value):
        if key==value:
            if var2<i-var-1: 
                var2=i-var-1
            var=i
    if ans>var2: 
        ans=var2
print(ans)

Submission status: Accepted

Solution code:
S = input()
val = 10**9
 
voc = set(S)
for c in voc:
    cnt = 0
    tmp = list(S)
 
    while True:
        if all([w == c for w in tmp]):
            val = min(cnt, val)
            break
        cnt += 1
        for u in range(len(tmp)-1):
            tmp[u] = c if tmp[u] == c or tmp[u+1] == c else tmp[u]    
        tmp.pop()
 
 
print(val)

Submission status: Accepted

Solution code:
S = input()
ans = 10e9

voc = set(S)
for c in voc:
    cnt = 0
    tmp = list(S)

    while True:
        if all([w == c for w in tmp]):
            ans = min(cnt, ans)
            break
        cnt += 1
        for u in range(len(tmp)-1):
            tmp[u] = c if tmp[u] == c or tmp[u+1] == c else tmp[u]    
        tmp.pop()


print(ans)

Submission status: Accepted

Solution code:
s = input()
l = set(s)
ans = 10 ** 10
for x in l:
    cnt, cnt_max = 0, 0
    for i in s:
        if i != x:
            cnt += 1
        else:
            cnt_max = max(cnt_max, cnt)
            cnt = 0
    cnt_max = max(cnt_max, cnt)
    ans = min(cnt_max, ans)
print(ans)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
s=input().rstrip()
ans=1000
for i in range(26):
    x=chr(97+i)
    temp=0
    pre=list(s)
    while True:
        check=True
        for j in pre:
            if j!=x:
                check=False
        if check:
            ans=min(ans,temp)
            break
        temp+=1

        post=[]
        for j in range(len(pre)-1):
            if pre[j]==x or pre[j+1]==x:
                post.append(x)
            else:
                post.append('!')
        pre=post

print(ans)

Submission status: Accepted

Solution code:
#AGC016-A
from collections import Counter 
s = input()
alphabet = "abcdefghijklmnopqrstuvwxyz"

result = []
if len(set(s)) == 1:
    result.append(0)
for i in alphabet:
    s_tmp = s 
    cnt = 0
    for k in range(len(s_tmp)):
        s_new= ""
        for j in range(len(s_tmp)-1):
            if s_tmp[j] == i or s_tmp[j+1] == i:
                s_new += i
            else:
                s_new += s_tmp[j]
        cnt += 1
        if len(set(s_new)) == 1:
            result.append(cnt)
            break        

        s_tmp = s_new
        
            
 

#print(result)
print(min(result))

Submission status: Accepted

Solution code:
S = input()
l = set(S)

res = 10 ** 9
# 使われている文字列を全探索
for x in l:
    # tmp: xでない文字の連続する長さの数え上げ用
    # chk: xでない文字の連続する長さのmax
    tmp, chk = 0, 0
    # 文字列を見ていく
    for s in S:
        # xでない文字の連続する長さのmaxをとっていく
        if s != x:
            tmp += 1
        else:
            chk = max(chk, tmp)
            tmp = 0
    # 文字列の終わり部分
    chk = max(chk, tmp)
    # 連続する長さのmin
    res = min(chk, res)
print(res)

Submission status: Accepted

Solution code:
s = input()
l = set(s)
ans = 10 ** 10
for x in l:
    cnt, cnt_max = 0, 0
    for i in s:
        if i != x:
            cnt += 1
        else:
            cnt_max = max(cnt_max, cnt)
            cnt = 0
    cnt_max = max(cnt_max, cnt)
    ans = min(cnt_max, ans)
print(ans)

Submission status: Accepted

Solution code:
s = input()
def simulate(c):
    W = list(s[:])
    cnt = 0
    while len(set(W)) > 1:
        for i,w in enumerate(W):
            if w==c and i >= 1:
                W[i-1] = c
        cnt += 1
        W.pop()
    return cnt

ans = len(s)
for c in set(s):
    ans = min(ans, simulate(c))

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

s = input()

d = Counter(s)
ans = 101

if len(d) == 1:
    print(0)
    exit()

for item in d.keys():
    st = item
    tmp = list(s)
    n = len(s)
#    print('----------',item,tmp,'---------------')
    while n>0:
        gen = []
        flag = True
        for i in range(n-1):
            if tmp[i] == st or tmp[i+1] == st:
                gen.append(st)
            else:
                gen.append(tmp[i])
                flag = False
#        print(gen)
        if flag: break
        else: 
            tmp = gen.copy()
            n -= 1
    ans = min(ans,len(s)-len(gen))
print(ans)

Submission status: Accepted

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

alpha = "abcdefghijklmnopqrstuvwxyz"
ans = float("inf")
for tar in alpha:
    t = s
    cnt = 0
    flag = True
    while flag:
        nt = ""
        cnt += 1
        flag = False
        for i in range(len(t)-1):
            if t[i] == tar or t[i+1] == tar:
                nt += tar
            else:
                nt += t[i]
                flag = True
        t = nt
    ans = min(ans, cnt)
print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

s = ins()


def solve():
    ss = set(s)
    ans = 10000
    ach = None
    for c in range(26):
        ch = chr(ord("a") + c)
        if ch not in ss:
            continue
        msk = sk = 0
        for i, x in enumerate(s):
            if x == ch:
                if msk < sk:
                    msk = sk
                sk = 0
            else:
                sk += 1
        if msk < sk:
            msk = sk
        if ans > msk:
            ans = msk
            ach = ch

    return ans


print(solve())

Submission status: Accepted

Solution code:
S = input()
k = 1000 
for i in range (len(S)):
    x = y = 0
    for j in range (1, len(S) + 1):
        if S[i] == S[len(S)-j]:
            x = 0
        else:
            x += 1
        y = max(x,y)
    k = min(k,y)
print(k)

Submission status: Accepted

Solution code:
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(500000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

s=input()

cnter=Counter(s)

ans=float('inf')
for a in al:
    if cnter[a]==0:
        continue
    tmp=s[:]
    cnt=0
    while 1:
        flag=1
        for i in range(len(tmp)):
            if a!=tmp[i]:
                flag=0
        if flag==1:
            break
        tmp1=""
        for i in range(len(tmp)-1):
            if tmp[i]==a or tmp[i+1]==a:
                tmp1+=a
            else:
                tmp1+=tmp[i]
        cnt+=1
        tmp=tmp1[:]
    ans=min(ans,cnt)
print(ans)

Submission status: Accepted

Solution code:
def main():
    S = input()
    ST = set(S)
    ans = 100
    for st in ST:
        count = 0
        T = list(S)
        tmp = ''
        while len(set(T)) > 1:
            for i in range(len(T) - 1):
                if st in [T[i], T[i + 1]]:
                    tmp += st
                else:
                    tmp += T[i]
            T = tmp
            tmp = ''
            count += 1
        ans = min(ans, count)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
m = 10**9 + 1
# i,i+1のどちらかから選んで文字列にする
for alpha in set(s):
    temp = alpha + s + alpha
    M = 0
    now = 0
    for i, key in enumerate(temp):
        if key == alpha:
            if M < i - now - 1:
                M = i - now - 1
            now = i
    if m > M:
        m = M

print(m)

Submission status: Accepted

Solution code:
s = str(input())
def strop(s,c):
    
    count = 0
    while len(set(s)) != 1:
        t = ''
        for i in range(len(s)-1):
            if s[i] == c or s[i+1] == c:
                t += c
            else:
                t += s[i]
        s = t
        #print(s)
        count += 1
    return count
            
mn = 10**9
for i in range(len(set(s))):
    mn = min(mn,strop(s,list(set(s))[i]))
print(mn)

Submission status: Accepted

Solution code:
s=input()
m=10**9+1
for alpha in set(s):
    temp=alpha+s+alpha
    M=0
    now=0
    for i,key in enumerate(temp):
        if key==alpha:
            if M<i-now-1: M=i-now-1
            now=i
    if m>M: m=M

print(m)

Submission status: Accepted

Solution code:
import sys
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

def f(S, a):
    S = S == a
    if not np.any(S):
        return len(S)
    for n in range(len(S)):
        if np.all(S):
            return n
        S = S[1:] | S[:-1]

x = min(f(S, a) for a in S)
print(x)

Submission status: Accepted

Solution code:
import string
d = {}
alph = string.ascii_lowercase
for i in range(len(alph)):
    d[alph[i]] = i
s = list(input())


count = [0 for _ in range(len(alph))]
for letter in s:
    count[d[letter]] += 1
# print(count)

target = []
for i in range(len(alph)):
    if count[i] == max(count):
        target.append(alph[i])

# print(target)
answer = []
for t in s:
    time = 0
    size = len(s)
    line = list(s)
    ifOK = True
    while ifOK:
        # print("".join(line))
        ifOK = False
        new_line = ""
        for i in range(size-1):
            if (line[i] == t or t == line[i+1]) and not line[i] == line[i+1]:
                new_line += t
                ifOK = True
            else:
                new_line += line[i]
        line = list(new_line)
        size -= 1
        time += 1
    answer.append(time)
print(min(answer)-1)

Submission status: Accepted

Solution code:
from collections import Counter

A = list(input())
S = A
MIN = 2000
for t in set(A):
    count = 0
    S = A
    while count <= MIN:
        if len(set(S)) == 1:
            MIN = min(count,MIN)
            break
        T = [None] * (len(S)-1)
        for i in range(len(T)):
            if S[i] == t or S[i+1] == t:
                T[i] = t
            else:
                T[i] = S[i]

        S = T
        count += 1


print(MIN)

Submission status: Accepted

Solution code:
s = list(input())

s_set = set(s)

cnt = 101
for l in s_set:
    indexs = set([i for i, x in enumerate(s) if x == l])
    i = len(s)
    while len(indexs) < i:
        indexsList = list(indexs)
        for j in range(len(indexs)):
            n = indexsList[j]
            if n == 0:
                pass
            elif n == i - 1:
                indexs.remove(n)
                indexs.add(n-1)
            else:
                indexs.add(n-1)
        i -= 1
    cnt = min(cnt, len(s)-i)

print(cnt)

Submission status: Accepted

Solution code:
S = input()

tmp = 0
length = 0
ans = 100
for s in S:
    for t in S:
        if t != s:
            tmp += 1
        else:
            length = max(tmp, length)
            tmp = 0
    else:
        length = max(tmp, length)
        tmp = 0
    ans = min(ans, length)
    length = 0

print(ans)

Submission status: Accepted

Solution code:
INF = int(1e9+7)
s = input()
d = list('abcdefghijklmnopqrstuvwxyz')
ans = INF
for i in d:
    lis = [-1]
    for j in range(len(s)):
        if s[j] == i:
            lis.append(j)
    lis.append(len(s))
    val = 0
    for j in range(1,len(lis)):
        val = max(val,lis[j]-lis[j-1]-1)
    ans = min(ans,val)
print(ans)

Submission status: Accepted

Solution code:
s = input()
l = set(s)
ans = 10**10
for x in l:
    tmp, chk = 0, 0
    for i in s:
        if i != x:
            tmp += 1
        else:
            chk = max(chk, tmp)
            tmp = 0
    chk = max(chk, tmp)
    ans = min(chk, ans)
print(ans)

Submission status: Accepted

Solution code:
S = input()
# シミュレーションでいい気がする
# aで埋めるのに最小手数, bで埋めるのに最小手数, ...
ans = 10**9  # これを最小にしたい
alphabet = []
for i in range(26):
    alphabet.append(chr(ord("a")+i))
# print(alphabet)   # 全てのアルファベットが揃った
hoge = set(S)
for character in alphabet:  # characterで埋めることを考える
    if character not in hoge:
        continue
    tmp = 0
    copy_s = list(S).copy()
    # print(copy_s)
    # このcopy_sの文字を減らしていくことを考える
    while set(copy_s) != set(character):
        copy_s_dash = []
        for i in range(len(copy_s)-1):
            if copy_s[i] == character or copy_s[i+1] == character:
                copy_s_dash.append(character)
            else:
                copy_s_dash.append("?")
        # print(copy_s_dash)
        copy_s = copy_s_dash.copy()
    ans = min(ans, len(S)-len(copy_s))
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

def main():
    S = list(input())
    sr = set(S)
    ret = 100
    for s in sr:
        max_cnt = 0
        cnt = 0
        for t in S:
            if s!=t:
                cnt += 1
            else:
                max_cnt = max(max_cnt, cnt)
                cnt = 0
        max_cnt = max(max_cnt, cnt)
#        print(s,max_cnt)
        ret = min(ret, max_cnt)
    print(ret)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

s = list(input())
alphabets = [chr(ord('a')+i) for i in range(26)]

ans = float('inf')
for a in alphabets:
    t = s[:]
    count = 0
    while True:
        b = []
        if t == [a]*len(t):
            ans = min(ans,count)
            break
        else:
            for i in range(len(t)-1):
                if t[i] == a or t[i+1] == a:
                    b.append(a)
                else:
                    b.append('A')
            count += 1
            t = b[:]

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

S = input()
S1 = S

st = set(S)
rlt = 1000

if len(Counter(st)) == 1:
  print(0)
  exit()
  
for s in st:
  S = S1
  t = 0
  while len(Counter(S)) > 1:
    tmp = ''
    for i in range(1,len(S)):
      if S[i-1] == s or S[i] == s:
        tmp += s
      else:
        tmp += S[i]
    t += 1
    S = tmp

  if t < rlt:
    rlt = t
    
print(rlt)

Submission status: Accepted

Solution code:
#import sys
#import numpy as np
import math
#import itertools
#from fractions import Fraction
#import itertools
from collections import deque
from collections import Counter
#import heapq
#from fractions  import gcd
#input=sys.stdin.readline
#import bisect
s=list(input())
n=len(s)
s_set=set(s)
c=Counter(s)
dic=[]
for i in s_set:
    bef=-1
    delta=0
    if c[i]==1:
        continue
    else:
        for j in range(n):
            if  i==s[j]:
                res=j-bef-1
                bef=j
                delta = max(delta,res)
        res=n-1-bef
        dic.append(max(delta,res))
if not dic:
  print(n//2)
else:
  print(min(n//2,min(dic)))

Submission status: Accepted

Solution code:
s=str(input())
l=len(s)
ans=99999999999
for j in s:
  temp=j
  a=0
  cnt=0
  for i in range(l):
    if s[i]!=temp:
      cnt=cnt+1
    else:
      a=max(a,cnt)
      cnt=0
  a=max(a,cnt)
  ans=min(a,ans)
print(ans)

Submission status: Accepted

Solution code:
def find_idx(s, c):
  idx_lst = []
  while c in s:
    idx = s.index(c)
    idx_lst += [idx]
    s = s[idx+1:]
  else:
    idx_lst += [len(s)]
  return idx_lst

s = input()
res = float('inf')
for i in range(97, 97 + 26):
  tmp = find_idx(s, chr(i))
  if tmp:
    res = min(res, max(tmp))
print(res)

Submission status: Accepted

Solution code:
import sys
S = list(sys.stdin.readline().rstrip())
chars = [chr(ord("a") + i) for i in range(26)]
ans = 10**5
for c in chars:
    if c not in S:
        continue
    cnt = 0
    S_copy = S.copy()
    while len(set(S_copy)) > 1:
        S_tmp = []
        for i in range(len(S_copy) - 1):
            if S_copy[i] == c or S_copy[i + 1] == c:
                S_tmp.append(c)
            else:
                S_tmp.append(S[i])
        S_copy = S_tmp.copy()
        cnt += 1
    ans = min(ans, cnt)
print(ans)

Submission status: Accepted

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

def f(s, c):
    n = len(s)
    if not c in s:
        return n-1
    else:
        longest = 0
        last = -1
        i = 0
        while i < n:
            l = 0
            while i < n and s[i]!=c:
                l+=1 
                i+=1
            if i < n and s[i]==c:
                last = i
                longest = max(longest, l)
            i+=1
        return max(n-last-1, longest)

ans = float('inf')
for i in range(26):
    c = chr(ord('a')+i)
    ans = min(ans, f(s, c))
print(ans)

Submission status: Accepted

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

ans = float('inf')
s_len = len(s)
for target in s_array:
    s_check = s
    for i in range(1,s_len):
        count = 0
        s_dosh = [""] * (s_len-i)
        for j in range(s_len-i):
            if s_check[j] == target or s_check[j+1] == target:
                s_dosh[j] = target
            else:
                s_dosh[j] = s[j]
                count = 1
        s_check = ''.join(s_dosh)
        # print(s_check)
        if count == 0:
            ans = min(ans,i)
            break

print(ans)

Submission status: Accepted

Solution code:
l = list(input())
ans = 10000000
for let in range(26):
    cur = chr(let+97)
    temp = l[:]
    cnt = 0
    while len(set(temp)) > 1:
        for i in range(len(temp)-1):
            if temp[i] == cur or temp[i+1] == cur:
                temp[i] = cur
            else:
                temp[i] = l[i]
        temp.pop()
        cnt += 1
    ans = min(ans, cnt)
    # print(temp)
print(ans)

Submission status: Accepted

Solution code:
s=list(input())
S=list(set(s))
ans=100
for c in S:
  kari=0
  tmp=0
  for w in s:
    if w==c:
      kari=max(tmp, kari)
      tmp=0
    else:
      tmp+=1
  kari=max(tmp,kari)
  ans=min(ans, kari)
print(ans)

Submission status: Accepted

Solution code:
S=input()
A="abcdefghijklmnopqrstuvwxyz"
minimum=len(S)//2
for a in A:
    ans_max=0
    p=0
    for s in S:
        if a==s:
            ans_max=max(p,ans_max)
            p=0
        else:
            p+=1
    ans_max=max(p,ans_max)
    minimum=min(ans_max,minimum)
print(minimum)