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: Wrong Answer

Solution code:
s = input()
n = len(s)
min_t = float('inf')
for i in range(n):
    target = s[i]
    pre = 0
    max_t = 0
    for j in range(n):
        if s[j] == s[i]:
            max_t = max(max_t, j-pre-1)
            pre = j
    max_t = max(max_t, n - pre-1)
    if max_t < min_t:
        min_t = min(min_t, max_t)
print(min_t)

Submission status: Wrong Answer

Solution code:
s=input()
s.lower()

a=[0 for i in range(26)]
for i in s:
	a[ord(i)-ord('a')]+=1
test=max(a)

print(len(s)-test)

Submission status: Time Limit Exceeded

Solution code:
s=input()
ans=1000
for i in range(26):
  if chr(97+i) not in s:continue
  S=s
  cnt=0
  while(len(set(S)))!=1:
    cnt+=1
    t=""
    for j in range(len(S)-1):
      if S[j] == chr(97+i) or S[j+1] == chr(97+i):
        t+=chr(97+i)
      else:
        t+=S[j]
  ans=min(cnt,ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()

def recur(s,c):
  if len(set(s)) == 1:
    return c
  else:
    ret = []
    roll = 2 ** (len(s) - 2)
    for i in range(roll):
      sen = ""
      for j in range(len(s) - 1):
        if ((i >> j) & 1):
          sen += s[j + 1]
        else:
          sen += s[j]
      ret.append(recur(sen,c + 1))
    return min(ret)
      
print(recur(S,0))

Submission status: Accepted

Solution code:
def solve(STR):
    CNT, tmp = 0, list(s)
    while len(set(tmp)) > 1:
        CNT += 1
        for i in range(len(tmp) - 1):
            if STR in tmp[i:i+2]:
                tmp[i] = STR
        tmp.pop()
    return CNT


s = input()

ans = 100
for lttr in set(s):
    ans = min(ans, solve(lttr))
print(ans)

Submission status: Accepted

Solution code:
def solve(STR):
    CNT, tmp = 0, s
    while len(set(tmp)) > 1:
        CNT += 1
        bfo, tmp = tmp, []
        for i in range(len(bfo) - 1):
            tmp.append(bfo[i])
            if STR in {bfo[i], bfo[i + 1]}:
                tmp[-1] = STR
    return CNT


s = input()

ans = 100
for lttr in set(s):
    cnt = solve(lttr)
    ans = min(ans, cnt)
print(ans)

Submission status: Accepted

Solution code:
S = input()
rests = [chr(i) for i in range(97, 97+26)]

def is_only(s, c):
  for sc in s:
    if sc != c:
      return False
  return True

def shrink(s, c):
  res = ''
  for i in range(len(s)-1):
    if s[i+1] == c:
      res += c
    else:
      res += s[i]
  return res

ans = 10**3
for c in rests:
  if not c in S:
    continue
  s = S
  cnt = 0
  while not is_only(s, c):
    s = shrink(s, c)
    cnt += 1
  ans = min(ans, cnt)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
S = input()
nums = list(set(S))
ans=pow(10,9)
for num in nums:
    s=S
    ans2=0
    while len(Counter(s)) > 1:
        list_s=list(s)
        new_s=[]
        for n in range(len(list_s)-1):
            if list_s[n] == num or list_s[n+1] == num:
                new_s.append(num)
            else:
                new_s.append(list_s[n])
        s = new_s
        ans2 += 1
    ans =  min(ans, ans2)
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import collections as cl
import sys


def II():
    return int(sys.stdin.readline())


def MI():
    return map(int, sys.stdin.readline().split())


def LI():
    return list(map(int, sys.stdin.readline().split()))


def main():
    S = input()

    indexes = {}

    for i in range(len(S)):
        if S[i] not in indexes.keys():
            indexes[S[i]] = [i]
        else:
            indexes[S[i]].append(i)

    ans = 100

    for k, v in indexes.items():
        tmp_max = v[0]
        tmp_max = max(v[0], len(S) - v[-1] - 1)
        v.append(len(S))
        for i in range(len(v) - 1):
            tmp_max = max(tmp_max, v[i+1] - v[i] - 1)
        ans = min(ans, tmp_max)
    print(ans)


main()

Submission status: Accepted

Solution code:
s = input(); n = len(s); a = [-1]*26; b = [0]*26
for i in range(n): j = ord(s[i])-97; b[j] = max(b[j], i-a[j]-1); a[j] = i
for i in range(26): b[i] = max(b[i], n-a[i]-1)
print(min(b))

Submission status: Accepted

Solution code:
from math import ceil

s=input()
x='abcdefghijklmnopqrstuvwxyz'
ans=1000


def greedy(i,s,cnt):
    if s.count(s[0])==len(s):
        return cnt
    t=''
    for j in range(len(s)-1):
        if s[j]==i or s[j+1]==i:
            t+=i
        
        else:
            t+=s[j]
    
    return greedy(i,t,cnt+1)


for i in x:
    ans=min(ans,greedy(i,s,0))


print(ans)

Submission status: Accepted

Solution code:


def main():
    s = input()
    n = len(s)
    ans = 0
    for j in range(n):
        t = s
        cnt = 0
        for c in t:
            if c == s[j]:
                cnt += 1
        while len(t) != cnt:
            t2 = ''
            for i in range(len(t) - 1):
                t2 += (s[j] if s[j] == t[i] or s[j] == t[i + 1] else t[i])
            t = t2
            cnt = 0
            for c in t:
                if c == s[j]:
                    cnt += 1
        ans = max(ans, len(t))

    print(n - ans)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()

v = 'abcdefghijklmnopqrstuvwxyz'
ans = 1000
for i in range(26):
  t = s
  a = t.split(v[i])
  if len(a) == 1:
    continue
  b = 0
  for j in a:
    b = max(len(j),b)
  ans = min(b,ans)
print (ans)

Submission status: Accepted

Solution code:
s = input()

a = [[] for i in range(26)]

l = len(s)

for i in range(l):
    a[ord(s[i]) - 97].append(i)
ans = 1000

for i in range(26):
    a[i].append(l)
    t = a[i][0]
    for j in range(1, len(a[i])):
        t = max(t, a[i][j] - a[i][j-1] - 1)
    ans = min(ans, t)

print(ans)

Submission status: Accepted

Solution code:
import copy
char = list(map(chr, range(ord('a'), ord('z')+1)))
target = list(input())
ans = float("inf")
for c in char:
  s = copy.deepcopy(target)
  tmpans = 0
  while True:
    flag = True
    for si in s:
      if si != s[0]:
        flag = False
    if flag==True:
      break
    for i in range(len(s)-1):
      if s[i] == c or s[i+1] == c:
        s[i] = c
    s.pop()
    tmpans += 1
  if ans > tmpans:
    ans = tmpans
print(ans)

Submission status: Accepted

Solution code:
import sys

def set_list_flag(es):
    return len(list(set(es))) == 1

input = sys.stdin.readline

S = input()
S = S.replace('\n','')
s_list = list(S)

s_result = s_list
next_s_result = []
min_count = 10**6

if set_list_flag(s_list):
    print(0)
    exit(0)
for s in s_list:
    count = 0
    while not set_list_flag(next_s_result):
        count += 1
        next_s_result = []
        for i in range(len(s_result)):
            cur_s = s_result[i]
            nex_s = s_result[i+1]
            get_s = nex_s
            
            if cur_s == s:
                get_s = s

            next_s_result.append(get_s)
            if i+1 == len(s_result)-1:
                break
        s_result = next_s_result

    min_count = min(min_count,count)
    s_result = s_list
    next_s_result = []


print(min_count)

Submission status: Accepted

Solution code:
s=input();a=101
for i in range(97,123):
  b,t,c=0,s,chr(i)
  while len(set(t))>1:
    r=""
    for i in range(len(t)-1):
      r+=[t[i],c][c in t[i:i+2]]
    t=r
    b+=1
  a=min(a,b)
print(a)

Submission status: Accepted

Solution code:
import sys, math
from functools import lru_cache
from collections import deque
sys.setrecursionlimit(10**9)
MOD = 10**9+7

def input():
    return sys.stdin.readline()[:-1]

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]

def f(t, c):
    res = ''
    for i in range(len(t)-1):
        if t[i] == c or t[i+1] == c:
            res += c
        else:
            res += t[i]
    return res

S = input()
ans = 1000
for c in 'qwertyuiopasdfghjklzxcvbnm':
    T = S[:]
    cnt = 0
    while not all(t == c for t in T):
        T = f(T, c)
        cnt += 1
    ans = min(ans, cnt)

print(ans)

Submission status: Accepted

Solution code:
s=input()
a=len(s)
for i in set(s):
    ss=s
    j=0
    while len(set(ss))>1:
        t=""
        for k in range(len(ss)-1):
            if i in (ss[k],ss[k+1]):
                t+=i
            else:
                t+=ss[k]
        ss=t
        j+=1
    a=min(a,j)
print(a)

Submission status: Accepted

Solution code:
s=input()
import sys
mod=10**9+7 ; inf=float("inf")
from math import sqrt, ceil
from collections import deque, Counter, defaultdict #すべてのkeyが用意されてる defaultdict(int)で初期化
input=lambda: sys.stdin.readline().strip()
sys.setrecursionlimit(11451419)
from decimal import ROUND_HALF_UP,Decimal as D  #変換後の末尾桁を0や0.01で指定
  #Decimal((str(0.5)).quantize(Decimal('0'), rounding=ROUND_HALF_UP))
from functools import lru_cache
from bisect import bisect_left as bileft, bisect_right as biright
from fractions import Fraction as F  #F(a,b)で正確なa/b
#メモ化再帰defの冒頭に毎回 @lru_cache(maxsize=10**10)
#引数にlistはだめ
#######ここまでテンプレ#######
#ソート、"a"+"b"、再帰ならPython3の方がいい
#######ここから天ぷら########

# C=Counter(s)
n=len(s)
ans=n+1
for c in range(ord("a"),ord("z")+1):
    c=chr(c)
    now=[]
    for v,cc in enumerate(s):
        if cc==c:
            now.append(v)
    if len(now)==0: continue
    #print(n,c,now)
    d=[now[0]]
    d+=[ (now[i+1]-now[i]-1) for i in range(len(now)-1) ]+[n-1-now[-1]]
    ans=min( max(d),  ans )
print(ans)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time, copy

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

d = []
s = ns()
que = collections.deque()
for c in s:
    if c not in d:
        d.append(c)

ans = inf
if len(d) == 1:
    print(0)
else:
    for c in d:
        t = copy.copy(s)
        for j in range(len(s) - 1):
            nt = []
            for i in range(len(t)-1):
                if c not in t[i:i+2]:
                    nt.append(t[i])
                else:
                    nt.append(c)
            if nt.count(c) == len(nt):
                ans = min(j+1, ans)
                break
            t = copy.copy(nt)
            
    print(ans)

Submission status: Accepted

Solution code:
S = input()
N = len(S)
L = S
ans = 10000000
for c in set(S):
    S = L
    next = []
    while S.count(c) < len(S):
        for i in range(len(S)-1):
            if S[i+1] == c:
                next.append(S[i+1])
            else:
                next.append(S[i])
        S = next
        next = []
    ans = min(ans,N-len(S))
print(ans)

Submission status: Accepted

Solution code:
import sys
# sys.setrecursionlimit(100000)
from collections import Counter


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    S = input()
    S_cnt = Counter(S).most_common()

    def simulation(org, target):
        cnt = 0
        while org.count(target) != len(org):
            t = ""
            for i in range(len(org) - 1):
                if org[i] == target or org[i + 1] == target:
                    t += target
                else:
                    t += org[i]
            cnt += 1
            org = t
        return cnt
    ans = float("inf")
    for k, v in S_cnt:
        ans = min(simulation(S, k), ans)
    print(ans)

    return


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
s = input()
n = len(s)
ans = 1000000000
keep = 0

for i in range(n):
    check = 0
    keep = 0
    for j in range(n):
        if s[i] != s[j]:
            check += 1
        else:
            check = 0
        keep = max(check, keep)
    ans = min(ans, keep)
print(ans)

Submission status: Accepted

Solution code:
import collections
s = input()
ls = len(s)
c = set(s)
mi = ls
valuePlaces = []
for v in c:
    co = 0
    valuePlaces.clear()
    for i in range(ls):
        if s[i] == v:
            valuePlaces.append(i)
    ushiro = ls-valuePlaces[-1]-1
    maxLen = 0
    len_vp = len(valuePlaces)
    for i in range(len_vp):
        start = valuePlaces[i]
        if i == 0:
            goal = -1
        else:
            goal = valuePlaces[i-1]
        maxLen = max(start-goal-1,maxLen)
    mi = min (mi,max(maxLen,ushiro))
print(mi)

Submission status: Accepted

Solution code:
import collections
s = input()
ls = len(s)
c = collections.Counter(s)
mi = ls
valuePlaces = []
for v,k in c.items():
    co = 0
    valuePlaces.clear()
    for i in range(ls):
        if s[i] == v:
            valuePlaces.append(i)
    ushiro = ls-valuePlaces[-1]-1
    #print(ushiro,v)
    maxLen = 0
    len_vp = len(valuePlaces)
    #print(len_vp)
    for i in range(len_vp):
        start = valuePlaces[i]
        if i == 0:
            goal = -1
        else:
            goal = valuePlaces[i-1]
        maxLen = max(start-goal-1,maxLen)
    mi = min (mi,max(maxLen,ushiro))
print(mi)

Submission status: Accepted

Solution code:
S = list(input())
Alphabet_list = set(S)
mincnt = 10**5

for letter in Alphabet_list:
    #convert T->Uniform
    cnt = 0
    T = S.copy()
    while(len(set(T)) > 1):
        cnt += 1
        for i in range(len(T)-1):
            if(T[i] == letter or T[i+1] == letter):
                T[i] = letter
            else:
                pass
        del T[-1]
    mincnt = min(mincnt,cnt)
print(mincnt)

Submission status: Accepted

Solution code:
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right #func(リスト,値)
from heapq import heapify, heappop, heappush
from math import *

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)

s = input()
N = len(s)
ans = INF
for i in range(ord('a'),ord('z')+1):
    c = chr(i)
    k = 0
    tmp = 0
    for j in range(N):
        if s[N-1-j]==c:
            tmp = max(k,tmp)
            k = 0
        else:
            k += 1
    tmp = max(k,tmp)
    ans = min(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:
s = list(input())
ans = 100
n = len(s)
q = list(set(s))
for _ in q:
    x = _
    m = n - 1
    t = list(s)
    r = ['0'] * m
    while len(set(t)) > 1:
        for i in range(m):
            if t[i] == x or t[i+1] == x:
                r[i] = x
            else:
                r[i] = t[i]
        m -= 1
        t = list(r)
        r = ['0'] * m
    ans = min(ans,n-len(t))
print(ans)

Submission status: Accepted

Solution code:
S = input()
ans = len(S)
for i in range(26):
    s = chr(97+i)
    tmp = -1
    res = 0
    flg = False
    for j in range(len(S)):
        if S[j] == s:
            flg = True
            res = max(res, j-tmp-1)
            tmp = j
    if not flg:
        continue
    ans = min(ans, max(res, len(S)-tmp-1))
print(ans)

Submission status: Accepted

Solution code:
import copy
s = input()

alpha_list = "abcdefghijklmnopqrstuvwxyz"

ans = 10**4
for alpha in alpha_list:
#    print(alpha)
    s_check = copy.deepcopy(s)

    mojisu_count = 0
    for i in range(len(s_check)):
        if(len(s_check) == s_check.count(alpha)):
            ans = min(ans, mojisu_count)
            break

        s_dash = ""
        for j in range(len(s_check) - 1):
            if(s_check[j] == alpha or s_check[j+1] == alpha):
                s_dash += alpha
            else:
                s_dash += s[j]

        mojisu_count += 1
        s_check = copy.deepcopy(s_dash)

print(ans)

Submission status: Accepted

Solution code:
s = input()
num = len(s)
ans = num/2
if not ans.is_integer():
    ans = int(ans)+1

for i in range(num):
    a, b, count = s[i], [], 0
    for j in range(num):
        if s[j] != a:
            count += 1
        else:
            b.append(count)
            count = 0
        b.append(count)
    ans = min(ans, max(b))
print(int(ans))

Submission status: Accepted

Solution code:
s = input()
a = "qwertyuiopasdfghjklzxcvbnm"
mi = 100
for i in a:
    ma,cnt = 0,0
    for j in s:
        if j != i:
            cnt += 1
        else:
            ma = max(ma, cnt)
            cnt = 0
        ma = max(ma,cnt)
    mi = min(mi,ma)
print(mi)

Submission status: Accepted

Solution code:
s = list(input())

ans = len(s)
for t in s:
    m = 0
    cnt = 0
    # 同じ文字の区間の最大値がその文字の最小値
    for S in s:
        if S == t: 
            m = max(m,cnt)
            cnt = 0
        else:
            cnt += 1
    m = max(m,cnt)
    ans = min(ans,m)
print(ans)

Submission status: Accepted

Solution code:
#41 A - Shrinking
import collections 
s = list(input())
cnt = collections.Counter(s)
valcnt = list(cnt.values())
M = max(valcnt)
# 出現回数が最多の文字リスト
mkeys = [i for i,v in cnt.items() if v == M]

ans = len(s)
for key in s:
    m = 0
    cnt = 0
    # 最多の文字の区間の最大値がその文字の最小値
    for S in s:
        if S == key: 
            m = max(m,cnt)
            cnt = 0
        else:
            cnt += 1
    m = max(m,cnt)
    ans = min(ans,m)

print(ans)

Submission status: Accepted

Solution code:
#41 A - Shrinking
import collections 
s = list(input())
cnt = collections.Counter(s)
valcnt = list(cnt.values())
M = max(valcnt)
# 出現回数が最多の文字リスト
mkeys = [i for i,v in cnt.items() if v == M]

ans = len(s)
for key in s:
    m = 0
    cnt = 0
    # 最多の文字の区間の最大値がその文字の最小値
    for S in s:
        if S == key: 
            m = max(m,cnt)
            cnt = 0
        else:
            cnt += 1
    m = max(m,cnt)
    ans = min(ans,m)
ans = min(ans,len(s)//2)
print(ans)

Submission status: Accepted

Solution code:
s = input()
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"]

n = len(s)
ans = n

for v in alphabet:
    key = [s[i] for i in range(n)]
    
    count = 0
    end = False
    while True:
        for i in range(n-count):
            if key[i] != v:
                break
            if i == n-count-1:
                ans = min(ans, count)
                end = True
        if count == n-1:
            break
        
        if end:
            break
        
        count += 1
        for i in range(n-count):
            if key[i] == v or key[i+1] == v:
                key[i] = v

print(ans)

Submission status: Accepted

Solution code:
s = list(str(input()))

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

import copy
ans = 101
for i in range(26):
    c = chr(i+ord('a'))
    t = copy.copy(s)
    cnt = 0
    while True:
        temp = []
        cnt += 1
        for j in range(len(t)-1):
            if t[j] == c:
                temp.append(t[j])
            elif t[j+1] == c:
                temp.append(t[j+1])
            else:
                temp.append(t[j])
        t = copy.copy(temp)
        if len(set(t)) == 1:
            break
    ans = min(ans, cnt)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
s=input()
ch=set([c for c in s])
ans=101

for c in ch:
    cur=s
    cnt=0
    while len(set([x for x in cur]))>1:
        cnt+=1
        tmp=''
        for i in range(len(cur)-1):
            if cur[i]==c or cur[i+1]==c:
                tmp+=c
            else:
                tmp+=cur[i]
        cur=tmp
    ans=min(ans,cnt)

print(ans)

Submission status: Accepted

Solution code:
s=input()
print(min(max(map(len,s.split(t)))for t in s))

Submission status: Accepted

Solution code:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    s = S()
    s = list(s)
    blen = len(s)
    same = len(s) == s.count(s[0])
    ans = float("inf")
    dic = collections.Counter(s)
    chars = dic.keys()

    if same:
        ans = 0
    elif not same:
        for char in chars:
            same = False
            bs = copy.copy(s)
            while not same:
                same = True
                for i in range(1,len(bs)):
                    if bs[i-1] == char and bs[i] == char:
                        pass
                    elif bs[i-1] == char or bs[i] == char:
                        bs[i-1] = char
                    else:
                        same = False
                bs.pop()
            cnt = blen - len(bs)
            ans = min(ans,cnt)
    print(ans)
main()

Submission status: Accepted

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

L = "abcdefghijklmnopqrstuvwxyz"

ans = 10**9
for s in L:
    X = S
    res = ""
    cnt = 0
    while(1):
        res = ""
        if X.count(s)==len(X):
            break
        for i in range(len(X)-1):
            if X[i]==s or X[i+1]==s:
                res += s
            else:
                res += X[i]
        X = res
        cnt += 1
    ans = min(ans,cnt)

print(ans)

Submission status: Accepted

Solution code:
S = input()

ans = len(S)

abc = "abcdefghijklmnopqrstuvwxyz"

def do(x,c):
    if x.count(c)==len(x):
        return 0
    res = ""
    for i in range(len(x) - 1):
        if (x[i]==c or x[i+1]==c):
            res += c
        else:
            res += x[i]
    return 1+do(res,c)

for c in abc:
    ans = min(ans,do(S,c))

print(ans)

Submission status: Accepted

Solution code:
S = input()

ans = len(S)

abc = "abcdefghijklmnopqrstuvwxyz"

def do(x,c):
    if len(x)==1 or x.count(c)==len(x):
        return 0
    res = ""
    for i in range(len(x) - 1):
        if (x[i]==c or x[i+1]==c):
            res += c
        else:
            res += x[i]
    return 1+do(res,c)

for c in abc:
    ans = min(ans,do(S,c))

print(ans)

Submission status: Accepted

Solution code:
s = list(input())

abc = "abcdefghijklmnopqrstuvwxyz"

ans = len(s)

for a in abc:
    if a in s:
        t = s.index(a)
        anst = t
        for i in range(t+1,len(s)):
            if s[i]==a:
                anst=max(anst,i-t-1)
                t = i

        anst = max(anst,len(s)-t-1)
        ans = min(ans,anst)

print(ans)

Submission status: Accepted

Solution code:
S=input()
A="abcdefghijklmnopqrstuvwxyz"
ans=100
for a in A:
  T=S;
  x=0;
  while(T.count(a)!=len(T)):
    U=""
    for i in range(len(T)-1):
      U+=(T[i+1]if T[i+1]==a else T[i])
    T=U
    x+=1
  ans=min(ans,x)
print(ans)

Submission status: Accepted

Solution code:
def main():
    S = list(input())
    ans = 1000
    for t in [chr(ord('a')+i) for i in range(26)]:
        if S.count(t) == len(S):
            print(0)
            return
        nextS = S[:]
        count = 0
        while True:
            changed = False
            deleted = False
            nextnextS = []
            num = 0
            for i in range(len(nextS)-1):
                if nextS[i] == t or nextS[i+1] == t:
                    nextnextS.append(t)
                else:
                    # if num == 0:
                    #     deleted = True
                    #     num += 1
                    # else:
                        changed = True
                        nextnextS.append(nextS[i])
            nextS = nextnextS
            
            count += 1
            if not changed:
                if deleted:
                    count += 1
                break

            
            # print(t,nextS,count)
        ans = min(ans,count)
    print(ans)

        


main()

Submission status: Accepted

Solution code:
def f(s,target):
    cnt = 0
    while len(set(s)) != 1:
        tmps = ""
        for i in range(len(s)-1):
            if s[i] == target and s[i+1] != target:
                tmps += target * 2
            elif s[i] != target and s[i+1] == target:
                tmps += target * 2
            else:
                tmps += s[i]
        cnt += 1
        s = tmps
    return cnt

ans = float('inf')
s = input()
for alp in sorted(set(s)):
    print
    ans = min(f(list(s),alp),ans)
print(ans)

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil,pi,factorial,sqrt
    from operator import itemgetter
    def I(): return int(input())
    def MI(): return map(int, input().split())
    def LI(): return list(map(int, input().split()))
    def LI2(): return [int(input()) for i in range(n)]
    def MXI(): return [[LI()]for i in range(n)]
    def SI(): return input().rstrip()
    def printns(x): print('\n'.join(x))
    def printni(x): print('\n'.join(list(map(str,x))))
    inf = 10**17
    mod = 10**9 + 7
#main code here!
    s=SI()
    n=len(s)
    ans=inf
    c=Counter(list(s))
    #print(c)
    for strs in c.keys():
        mx=0
        key=0
        for i in range(n):
            if s[i]==strs:
                mx=max(mx,key)
                key=0
            else:
                key+=1
        mx=max(mx,key)
        #print(mx)
        ans=min(ans,mx)
    print(ans)
        
            
                
                
        
    
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
from copy import deepcopy
s = input()

ans = 10**10
for i in range(len(s)):
    a = s[i]
    t = deepcopy(s)
    cnt = 0
    
    while len(set(list(t))) !=1:
        
        cnt +=1
        while True:
            S = ""
            for j in range(len(t)-1):
                if (t[j] == a) or (t[j+1] == a):
                    S +=a
                    #print(t,S,j,t[j],t[j+1],a)
                else:
                    S +=t[j]

            else:
                t = deepcopy(S)
                #print(S,cnt)
                break
    ans = min(ans,cnt)
    #print(S,t,i,ans)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
s = input()
memo = [100] * 26
d = Counter(s)
for key in d.keys():
    index = [i for i, x in enumerate(s) if x == key]
    asc = ord(key) - 97
    memo[asc] = max(index[0], len(s) - 1 - index[-1])
    if len(index) >= 2:
        for i in range(1, len(index)):
            memo[asc] = max(memo[asc], index[i] - index[i-1] - 1)
print(min(memo))

Submission status: Accepted

Solution code:
s= list(input())

abc = "abcdefghijklmnopqrstuvwxyz"

ans = len(s)

for a in abc:
    if a in s:
        t = s.index(a)
        anst = t
        for i in range(t+1, len(s)):
            if s[i] == a:
                anst = max(anst, i - t - 1)
                t = i

        anst = max(anst, len(s) - t - 1)

        ans = min(ans, anst)

print(ans)

Submission status: Accepted

Solution code:
import math
s = input()
m = len(s)
for i in range(26):
    c = chr(i+ord('a'))
    t = s
    for j in range(len(t)):
        if t.count(c) == len(t):
            m = min(m, j)
        u = ''
        for k in range(len(t)-1):
            if t[k+1] == c:
                u += c
            else:
                u += t[k]
        t = u
print(m)

Submission status: Accepted

Solution code:
S = input().rstrip()
D = [[-1] for _ in range(26)]
E = [[0] for _ in range(26)]
for i, s in enumerate(S):
    E[ord(s)-ord("a")].append(i-D[ord(s)-ord("a")][-1]-1)
    D[ord(s)-ord("a")].append(i)
for i in range(26):
    E[i].append(len(S)-1-D[i][-1])
res = []
for i in range(26):
    res.append(max(E[i]))
print(min(res))

Submission status: Accepted

Solution code:

max2 = lambda x,y: x if x > y else y
min2 = lambda x,y: x if x < y else y

s = input()
N = len(s)

indices = [[] for _ in range(26)]

for i,c in enumerate(s):
    indices[ord(c)-ord('a')].append(i)


res = N-1
for l in indices:
    if not l:
        continue

    t = 0
    for a,b in zip([-1]+l,l+[N]):
        t = max2(t,b-a-1)
    res = min2(res, t)

print(res)

Submission status: Accepted

Solution code:
from collections import Counter
s = str(input())
l = [chr(i) for i in range(97, 97+26)]

ans = 100
for i in l:
  tmp = s
  cnt = 0
  if i in s:
    while len(Counter(tmp)) > 1:
      c = ''
      for j in range(len(tmp)-1):
        if (tmp[j]==i) or (tmp[j+1]==i):
          c += i
        else:
          c += tmp[j]
      cnt += 1
      tmp = c
    ans = min(ans, cnt)
print(ans)

Submission status: Accepted

Solution code:
s = input()
ans = float('inf')
for i in range(26):
    c = chr(ord('a') + i)
    work_s = s
    cnt = 0
    while len(set(work_s)) != 1:
        next_s = ''
        for i in range(len(work_s) - 1):
            if work_s[i] == c or work_s[i + 1] == c:
                next_s += c
            else:
                next_s += work_s[i]
        work_s = next_s
        cnt += 1
    if ans > cnt:
        ans = cnt

print(ans)

Submission status: Accepted

Solution code:
s=input()
str1=set()
ans=len(s)
for i in range(len(s)):
    str1.add(s[i])
for i in range(len(str1)):
    str2=str1.pop()
    sans=0
    if s[0]==str2:
        cnt=0
    else:
        cnt=1
    for j in range(1,len(s)):
        if s[j]!=str2:
            cnt+=1
        else:
            sans=max(sans,cnt)
            cnt=0
    sans=max(sans,cnt)
    ans=min(ans,sans)
print(ans)

Submission status: Accepted

Solution code:
s = list(str(input()))
alpha = [chr(i) for i in range(97, 97+26)]
temp = [[] for _ in range(26)]
k = 0
for i in range(26):
  for j in range(len(s)):
    if alpha[i] == s[j]:
      temp[i].append(j)
M = []      
for i in range(26):
  m = 0
  l = len(temp[i])
  if l != 0:
    m = max([len(s) - temp[i][l - 1] - 1,temp[i][0]])
    for j in range(l-1):
      m = max([m,temp[i][j+1] - temp[i][j]-1])
    M.append(m)
print(min(M))

Submission status: Accepted

Solution code:
s = input()
n = len(s)
char_in_s = set()
for i in range(n):
    char_in_s.add(s[i])
ans = float('inf')
for char in char_in_s:
    max_interval = 0
    now = 0
    for i in range(n-1,-1,-1):
        if s[i] != char:
            now += 1
        else:
            max_interval = max(max_interval,now)
            now = 0
    max_interval = max(max_interval,now)
    ans = min(ans,max_interval)
print(ans)