Problem:


Score : 300 points

Problem Statement

Two foxes Jiro and Saburo are playing a game called 1D Reversi. This game is played on a board, using black and white stones. On the board, stones are placed in a row, and each player places a new stone to either end of the row. Similarly to the original game of Reversi, when a white stone is placed, all black stones between the new white stone and another white stone, turn into white stones, and vice versa.

In the middle of a game, something came up and Saburo has to leave the game. The state of the board at this point is described by a string S. There are |S| (the length of S) stones on the board, and each character in S represents the color of the i-th (1 ≦ i ≦ |S|) stone from the left. If the i-th character in S is B, it means that the color of the corresponding stone on the board is black. Similarly, if the i-th character in S is W, it means that the color of the corresponding stone is white.

Jiro wants all stones on the board to be of the same color. For this purpose, he will place new stones on the board according to the rules. Find the minimum number of new stones that he needs to place.

Constraints

1 ≦ |S| ≦ 10^5

Each character in S is B or W.

Input

The input is given from Standard Input in the following format:

S

Output

Print the minimum number of new stones that Jiro needs to place for his purpose.

Sample Input 1

BBBWW

Sample Output 1

1

By placing a new black stone to the right end of the row of stones, all white stones will become black. Also, by placing a new white stone to the left end of the row of stones, all black stones will become white.

In either way, Jiro's purpose can be achieved by placing one stone.

Sample Input 2

WWWWWW

Sample Output 2

0

If all stones are already of the same color, no new stone is necessary.

Sample Input 3

WBWBWBWBWB

Sample Output 3

9



Sample input:
BBBWW


Sample output:
1

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def main():
    S = input()

    T = S[0]
    for i in range(1, len(S)):
        if S[i] != T[-1]:
            T += S[i]

    if T == "W":
        print(0)
    elif T == "B":
        print(2)
    else:
        print(len(T) - 1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
s = input()
if s == "W" * len(s):
    print(0)
elif s == "B" * len(s):
    print(1)
else:
    if s == "B" + "W" * (len(s) - 1):
        print(1)
    else:
        num = 0
        k = 1
        if s[0] == "B":
            num += 1
            while s[k] == "B":
                k += 1
        pre = "W"

        for i in range(k, len(s)):
            if pre != s[i]:
                num += 1
            pre = s[i]
        print(num)

Submission status: Wrong Answer

Solution code:
s = input()
if s == "W" * len(s):
    print(0)
elif s == "B" * len(s):
    print(2)
else:
    if s == "B" + "W" * (len(s) - 1):
        print(1)
    else:
        num = 0
        k = 1
        if s[0] == "B":
            num += 1
            while s[k] == "B":
                k += 1
        pre = "W"

        for i in range(k, len(s)):
            if pre != s[i]:
                num += 1
            pre = s[i]
        print(num)

Submission status: Wrong Answer

Solution code:
s = input()
if s[0] == 'W':
    ans = s.count('WB')
elif s[0] == 'B':
    ans = s.count('BW')
if s[0] == s[len(s) - 1]:
    a = 1
else:
    a = 0
print([0,ans * 2 - 1 + a][ans==0])

Submission status: Wrong Answer

Solution code:
S = input()
i = 0
ans = 0
while i < len(S)-1:
  if S[i] == S[i+1]:
    ans += 1
    i += 1
  else:
    i += 1
print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
ans = 0
for i in range(len(S) - 1):
    if S[i] != S[i-1]: ans+=1
print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
t=len(S)
ans=0
for i in range(t-1):
 if S[i-1]!=S[i]:
  ans+=1
 else:
  ans+=0
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    s=input()
    n=len(s)
    if n == 1:
        print(1)
        return
    t=s[0]
    for _ in range(n-1):
        i = _ + 1
        if s[i] != s[i-1]:
            t += s[i]

    w,b = 0,0
    for c in t:
        if c == 'W':
            w = w + 1
        else:
            b = b + 1
    print(w+b-1)
            
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
(lambda line: print(line.count("WB") + int(line.endswith("W"))))(input())

Submission status: Wrong Answer

Solution code:
x = raw_input('> ')
y = []

y.append(x[0])

for i in range(len(x)):
	if y[-1] != x[i]:
		y.append(x[i])

print len(y)-1

Submission status: Wrong Answer

Solution code:
s = input()
if len(s) == 1:
    print("1")
else:
    c = 0
    for i in range(len(s) - 1):
        if s[i] != s[i + 1]:
            c += 1
    print(str(c))

Submission status: Wrong Answer

Solution code:
s=input()
turn=s[0]
res=1
for i in range(1,len(s)):
    if s[i]!=turn:
        res+=1
        turn=s[i]
print(res)

Submission status: Wrong Answer

Solution code:
line = input()

count = 0

if line[0] == 'W':
    if 'WBW' in line:
        count = line.count('WBW') + 2
elif line[0] == 'B':
    if 'BWB' in line:
        count = line.count('BWB') + 2
else:
    pass

if not 'W' in line:
    print(0)
elif not 'B' in line:
    print(0)
else:
    ans = int((int(count / 2) + 1) * 4 - 3)
    print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    S = raw_input()
    count=0
    for i in range(len(S)-1):
        if S[i] != S[i+1]:
            count+=1
    print count

Submission status: Wrong Answer

Solution code:
S = input()

ans = 0

for i in range(1, len(S)):
    if S[i - 1] != S[i]:
        ans += 1

if ans > 0 and S[0] == S[-1]:
    ans -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
S = np.array(input().split("W"))
if len(S) == 1:
    print(0)
else:
    num = len(S[S != '']) * 2 - 1
    if S[0] == '' and S[-1] == '':
        num += 1
    if num < 0:
        num = 0
    print(num)

Submission status: Wrong Answer

Solution code:
import numpy as np
S = np.array(input().split("W"))
num = len(S[S != '']) * 2 - 1
if num < 0:
    num = 0
print(num)

Submission status: Wrong Answer

Solution code:
S = raw_input()
prev = ""
count = -1
for s in S:
    if s != prev:
        count += 1

print count

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
c=0
while True:
  if S[0] == 'W':
    oku = 'B'
  else:
    oku = 'W'
  S.insert(0,oku)
  S[1] = oku
  isbreak = False
  for i in range(2,len(S)):
    if S[i] != oku:
      S[i] = oku
    else:
      isbreak = True
      break
      
  if not isbreak:
    break
  c+=1
print(c)

Submission status: Time Limit Exceeded

Solution code:
s = input()
if len(set(s))==1:
    print(0)
else:
    count = 0
    while True:
        if s[-1]=='W':
            k = 0
            for i in range(-1,len(s)*(-1),-1):
                if s[i]=='B':
                    k = i
                    break
            s = s[0:k+1]+'B'*(k*(-1))+'B'
            count+=1
        elif s[-1]=='B':
            k = 0
            for i in range(-1,len(s)*(-1),-1):
                if s[i]=='W':
                    k = i
                    break
            s = s[0:k+1]+'W'*(k*(-1))+'W'
            count+=1
        if len(set(s))==1:
            break
    print(count)

Submission status: Time Limit Exceeded

Solution code:
S = input()

# 1以上の同じ色の石の列は1つに集約できる
T = S[0]
prev = T
for s in S[1:]:
    if prev != s:
        T += s
        prev = s

print(len(T) - 1)

Submission status: Time Limit Exceeded

Solution code:
S = input()

# 1以上の同じ色の石の列は1つに集約できる
T = S[0]
for s in S[1:]:
    if T[-1] != s:
        T += s

print(len(T) - 1)

Submission status: Time Limit Exceeded

Solution code:
s=list(input())
i=0
while i<len(s)-1: #重複を削除
    if s[i]==s[i+1]:
        s.remove(s[i])
    else:
        i+=1
print(len(s)-1)

Submission status: Time Limit Exceeded

Solution code:
s=input()
count=0
while(True):

	if not 'W' in s or not 'B' in s :
		print(count)
		break

	if s[0]=='W':
		point=s.find('B')
		for i in range(point):
			s=s[:i]+'B'+s[i+1:]
	elif s[0]=='B':
		point=s.find('W')
		for i in range(point):
			s=s[:i]+'W'+s[i+1:]

	count+=1

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = input()
    if len(set(S)) == 1:
        print(0)
    else:
        ans = 0
        while len(set(S)) != 1:
            if S[-1] == 'B':
                x = 'W'
            else:
                x = 'B'

            S = S + x
            S = S[::-1]
            new_s = x
            flag = False
            for si in S[1:]:
                if not flag and si != x:
                    new_s += x
                elif not flag and si == x:
                    new_s += x
                    flag = True
                else:
                    new_s += si
            S = new_s
            ans += 1
            # print(S)
        print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  raw, answer = raw_input(), 0
  s = raw[0]
  for x in xrange(1, len(raw)):
      if raw[x] == s[-1]:
          continue
      s += raw[x]

  while len(set(s)) > 1:
    char, pos = "", 0
    for i in xrange(len(s)-1,-1,-1):
      if s[i] != s[-1]:
        pos, char = i, s[i]
        break
    s = s[0:pos]+char
    answer += 1
  print(answer)


main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  s, answer = raw_input(), 0
  while len(set(s)) > 1:
    char, pos = "", 0
    for i in xrange(len(s)-1,-1,-1):
      if s[i] != s[-1]:
        pos, char = i, s[i]
        break
    s = s[0:pos]+char
    answer += 1
  print(answer)


main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
l = len(s)
i =0
while s != 'W' * (l ) and s != 'B' * (l):
    x = s[0]
    if x == 'B':
        k = s.find('W')
        s = s[k:]
        s = 'W' * (k+1) + s
    else:
        k = s.find('B')
        s = s[k:]
        s = 'B' * (k+1) + s
    i += 1
    l = len(s)
print(i)

Submission status: Time Limit Exceeded

Solution code:
import re
s = input()
x = s[0]
l = len(s)

i =0
while s != 'W' * (l ) and s != 'B' * (l):
    x = s[0]
    if x == 'B':
        k = s.find('W')
        s = s[k:]
        s = 'W' * (k+1) + s

    else:
        k = s.find('B')
        s = s[k:]
        s = 'B' * (k+1) + s

    i += 1
    l = len(s)
print(i)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())

def check_S():
    check = S[0]
    for char in S:
        if check != char:
            return True
    return False

count = 0
while check_S():
    check = S[0]
    k = 0
    if check == 'B':
        while S[k] == 'B':
            S[k] = 'W'
            k += 1
    elif check == 'W':
        while S[k] == 'W':
            S[k] = 'B'
            k += 1
    count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
i = 0
count = 0
while True:
    #print("line6 s=",s,"i =",i,"s[i] =",s[i])
    if len(s)==1:break
    if len(set(s)) == 1:break
    while s[i] == s[i+1]and i<len(s)-2:
        i+=1
    tmp = s[i+1]
    count += 1
    j=0
    while j<=i:
        s[j]=tmp
        j+=1

print(count)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
i = 0
count = 0
while True:
    #print("line6 s=",s,"i =",i,"s[i] =",s[i])
    if len(s)==1:break
    while s[i] == s[i+1]and i<len(s)-2:
        i+=1
    if len(set(s)) == 1:
        break
    tmp = s[i+1]
    count += 1
    j=0
    while j<=i:
        s[j]=tmp
        j+=1

print(count)

Submission status: Time Limit Exceeded

Solution code:
S = input()


def reverse(rep, target):
    for i in range(0, len(target)):
        if target[i] == rep:
            break
        target = target[:i] + rep + target[i + 1:]
    return target


cnt = 0

while not len(S) == S.count('W') and not len(S) == S.count('B'):
    if S[0] == 'B':
        S = reverse('W', S)
        S = 'W' + S
    else:
        S = reverse('B', S)
        S = 'B' + S

    cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = [i for i in input()]
ans = 0
while s.count('B') != 0 and s.count('W') != 0:
    lim,init = len(s)-1,s[-1]
    while s[lim] == init:
        if s[lim] is 'B':   s[lim] = 'W'
        else:   s[lim] = 'B'
        lim-=1
    if init is 'B': s.append('W')
    else:   s.append('B')
    ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
ans = 0
#sが全部Wか全部Bになるまでループ
while s.count('W')!=0 and s.count('B')!=0:
    #一番右がBならWを置き、sの一番右のWまでWにする
    if s[-1] == 'B':
        W_usiro = s.rfind('W')
        s = s[0:W_usiro+1] + s[W_usiro+1:].replace('B','W') + 'W'
    #一番右がWならBを置き、sの一番右のBまでBにする
    else:
        B_usiro = s.rfind('B')
        s = s[0:B_usiro+1] + s[B_usiro+1:].replace('W','B') + 'B'

    ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
T = [s for s in S]
cnt=0
while not (all(t=='W' for t in T) or all(t=='B' for t in T)):
    if(T[0]=='W'):
        cnt+=1
        T = ['B'] + T
        for i in range(1,len(T)):
            if T[i]=='W':
                T[i]='B'
            else:
                break
    else:
        cnt+=1
        T = ['W'] + T
        for i in range(1,len(T)):
            if T[i]=='B':
                T[i]='W'
            else:
                break
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = input()
ans = 0
while len(set(s)) == 2:
    c = s[0]
    for i in range(len(s)):
        if s[i] != c:
            idx = i
            break
    s = s[i:]
    ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin

def reversi(s,a):
    if s == a:
        return "end"
    if s[-1] == a:
        return reversi(s[:-1],a)
    else:
        return s
    
s = stdin.readline().rstrip()
ans = 0

while True:
    if len(list(set(s))) == 1:
        break
    else:
        s = reversi(s,s[-1])
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def getinputdata():

    # 配列初期化
    array_result = []
    
    data = input()
    
    array_result.append(data.split(" "))

    flg = 1

    try:

        while flg:

            data = input()
            if(data != ""):
                
                array_result.append(data.split(" "))

            else:

                flg = 0
    finally:

        return array_result

arr_data = getinputdata()

s = arr_data[0][0]

wposi1=0
bposi1=0

cnt=0
while not(all(x =="W" for x in list(s)) or all(x =="B" for x in list(s))):
    
    if s[0]=="W":

        itiw=s.find("W",wposi1)
        itib=s.find("B",bposi1)
        s=s.replace("W","B",itib-itiw)
        
    elif s[0]=="B":

        itiw=s.find("W",wposi1)
        itib=s.find("B",bposi1)
        s=s.replace("B","W",itiw-itib)
    
    cnt+=1
    
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
l = list(input())
op = 0
while True:
    ss = None
    s_p = l[0]
    for i, s in enumerate(l[1:], start=1):
        if s == s_p:
            continue
        else:
            for i, m in enumerate(l[:i]):
                l[i] = s
            ss = s
            op += 1
            break
    if ss is None:
        break
    else:
        l.insert(0, ss)

print(op)

Submission status: Time Limit Exceeded

Solution code:
s = input()

x = ''
if s[-1] == 'B':
    x = 'W'
else:
    x = 'B'

cnt = 0
while True:
    i = s.rfind(x)
    if i != -1:
        s = s[:i] + "".join([x]*(len(s[i:]) + 1))
        cnt += 1
        if x == 'B':
            x = 'W'
        else:
            x = 'B'
    else:
        break
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
S = input()
ans = 0
if S.count('W') == 0 or S.count('B') == 0:
    print(0)
    exit()
a = S[0]
while True:
    for i, s in enumerate(S):
        if s != a: break
    S = S[i:]
    a = S[0]
    ans += 1
    if S.count('W') == 0 or S.count('B') == 0:break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import re

S = input()

steps = 0

while not (re.findall('W*', S)[0] == S or re.findall('B*', S)[0] == S):
    cur = re.findall('^W+', S)
    if cur and cur[0] != S:
        temp = ''
        for i in range(len(cur[0])):
            temp += 'B'
        S = temp + S[len(cur[0]):]
        steps += 1

    # print(S)

    cur = re.findall('^B+', S)
    if cur and cur[0] != S:
        temp = ''
        for i in range(len(cur[0])):
            temp += 'W'
        S = temp + S[len(cur[0]):]
        steps += 1

    # print(S)

    cur = re.findall('W+$', S)
    if cur and cur[0] != S:
        temp = ''
        for i in range(len(cur[0])):
            temp += 'B'
        S = S[:len(S) - len(cur[0])] + temp
        steps += 1

    # print(S)

    cur = re.findall('B+$', S)
    if cur and cur[0] != S:
        temp = ''
        for i in range(len(cur[0])):
            temp += 'W'
        S = S[:len(S) - len(cur[0])] + temp
        steps += 1

    # print(S)

print(str(steps))

Submission status: Time Limit Exceeded

Solution code:
import re

S = input()

steps = 0

while not (re.findall('W*', S)[0] == S or re.findall('B*', S)[0] == S):
    # add one black left
    temp_S = S
    i = 0
    while temp_S[i] != 'B':
        temp_S = temp_S[:i] + 'B' + temp_S[i+1:]

        i += 1
        if i == len(temp_S) - 1:
            if temp_S[i] == 'W':
                temp_S = S
                break
        elif i == len(temp_S):
            break
    if temp_S != S:
        steps += 1
        S = 'B' + temp_S

    # print(S)

    # add one white left
    temp_S = S
    i = 0
    while temp_S[i] != 'W':
        temp_S = temp_S[:i] + 'W' + temp_S[i+1:]

        i += 1
        if i == len(temp_S) - 1:
            if temp_S[i] == 'B':
                temp_S = S
                break
        elif i == len(temp_S):
            break
    if temp_S != S:
        steps += 1
        S = 'W' + temp_S

    # print(S)

    # add one black right
    temp_S = S
    i = len(S) - 1
    while temp_S[i] != 'B':
        temp_S = temp_S[:i] + 'B' + temp_S[i+1:]

        i -= 1
        if i == 0:
            if temp_S[i] == 'W':
                temp_S = S
                break
        elif i == -1:
            break
    if temp_S != S:
        steps += 1
        S = temp_S + 'B'

    # print(S)

    # add one white right
    temp_S = S
    i = len(S) - 1
    while temp_S[i] != 'W':
        temp_S = temp_S[:i] + 'W' + temp_S[i+1:]

        i -= 1
        if i == 0:
            if temp_S[i] == 'B':
                temp_S = S
                break
        elif i == -1:
            break
    if temp_S != S:
        steps += 1
        S = temp_S + 'W'

    # print(S)
    

# print(S)
print(str(steps))

Submission status: Time Limit Exceeded

Solution code:
import re

S = input()

steps = 0

while not (re.findall('W*', S)[0] == S or re.findall('B*', S)[0] == S):
    # add one black left
    temp_S = S
    i = 0
    while temp_S[i] != 'B':
        temp_S = temp_S[:i] + 'B' + temp_S[i+1:]

        i += 1
        if i == len(temp_S) - 1:
            if temp_S[i] == 'W':
                temp_S = S
                break
        elif i == len(temp_S):
            break
    if temp_S != S:
        steps += 1
        S = 'B' + temp_S

    # print(S)

    # add one white left
    temp_S = S
    i = 0
    while temp_S[i] != 'W':
        if i == 0:
            temp_S = 'W' + temp_S[1:]
        else:
            temp_S = temp_S[:i] + 'W' + temp_S[i+1:]

        i += 1
        if i == len(temp_S) - 1:
            if temp_S[i] == 'B':
                temp_S = S
                break
        elif i == len(temp_S):
            break
    if temp_S != S:
        steps += 1
        S = 'W' + temp_S

    # print(S)

    # add one black right
    temp_S = S
    i = len(S) - 1
    while temp_S[i] != 'B':
        if i == 0:
            temp_S = 'B' + temp_S[1:]
        else:
            temp_S = temp_S[:i] + 'B' + temp_S[i+1:]

        i -= 1
        if i == 0:
            if temp_S[i] == 'W':
                temp_S = S
                break
        elif i == -1:
            break
    if temp_S != S:
        steps += 1
        S = temp_S + 'B'

    # print(S)

    # add one white right
    temp_S = S
    i = len(S) - 1
    while temp_S[i] != 'W':
        temp_S = temp_S[:i] + 'W' + temp_S[i+1:]

        i -= 1
        if i == 0:
            if temp_S[i] == 'B':
                temp_S = S
                break
        elif i == -1:
            break
    if temp_S != S:
        steps += 1
        S = temp_S + 'W'

    # print(S)
    

# print(S)
print(str(steps))

Submission status: Time Limit Exceeded

Solution code:
S = input()
B = []
Nc = 0

while S != 'W'*len(S) and S != 'B'*len(S):
    if S.index('B') > S.index('W'):
        S = S.replace('W','B',S.index('B'))
        Nc += 1
    else:
        S = S.replace('B','W',S.index('W'))
        Nc += 1
print(Nc)

Submission status: Time Limit Exceeded

Solution code:
S = input()
B = []
Nc = 0
Ns = 0
for i in range(len(S)):
    if S[i] == 'W':
        B.append(1)
    if S[i] == 'B':
        B.append(0)

while B != [1]*len(B) and B != [0]*len(B):
    if B.index(0) > B.index(1):
        B = [1] + B
        B[0:B.index(0)+1] = [0]*(B.index(0)+1)
        Nc += 1
    else:
        B = [0] + B
        B[0:B.index(1)+1] = [1]*(B.index(1)+1)
        Nc += 1
print(Nc)

Submission status: Time Limit Exceeded

Solution code:
s = input()
ans = 0
def neg(c):
    if c == 'W':
        return 'B'
    else:
        return 'W'

while 'W' in s and 'B' in s:
    ans += 1
    l = s[0]
    r = s[-1]
    cl = 0
    cr = len(s) - 1
    while l == s[cl]:
        cl += 1
    while r == s[cr]:
        cr += -1
    if cr > cl:
        s = s[:cr] + (len(s) - cr + 1) * neg(r)
    else:
        s = (cl+1) * neg(l) + s[cl:]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict


memo = {}


def dfs(s):
    tuple_s = tuple(s)
    if tuple_s in memo:
        return memo[tuple_s]

    if s.count("B") == len(s) or s.count("W") == len(s):
        return 0

    ans = 10 ** 20
    stone = s[0]
    t = s[:]
    while t[0] == stone:
        t = t[1:]
    ans = min(ans, dfs(t) + 1)

    stone = s[-1]
    t = s[:]
    while t[-1] == stone:
        t = t[:-1]
    ans = min(ans, dfs(t) + 1)

    memo[tuple_s] = ans
    return ans


def main():
    s = input()
    print(dfs(list(s)))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict


def dfs(s):
    if s.count("B") == len(s) or s.count("W") == len(s):
        return 0

    ans = 10 ** 20
    stone = s[0]
    t = s[:]
    while t[0] == stone:
        t = t[1:]
    ans = min(ans, dfs(t) + 1)

    stone = s[-1]
    t = s[:]
    while t[-1] == stone:
        t = t[:-1]
    ans = min(ans, dfs(t) + 1)

    return ans


def main():
    s = input()
    print(dfs(list(s)))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s=raw_input();i=a=0;exec"a+=s[i]!=s[-~i];i+=1;"*~-len(s);print a

Submission status: Time Limit Exceeded

Solution code:
S = input()
list = []
for i in range( len(S) ):
   if  S[i]=="B":
       list.append(int(0))
   else:
       list.append(int(1))
counter = 0

while (list.count(0)!=0) or (list.count(1)!=0):
    if (list.count(0)==0) or (list.count(1)==0):
        break

    
    if list[0] == 0:
        x = list.index(1)
        for i in range(x):
            list[i]=1
    elif list[0] == 1:
        x = list.index(0)
        for i in range(x):
            list[i]=0

    counter += 1

print(counter)

Submission status: Time Limit Exceeded

Solution code:
def line(s):
    ret = [0] * len(s)
    for i in range(len(s)):
        ret[i] = 0 if s[i] == 'W' else 1

    return ret

def all_same(arr):
    for i in range(len(arr)-1):
        if arr[i] != arr[i+1]:
            return False
    return True

def opposite(x):
    return (x - 1) ** 2

def update(arr):
    count = 0
    while(not all_same(arr)):
        append = opposite(arr[-1])
        other = arr[-1]
        i = -1
        while(arr[i] == other or -i == len(arr) + 1):
            arr[i] = append
            i -= 1
        if -i == len(arr)/2:
            arr.reverse()
        count += 1
    return count

if __name__ == '__main__':
    s = raw_input()
    arr = line(s)
    print(update(arr))

Submission status: Time Limit Exceeded

Solution code:
def line(s):
    ret = [0] * len(s)
    for i in range(len(s)):
        ret[i] = 0 if s[i] == 'W' else 1

    return ret

def all_same(arr):
    for i in range(len(arr)-1):
        if arr[i] != arr[i+1]:
            return False
    return True

def opposite(x):
    return (x - 1) ** 2

def update(arr):
    count = 0
    while(not all_same(arr)):
        append = opposite(arr[-1])
        other = arr[-1]
        i = -1
        while(arr[i] == other or -i == len(arr) + 1):
            arr[i] = append
            i -= 1
        count += 1
    return count

if __name__ == '__main__':
    s = raw_input()
    arr = line(s)
    print(update(arr))

Submission status: Time Limit Exceeded

Solution code:
def isSame(string):
    for i in range(1, len(string)):
        if string[i] != string[0]:
            return False
    return True

s = raw_input()
a = []
l = len(s)
for i in range(l):
    a.append(s[i])

n = 0

while not isSame(a):
    if a[0] == 'B':
        for i in range(l):
            if a[i] == 'B':
                a[i] = 'W'
            else:
                break
        a.insert(0, 'W')
    elif a[0] == 'W':
        for i in range(l):
            if a[i] == 'W':
                a[i] = 'B'
            else:
                break
        a.insert(0, 'B')
    n += 1
    l += 1

print n

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
cnt = 0
while len(set(S)) !=1:
    if S[-1] == 'W':
        j=1
        while S[-j] != 'B':
            S[-j] = 'B'
            j+=1
        S.append('B')
    elif S[-1] == 'B':
        j=1
        while S[-j] != 'W':
            S[-j] = 'W'
            j+=1
        S.append('W')
    cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
ans = []
S=list(input())
for i in range(len(S)-1):
  if S[i] != S[i+1]:
    ans.append(S[i])
print(ans.count("W")+ans.count("B"))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 30 02:12:31 2020

@author: liang
"""

S = input()
ans = 0
for i in range(len(S) - 1):
    if S[i] != S[i+1]:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
s=input()

color = s[0]
result = 0
for i in range(len(s)):
  if color != s[i]:
    result += 1
    color = s[i]
print(result)

Submission status: Accepted

Solution code:
s = input()
count = 0

for i in range(len(s)-1):
  if s[i] != s[i+1]:
    count += 1
print(count)

Submission status: Accepted

Solution code:
s = input()

s_ = s[0]

for c in s:
    if s_[-1]!=c:
        s_ += c
print(len(s_)-1)

Submission status: Accepted

Solution code:
s = input()
ans = 0

for i in range(len(s)-1):
  if s[i] != s[i+1]:
    ans += 1

print(ans)

Submission status: Accepted

Solution code:
def main():
    S = input()
    ans = 0
    for i, s in enumerate(S):
        if i == 0:
            t = s
            continue
        if t == s:
            continue
        else:
            ans += 1
            t = s
    print(ans)
        
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
s = input()
count = 0
for i in range(len(s)-1):
    if s[i] != s[i+1]:
        count += 1
print(count)

Submission status: Accepted

Solution code:
s = input()
cnt = 0
for i in range(len(s)-1):
    if s[i] != s[i+1]:
        cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
S = input()
ans = 0

for i in range(len(S)-1):
  if S[i] != S[i+1]:
    ans += 1

print(ans)

Submission status: Accepted

Solution code:
s = input()
l = len(s)
cnt = 0
for i in range(1, l):
    if s[i] != s[i - 1]:
        cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
s = input()
prev = s[0]
ans = 0
for i in range(1, len(s)):
    if s[i] != prev:
        ans += 1
    prev = s[i]
print(ans)

Submission status: Accepted

Solution code:
import itertools
s = input()
print(len(list(itertools.groupby(s)))-1)

Submission status: Accepted

Solution code:
s = input()
cnt = 0
for i in range(1,len(s)):
  if s[i] != s[i-1]: 
    cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
s = input()
cnt = 1
cw = s[0]
for i in range(len(s)):
  if s[i] != cw: 
    cnt += 1
    cw = s[i]
print(cnt - 1)

Submission status: Accepted

Solution code:
s=list(input())
cnt=0
bf=s[0]
for i in s[1:]:
  if i!=bf:
    cnt+=1
  bf=i
print(cnt)

Submission status: Accepted

Solution code:
from itertools import groupby
print(len(list(groupby(input())))-1)

Submission status: Accepted

Solution code:
s = input()
counter = 0

for i in range(1, len(s)):
    if s[i] != s[i - 1]:
        counter += 1

print(counter)

Submission status: Accepted

Solution code:
s = list(input())
cnt = 0
for i in range(len(s)-1):
    if s[i] != s[i+1]:
        cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
S=input()
c=0
for i in range(len(S)-1):
  if S[i]!=S[i+1]:
    c+=1
print(c)

Submission status: Accepted

Solution code:
s=input()
x=s.count("WB")
y=s.count("BW")
print(x+y)

Submission status: Accepted

Solution code:
def main():
    s = str(input())
    b_count = s.count('B')
    w_count = len(s) - b_count

    if b_count == len(s) or w_count == len(s):
        count = 0
    else:
        count = 0
        index = 0
        alp = s[index]

        while True:
            if b_count == 0 or w_count == 0:
                break
            else:
                if s[index] == 'B':
                    b_count -= 1
                else:
                    w_count -= 1

                if s[index] == alp:
                    index += 1
                else:
                    count += 1
                    alp = s[index]
                    index += 1

        if not (b_count == 0 and w_count == 0):
            count += 1

    print(count)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S = [1 if x=="W" else 0 for x in input()]

groups = []
tmp = 0
zeros = 0
for n in S:
    if n == 1:
        if zeros != 0:#直前まで０だったとき
            groups.append(zeros)
            zeros = 0#リセット
        tmp += 1
    elif n == 0:
        if tmp != 0:#直前まで１だったとき
            groups.append(tmp)
            tmp = 0#リセット
        zeros -= 1

#終端の処理
if tmp != 0:
    groups.append(tmp)
if zeros != 0:
    groups.append(zeros)
    
print(len(groups)-1)

Submission status: Accepted

Solution code:
s = input()

n = len(s)

cnt = 0
for i in range(1,n):
  if s[i] != s[i-1]:
    cnt +=1
print (cnt)

Submission status: Accepted

Solution code:
S = input()
List = list(S)
trial = 0
for i in range(1,len(List)):
  if List[i] != List[i-1]:
    trial += 1
print(trial)

Submission status: Accepted

Solution code:
S = input()

ls = ["WB", "BW"]

cnt = 0
if S[0] == "W":
  i = 0
else:
  i = 1

for j in range(len(S)-1):
  if i == 0 and S[j:j+2] == "WB":
    cnt += 1
    i = 1
  if i == 1 and S[j:j+2] == "BW":
    cnt += 1
    i = 0
    
print(cnt)