Problem:


Score : 300 points

Problem Statement

There are N people standing in a row from west to east.
Each person is facing east or west.
The directions of the people is given as a string S of length N.
The i-th person from the west is facing east if S_i = E, and west if S_i = W.

You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader.
Here, we do not care which direction the leader is facing.

The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized.
Find the minimum number of people who have to change their directions.

Constraints

2 \leq N \leq 3 \times 10^5

|S| = N

S_i is E or W.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the minimum number of people who have to change their directions.

Sample Input 1

5
WEEWW

Sample Output 1

1

Assume that we appoint the third person from the west as the leader.
Then, the first person from the west needs to face east and has to turn around.
The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case.
It is not possible to have 0 people who have to change their directions, so the answer is 1.

Sample Input 2

12
WEWEWEEEWWWE

Sample Output 2

4

Sample Input 3

8
WWWWWEEE

Sample Output 3

3



Sample input:
5
WEEWW


Sample output:
1

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
min_c = 9999999

for i in range(n):
   if i == 0:
      cnt = s.count("E")
      if s[0] == "E":
         cnt = cnt - 1
   if i == n -1:
      cnt = s.count("W")
      if s[n-1] == "W":
         cnt -= 1
   else: 
      a = s[:i]
      b = s[i+1:]
      a_cnt = a.count("W")
      b_cnt = b.count("E")
      cnt = a_cnt + b_cnt
   min_c = min(min_c, cnt)

print(min_c)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

n = int(raw_input())
s = raw_input()

minn = float("inf")

minn = s[:n-2].count("W")
xx = s[1:].count("E")
if xx < minn: minn = xx

for i in range(1,n-1):
	counter = s[:i-1].count("W") + s[i+1:].count("E")
	if counter <= minn: minn = counter

print minn

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
min = S[0:1].count("W") + S[2:N].count("E")

for i in range (N):
    count = S[0:i].count("W") + S[i+1:N].count("E")
    if min > count:
        min = count
print(min)

Submission status: Time Limit Exceeded

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

ans = 3*10^5

for i in range(N):
    n = 0
    for j in range(i):
        if "W" == S[j]:
            n += 1
    for j in range(i+1, N):
        if "E" == S[j]:
            n += 1

    if ans > n:
        ans = n

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
total = 0
l_c, r_c = 0,0
min = 3*10**5 + 1
for i in range(n):
    left = s[0:i]
    right = s[i+1:n]
    if(left and right):
        l_c = left.count('W')
        r_c = right.count('E')
        total = l_c + r_c
    elif(left):
        l_c = left.count('W')
        total = l_c
    elif(right):
        r_c = right.count('E')
        total = r_c

    if(min > total):
        min = total
        total = 0

print(min)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N = int(input())
s= input()
s = s.replace("E", "0")
s = s.replace("W", "1")

D = N
for i in range(N):
    temp = 0
    for j in range (N):
        if (i < j) and (s[j] == "0" ):
            temp += 1
        if (i > j) and (s[j] == "1" ):
            temp += 1 
    if temp < D:
        D = temp
print(D)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
min = 30000000
for i in range(1,n-1):
    be = s[:i]
    af = s[i+1:n]
    cont = 0
    for j in range(len(be)):
        if be[j] == 'W':
            cont += 1
    for k in range(len(af)):
        if af[k] == 'E':
            cont += 1
    if cont < min:
        min = cont

a = s[1:n]
cont = 0
for w1 in range(n-1):
    if a[w1] == 'E':
        cont += 1
if cont < min:
    min = cont

a = s[:n-1]
cont = 0
for w2 in range(n-1):
    if a[w2] == 'W':
        cont += 1
if cont < min:
    min = cont

print(min)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=list(input())
ans=3000000

for i in range(n):
    counts=0
    counts=s[:i].count("W")
    counts=counts+s[i+1:].count("E")
    ans=min(ans,counts)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()

changes = []
for leader in range(n):
    change = 0
    change += s[:leader-1].count("W")
    change += s[leader+1:].count("E")
    changes.append(change)

print(min(changes))

Submission status: Time Limit Exceeded

Solution code:
N = input()
S = input()
times = []
N = int(N)

for i in range(N):
    count = 0
    for j in range(N):
        if j < i:
            if S[j] == "W":
                count += 1
        else:
            if S[j] == "E":
                count += 1
    times.append(count)

print(min(times))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ans = 10**6
for i in range(len(s)):
    x = s[:i]
    y = s[i+1:len(s)]
    ans = min(ans,x.count("W")+y.count("E"))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
s = list(input())
r = 3 * 10**5 + 1
for l in range(n):
  sm = s[:l].count('W') + s[l:].count('E')
  r = min(r, sm)

print(r)

Submission status: Time Limit Exceeded

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

result = []

def cal(n):
    count = 0
    for i in range(N):
        if i < n:
            if S[i] == "W":
                count += 1
        elif i > n:
            if S[i] == "E":
                count += 1
    return count

for i in range(N):
    result.append(cal(i))
    
# print(result)
print(min(result))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())

S=input()

#print(S)

list1=[]

for i in range(N):
    E=S[:i].count("W")
    W=S[i+1:].count("E")
    list1.append(E+W)
    
print(min(list1))

Submission status: Time Limit Exceeded

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

ans = N*N
tmp = ""

for i in range(N):
    tmp += S[i]
    ct = 0
    if i != 0:
        for j in tmp:
            if j == 'W':
                ct += 1

    for j in range(i+1, N):
        if S[j] == 'E':
            ct += 1

    #print(ct, tmp)
    ans = min(ans, ct)

print(ans)

Submission status: Time Limit Exceeded

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

count=0
min=1000000

for j in range(len(s)):
    if s[j]!="W":
        count+=1
        min=count

for i in range(N):
    a=s[:i+1]
    b=s[i+1:]
    for j in range(len(a)):
        if a[j]!="E":
            count+=1
    for k in range(len(b)):
        if b[k]!="W":
            count+=1
    if min>count:
        min=count
    count=0

print(min)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
s = list(s)
count = 0
count_up = []

for i_all in range(n):
    leader = s[i_all]
    if i_all == 0:
        for i_first in range(1,n):
            if s[i_first] == "E":
                count += 1
    elif i_all == n-1:
        for i_last in range(n-1):
            if s[i_last] == "W":
                count += 1
    else:
        for i_a in range(i_all):
            if s[i_a] == "W":
                count +=1
        for i_b in range(i_all+1,n):
            if s[i_b] == "E":
                count += 1
    count_up.append(count)
    count = 0
print(min(count_up))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()

ms = n
for i in range(n):
	c = s[0:i].count('W')
	c += s[i+1:n].count('E')
	if(ms > c):
		ms = c

print(ms)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
max = 0
re = 0
lis = []
for i in range(N):
    X = S[0:i+1]
    Y=S[i:N+1]
    XN = X.count("E")
    YN = Y.count("W")
    l = YN +XN
    if(l>max):max = l

print(N -max)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())

minNum = N
for i in range(N):
    cost = 0
    for j in range(N):
        if cost > minNum:
            break
        if i == j:
            continue
        if j < i:
            if S[j] == 'W':
                cost += 1
        else :
            if S[j] == 'E':
                cost += 1
    if minNum > cost:
        minNum = cost

print(cost-1)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
st = input()
l = []
stc = Counter(st)
if(stc["W"] == stc["E"] -1 or stc["W"] == stc["E"] +1 or stc["W"] == stc["E"]):
    for i in range(n):
        east = Counter(st[:i])["W"]
        west = Counter(st[i + 1:])["E"]
        l.append(east + west)
else:
    t = stc.most_common(1)[0][0]
    for i in range(n):
        if(st[i] == t):
            east = Counter(st[:i])["W"]
            west = Counter(st[i + 1:])["E"]
            l.append(east + west)
print(min(l))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
S = input()

c = N
for a in range(0, N):
	left = Counter(S[0:a])
	right = Counter(S[(a+1):N])
	c = min(c, left['W'] + right['E'])
print(c)

Submission status: Time Limit Exceeded

Solution code:
import sys
if __name__ == "__main__":
    N = int(input())
    S = input()
    min = 100000
    for i in range(0, N):
        ws = S[0:i]
        es = S[i+1:N]
        cnt = 0
        cnt += ws.count('W')
        #for ele in ws:
        #    if ele == 'W':
        #        cnt += 1
        cnt += es.count('E')
        #for ele in es:
        #    if ele == 'E':
        #        cnt += 1
#        print(ws)
#        print(es)
#        print(cnt)
        if cnt < min:
            min = cnt
    print(min)

Submission status: Time Limit Exceeded

Solution code:
# ABC098_C
num = int(input())
text = input()

answer=num

for i in range(0, num):
    #Leader = i
    change = 0

    change = text[0:i].count('W')+text[i+1:num].count('E')
    if (answer > change):
        answer = change

print(answer)

Submission status: Time Limit Exceeded

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

res = N
for i in range(N):
    if i==0:
        string = "W"*N
    else:
        string = "E"*(i-1)+S[i]+"W"*(N-i)
    num = 0
    for j in range(N):
        if string[j]!=S[j]:
            num+=1
    res = min(res,num)
    
print(res)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
str = input()
str_list=list(str)
answer_list=[]
for i in range (N):
  answer=0
  for j in range (N):
    if str_list[j] == "E":
      seki = -1
    else:
      seki = 1
    point = (j-i)*seki
    if point<0:
      answer = answer+1
  answer_list.append(answer)
print(min(answer_list))

Submission status: Time Limit Exceeded

Solution code:
import collections
n, s = int(input()), input()
cost = 2*n
for i in range(n):
    temp = collections.Counter(s[:i])['W']+collections.Counter(s[i+1:])['E']
    if temp<cost:
        cost = temp
print(cost)

Submission status: Time Limit Exceeded

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

for i in range(N):
 chE = S[0:i].count("W")
 chW = S[i+1:N].count("E")
 if i == 0:
  min = chE + chW
 elif min > chE + chW:
  min = chE + chW

print(min)

Submission status: Time Limit Exceeded

Solution code:
def abc098_c():
    n = int(input())
    s = input()
    counts = [s[:i].count("W") + s[i + 1:].count("E") for i in range(n)]
    return min(counts)


print(abc098_c())

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=list(input())
ans=300000

for i in range(n):
    count=0
    for j in range(i):
        if s[j]=="W":
            count+=1
    for j in range(i+1,n):
        if s[j]=="E":
            count+=1
    ans=min(ans,count)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
temp_min =10**15
for i in range(1,N):
    temp = S[0:i-1]
    temp2 = S[i:N]
    temp3 = temp.count("W")+temp2.count("E")
    if temp_min > temp3:
        temp_min = temp3
print(temp_min)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()#西→東方向でi番目並んだ人が向いている向きSi, E=東, W=西

#i番目をリーダーとするとき
#<i番目はE
#i<番目はW

ans = n - 1
for i in range(n):
    buf = 0
    for j in range(n):
        if j < i:
            if s[j] == 'W':
                buf += 1
        if j > i:
            if s[j] == 'E':
                buf += 1
    ans = min(ans, buf)
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
S = input()
s = [0 for i in range(n)]

for i in range(n):
    if S[i] == "E":
        s[i] = 1
min = n

for i in range(n):
    count = 0
    for j in range(n):
        if i < j and s[j] == 1:
            count += 1
        elif i > j and s[j] == 0:
            count += 1
    if count < min:
        min = count
print(min)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()

ans = 3 * 10**5

for i in range(n):
    ans = min(ans, s[:i].count('W') + s[i+1:].count('E'))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
P = list(input())
X = 100000000
for i in range(N):
    x = (P[:i].count("W")+P[i+1:].count("E"))
    if x < X:
        X = x
print(X)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
temp_min =10**15
for i in range(1,N):
    temp = S[0:i-1]
    temp2 = S[i:N]
    if temp_min > temp.count("W")+temp2.count("E"):
        temp_min = temp.count("W")+temp2.count("E")
print(temp_min)

Submission status: Time Limit Exceeded

Solution code:
# ABC098_C
num = int(input())
text = input()

answer=num

for i in range(0, num):
    #Leader = i
    change = 0
    for j in range(0, num):
        if (j<i) and (text[j]=="W"):
            change += 1
        if (i<j) and (text[j]=="E"):
            change += 1
    if (answer > change):
        answer = change

print(answer)

Submission status: Time Limit Exceeded

Solution code:
import sys
if __name__ == "__main__":
    N = int(input())
    S = input()
    min = 100000
    for i in range(0, N):
        ws = S[0:i]
        es = S[i+1:N]
        cnt = 0
        for ele in ws:
            if ele == 'W':
                cnt += 1
        for ele in es:
            if ele == 'E':
                cnt += 1
        if cnt < min:
            min = cnt
    print(min)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()

result = 10**10
for i in range(n):
    west_s = s[:i]
    east_s = s[(i+1):]
#    print(west_s)
#    print(east_s)
    
#    print(west_s.count("W"))
#    print(east_s.count("E"))
    
    if((west_s.count("W")+east_s.count("E")) < result):
        result = west_s.count("W")+east_s.count("E")

print(result)

Submission status: Time Limit Exceeded

Solution code:
N = input()
S = list(input())
min_ = 400000
for i in range(len(S)):
    left = S[:i]
    right = S[i+1:]
    num_change = left.count('W')+right.count('E')
    if num_change < min_:
        min_ = num_change
print(min_)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
for i in range(N):
    turn_sum = len([S[q] for q in range(N) if (i < q and S[q] == "E") or (i > q and S[q] == "W")])
    if i == 0:
        min = turn_sum
    if min > turn_sum:
        min = turn_sum
print(min)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
direction = list(input())
change = [0]*N
 
for i in range(N):
    left = direction[0:i]
    right = direction[i+1:N]
    change[i] += left.count("W")
    change[i] += right.count("E")
    # print(left,right)
print(min(change))

Submission status: Time Limit Exceeded

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

changes = []

wests = tuple([i for i in range(N) if S[i] == 'W'])
easts = tuple([i for i in range(N) if S[i] == 'E'])

# print(wests)
# print(easts)


for leader in range(N):
    change = 0
    change += len([i for i in range(N) if i < leader and S[i] == 'W'])
    change += len([i for i in range(N) if leader < i and S[i] == 'E'])
    changes.append(change)

print(min(changes))

Submission status: Time Limit Exceeded

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

# AtCoder Beginner Contest
# Problem C

if __name__ == '__main__':
    n = int(input())
    s = input()
    count = 0

    for i in range(len(s)):
        if i == 0:
            count = s[1:].count('E')
        else:
            count = min(count, s[:i].count('W') + s[i + 1:].count('E'))

    print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
m=n+5
for i in range(n):
    c=0
    w=s[:i]
    e=s[i+1:]
    for j in w:
        if j=='W':
            c+=1
    for j in e:
        if j=='E':
            c+=1
    m=min(m,c)
print(m)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
ans = 10**7
for i in range(n):
	count = 0
	a = s[:i]
	b = s[i:]
	ac = a.count("W")
	bc = b.count("E")
	count = ac + bc
	if count <= ans:
		ans = count
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
m = n
for i in range(n):
    x = s[:i]
    y = s[i+1:]
    m = min(m, x.count('W')+y.count('E'))
print(m)

Submission status: Time Limit Exceeded

Solution code:

def abc098_c():
    import numpy as np
    n = int(input())
    s = input()
    counts = np.zeros(n)
    for i in range(n):
        west = s[:i]
        east = s[i+1:]
        counts[i] = west.count("W") + east.count("E")
    return int(min(counts))


print(abc098_c())

Submission status: Time Limit Exceeded

Solution code:

def main():
    n = int(input().strip())
    s = input().strip()

    m = 300000
    for i in range(0, len(s)):
        c = len([x for x in s[0:i] if x == 'W'])
        c += len([x for x in s[i+1:] if x == 'E'])

        m = min(m, c)

    print(m)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())
s = list(input())

ans = 999999
for i in range(n):
    left = Counter(s[:i])
    right = Counter(s[i+1:])
    ans = min(ans, left['W'] + right['E'])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
s = list(map(lambda x: 0 if x == 'W' else 1, s))
ans = 300000
for i in range(n):
    left = s[:i]
    right = s[i+1:]
    ans = min(ans, len(left)-sum(left)+sum(right))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = input()
n = input()
nl = list(n)
ans = 1000000
for i in range(len(n)+1):
    test = []
    temp = 0
    for c in range(i):
        test.append("E")
    for c in range(len(n)-i):
        test.append("W")
    for j in range(len(n)):
        if nl[j] != test[j]:
            temp += 1
    if ans > temp:
        ans = temp
    # print(nl,test,temp)
print(ans)

Submission status: Time Limit Exceeded

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

import os
import sys

def main():
    list_length = int(input())
    result = list_length
    data = list(input())
    for i in range(list_length):
        c = l_count(i, data)
        if result > c:
            result = c
    print(result)

def l_count(i, data):
    west = data[:i].count("W")
    east = data[i:].count("E")
    return west + east

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
P = list(input())
X = []
for i in range(N):
    X.append(P[:i].count("W")+P[i+1:].count("E"))
print(min(X))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
ans = 100
for i in range(0,len(s)):
    tmp = 0
    for j in range(0,len(s)):
        if j<i and s[j]=='W':
            tmp += 1
        elif j>i and s[j]=='E':
            tmp += 1
    ans = min(ans,tmp)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
cnt = 10E6
for i in range(N):
    w = S[:i].count("W")
    e = S[i:].count("E")
    if cnt > (w+e):
        cnt = w+e
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
m = N
for i in range(N):
    S1 = S[0:i]
    S2 = S[i+1:]
    n1 = list.count(S1, 'W')
    n2 = list.count(S2, 'E')
    m = min(m, n1+n2)

print(m)

Submission status: Time Limit Exceeded

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

ans = N
for i in range(N):
    ans = min(S[0:i].count("W")+S[i+1:].count("E"), ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()

m = n

for i in range(n):

    turn = s[:i].count('W') + s[i+1:].count('E')

    m = min(turn, m)

print(m)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()


count_list = []
for index in range(1, len(s)+1):
    l = s[:index-1]
    r = s[index:]
    count = 0
    count += l.count("W")
    count += r.count("E")
    count_list.append(count)


print(min(count_list))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
tei = 10000000000000000000000000000
for i in range(1,N-1):
  a = S[:(i-1)]
  b = S[(i+1):]
  kae = a.count("W") + b.count("E")
  tei = min(tei,kae)
a = S[1:]
b = S[:(N-1)]
sai = a.count("E")
go = b.count("W")
print(min(tei,sai,go))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())

ans = 999999999
for i in range(n):
    left = s[:i]
    right = s[i+1:]

    tmp_ans = 0
    for person in left:
        if person == 'W':
            tmp_ans += 1
    for person in right:
        if person == 'E':
            tmp_ans += 1
    ans = min(ans, tmp_ans)
print(ans)

Submission status: Time Limit Exceeded

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

ans = N

for i in range(N):
    X = S[:i]
    Y = S[i+1:]
    res = 0
    
    num = X.count("W") + Y.count("E")
    
    ans = min(ans, num)
    
print(ans)

Submission status: Time Limit Exceeded

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

changes = []
for leader in range(N):
    change = 0
    for j in range(N):
        if j < leader and S[j] == 'W':
            change += 1
        if leader < j and S[j] == 'E':
            change += 1
    changes.append(change)

print(min(changes))

Submission status: Time Limit Exceeded

Solution code:

N = int(input())
S = input()

a = N


for i in range(N):
    X = S[:i]
    Y = S[i+1:]
    a = min(X.count("W")+Y.count("E"),a)
    
print(a)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
turn_sum_list = []
for i in range(N):
    turn_east = S[:i].count("W")
    turn_west = S[i:].count("E")
    turn_sum = turn_west + turn_east
    turn_sum_list.append(turn_sum)
print(min(turn_sum_list))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())

ans = float('inf')
for i in range(n):
    c = s[:i].count('W') + s[i + 1:].count('E')
    ans = min(ans, c)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()

count = len(s)

for i in range(0,len(s)):
    s1 = s[:i].count("W")
    s2 = s[i:].count("E")
    count = min(count,s1+s2)
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()

m = n

for i in range(n):
    west = s[:i]
    east = s[i+1:]

    turn = west.count('W') + east.count('E')

    m = min(turn, m)

print(m)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()

ans = s.count('W')
ans = min(ans, s.count('E'))

for i in range(n):
    test = s[:i]
    test = s[i+1:]
    ans = min(ans, s[:i].count('W') + s[i+1:].count('E'))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()

ans = n

for i in range(0, n):
  kaeru = sum(s[j] == 'W' for j in range(i)) + sum(s[j] == 'E' for j in range(i+1,n))
  
  ans = min(kaeru, ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
st = input()
l = []
for i in range(n):
    temp = st[i]
    east = [0 if i == "E" else 1 for i in st[:i]]
    west = [0 if i == "W" else 1 for i in st[i+1:]]
    l.append(sum(east) + sum(west))
print(min(l))

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
s = input()

res = float("inf")

for i in range(n):
    tmp_res = 0
    tmp = s[:i]
    tmptmp = s[i+1:]
    tmp_dict = collections.Counter(tmp)
    tmptmp_dict = collections.Counter(tmptmp)
    tmp_res += tmp_dict["W"]
    tmp_res += tmptmp_dict["E"]

    if tmp_res < res:
        res = tmp_res

print(res)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
s = []
count = 300000
for i in range(N):
    s.append(S[i])
for i in range(N):
    cnt = 0
    for j in range(i):
        if S[j] == "W":
            cnt += 1
            if count < cnt:
                cnt -= 1
                break
    for j in range(N - 1, i, -1):
        if S[j] == "E":
            cnt += 1
            if count < cnt:
                cnt -= 1
                break
    count = cnt

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
num = [S[:n].count("W") + S[n + 1:].count("E") for n in range(N)]
print(min(num))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ans = n
for i in range(1,n):
    a = s[:i-1]
    b = s[i:]
    ans = min(ans,a.count('W')+b.count('E'))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    s = input()
    w = 0
    e = 0
    r = []
    for i in range(n):
        c = 0
        ls = list(s[0:i])
        rs = list(s[i+1:])
        c += ls.count('W')
        c += rs.count('E')

        r.append(c)
    print(min(r))
main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
direction = list(input())
change = [0]*N

for i in range(N):
    for j in range(N):
        if j < i:
            if direction[j] != "E":
                change[i] += 1
        elif j > i:
            if direction[j] != "W":
                change[i] += 1

print(min(change))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())

ans = s[2:].count('E')
ans = min(ans, s[:n].count('E'))
for i in range(2, n):
  ans = min(ans, s[:i].count('W')+s[i+1:].count('E'))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(raw_input())
S = raw_input()

score_list = []
for i in range(N):
	E_num = S[i+1:].count('E')
	W_num = S[:i].count('W')
	score_list.append(E_num+W_num)

print min(score_list)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()

res = float("inf")

for i in range(n):
    tmp_res = 0
    tmp = s[:i]
    tmptmp = s[i+1:]
    tmp_res += tmp.count("W")
    tmp_res += tmptmp.count("E")

    if tmp_res < res:
        res = tmp_res

print(res)

Submission status: Time Limit Exceeded

Solution code:
N = int(raw_input())
S = list(raw_input())

ans = N
for i in range(N):
    left = S[:i].count('W')
    right = S[i:].count('E')
    ans = min(ans, left+right)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()

result = 10e6

for i in range(n):
    a = s[:i]
    b = s[i+1:]
    result = min(result, a.count('W') + b.count('E'))

print(result)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
res = 1000000
for i in range(N):
    Left = S[:i]
    Right = S[i+1:]
    res = min(res,Left.count('W')+Right.count('E'))
print(res)

Submission status: Accepted

Solution code:
import sys

N = int(input())
S = input()

W = [0]*N
E = [0]*N
ans = N
for i in range(1, N):
    W[i] = W[i-1]+1 if S[i-1]=='W' else W[i-1]
    E[-i-1] = E[-i]+1 if S[-i]=='E' else E[-i]

for i in range(N):
    ans = min(W[i]+E[i], ans)

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
S=list(input())
W=[0]*(n+1)
E=[0]*(n+1)
for i in range(n):
  W[i]=W[i-1]+S[i].count("W")
  E[n-i-1]=E[n-i]+S[n-i-1].count("E")
W = W[:n]
E = E[:n]
ans = 1000000
for i in range(n):
  ans = min(ans,W[i]+E[i]-1)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
numE = [0] * N
numW = [0] * N
for i in range(N):
    if S[i] == 'E':
        numE[i] += 1
    else:
        numW[i] += 1
    if 0 < i:
        numE[i] += numE[i - 1]
        numW[i] += numW[i - 1]
ans = N
for i in range(N):
    if i == 0:
        val = numE[-1] - numE[i]
    elif i == N - 1:
        val = numW[i - 1]
    else:
        val = numE[-1] - numE[i] + numW[i - 1]
    ans = min(ans, val)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()

li = []
for i in range(n):
    if s[i]=="W":
        li.append(-1)
    else:
        li.append(1)

cumsum = [li[0]]
for i in range(n-1):
    cumsum.append(cumsum[i] + li[i+1])

l = cumsum.index(max(cumsum))
w_to_e = 0
e_to_w = 0

for i in range(n):
    if i < l:
        if li[i]==-1:
            w_to_e += 1
    elif i > l:
        if li[i]==1:
            e_to_w += 1
print(e_to_w + w_to_e)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
ans_list = []

ans = S[1:].count("E")
ans_list.append(ans)

for i in range(1,N):
  if S[i-1] == "W":
    ans += 1
  if S[i] == "E":
    ans -= 1
  ans_list.append(ans)

print(min(ans_list))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
cnt_e = [0] * n
cnt_w = [0] * n
for i in range(n):
    if i == 0 :
        if s[i] == 'E':
            cnt_e[i] = 1
        else :
            cnt_w[i] = 1
    else :
        if s[i] == 'E':
            cnt_e[i] = cnt_e[i-1] + 1
            cnt_w[i] = cnt_w[i-1]
        else :
            cnt_e[i] = cnt_e[i-1]
            cnt_w[i] = cnt_w[i-1] + 1
ans = 3 * 10**5 + 1
for i in range(n):
    if s[i] == 'E':
        e = cnt_e[-1] - cnt_e[i]
        w = cnt_w[i]
    else :
        e = cnt_e[-1] - cnt_e[i]
        w = cnt_w[i] - 1
    ans = min(ans, e + w)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
cnt = 0
ans_list = [0]
e_cnt = 0

for i in range(1,n):
    if s[i-1] == "E" and s[i] == "E":
        cnt -= 1
        e_cnt += 1
    if s[i-1] == "E" and s[i] == "W":
        cnt -= 0
    if s[i-1] == "W" and s[i] == "E":
        cnt += 0
        e_cnt += 1
    if s[i-1] == "W" and s[i] == "W":
        cnt += 1
    ans_list.append(cnt)

print(min(ans_list) + e_cnt)

Submission status: Accepted

Solution code:
N = int(input())
N_List = str(input())
C_Ans = N_List[1:].count("E")
Ans = C_Ans
for i in range(1,N):
    C_Ans = C_Ans + (0,1)[N_List[i-1] == "W"] - (0,1)[N_List[i] == "E"]
    if C_Ans < Ans:
        Ans = C_Ans

print(Ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
e = s.count("E")
w = n - e
ans = 10**9
if s[0] == "W":
    cnt = e
else:
    cnt = e-1
num = cnt
for i in range(n-1):
    if s[i] == s[i+1] and s[i] == "E":
        cnt -= 1
    elif s[i] == s[i+1]:
        cnt += 1
    ans = min(ans,cnt,num)
    ans = max(ans,0)
print(max(ans,0))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
e = s.count("E")
w = n - e
ans = 10**9
if s[0] == "W":
    cnt = e
else:
    cnt = e-1
num = cnt
#print("Num =",num)
for i in range(n-1):
    if s[i] == s[i+1] and s[i] == "E":
        cnt -= 1
    elif s[i] == s[i+1]:
        cnt += 1
    #print(i,cnt)
    ans = min(ans,cnt,num)
    ans = max(ans,0)
print(max(ans,0))