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: Accepted

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

#累積和を求める
def get_cum_sum(s, c):
	cum_sum =[]
	cnt = 0
	
	for i in s:
		if i == c:
			cnt += 1
			cum_sum.append(cnt)
		else:
			cum_sum.append(cnt)
			
	return cum_sum
    
	

def main():
    #入力
    N = int(input())
    S = input()
		
    cum_sum_w = get_cum_sum(S, 'W')
    cum_sum_e = get_cum_sum(S, 'E')
    min_cnt = 300000
    
    for i in range(N):
    	if S[i] == 'W':
    		first_total_w = cum_sum_w[i] - 1
    	else:
    		first_total_w = cum_sum_w[i]
    		
    	last_total_e = cum_sum_e[-1] - cum_sum_e[i]
    	
    	#print(S[:i], S[i+1:], first_total_w, last_total_e)
    	
    	if min_cnt > first_total_w + last_total_e:
    		min_cnt = first_total_w + last_total_e
    
    print(min_cnt)
    	
    	
        


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
S=input()
ans=S[1:].count('E')
cnt=ans
for i in range(1,N):
    if S[i-1]=='W':
        cnt+=1
    if S[i]=='E':
        cnt-=1
    ans=min(ans,cnt)
    
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(10000)

# Debug output
def chkprint(*args):
    names = {id(v):k for k,v in inspect.currentframe().f_back.f_locals.items()}
    print(', '.join(names.get(id(arg),'???')+' = '+repr(arg) for arg in args))

# Binary converter
def to_bin(x):
    return bin(x)[2:]

# --------------------------------------------

dp = None

def main():
    N = int(input())
    S = input()

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

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

    print(min(A))



main()

Submission status: Accepted

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

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

# 累積和を使う方法
W_ruiseki = [0]
E_ruiseki = [0]
# 0地点を含めたN+1パターンを予め用意しておく
for i in range(N):
    if S[i] == "W":
        W_ruiseki.append(W_ruiseki[i] + 1)
    else:
        W_ruiseki.append(W_ruiseki[i])
    # Eは全体-Wで出せる
    E_ruiseki.append(i+1 - W_ruiseki[i+1])

ans = float("inf")
for i in range(N):
    cnt = 0
    cnt += W_ruiseki[i]
    cnt += E_ruiseki[N] - E_ruiseki[i+1]
    ans = min(ans, cnt)

print(ans)

Submission status: Accepted

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

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

# 初期値設定
cnt = S[1:].count("E")
ans = cnt
# 結局自分と1個前しか変化しない
for i in range(1, N):
    if S[i] == "E":
        cnt -= 1
    if S[i-1] == "W":
        cnt += 1
    ans = min(ans, cnt)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
 
cnt = S[1:].count("E")
ans = cnt
for i in range(1, N):
    if S[i-1] == "W":
        cnt += 1
    if S[i] == "E":
        cnt -= 1
    ans = min(ans, cnt)
 
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
S = list(input())
WL = [0]*(N+1)
for i in range(0,N):
    if S[i] == 'W':
        WL[i+1] = WL[i] + 1
    else:
        WL[i+1] = WL[i]
T = S[::-1]
ER = [0]*(N+1)
for i in range(0,N):
    if T[i] == 'E':
        ER[i+1] = ER[i] + 1
    else:
        ER[i+1] = ER[i]
ans = N
for i in range(N):
    ans = min(ans,WL[i]+ER[N-i-1])
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
S = input()

numW = [0]*N
numE = [0]*N
tmpW = 0
tmpE = 0
for i,s in enumerate(S):
    if s == 'W':
        tmpW += 1
    else:
        tmpE += 1

    numW[i] = tmpW
    numE[i] = tmpE

npans = np.zeros(N,dtype = int)
npans[0] = numE[-1] - numE[0]
for i in range(1,N):
    npans[i] = numW[i-1] + numE[-1] - numE[i]
print(np.min(npans))


# npans = np.zeros(N,dtype = int)
# for i,s in enumerate(S):
#     if s == 'W':
#         npans[i+1:] += 1
#     else:
#         npans[:i] += 1
# print(np.min(npans))

Submission status: Accepted

Solution code:
n = int(raw_input())
s = raw_input()
left = 0
right = 0
for i in range(1, n):
  if s[i] == 'E':
    right += 1
total = left + right
for i in range(0, n-1):
  if s[i] == 'W':
    left += 1
  if s[i+1] == 'E':
    right -= 1
  if left + right < total:
    total = left + right
print(total)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
minimum=3*pow(10, 5)
east=[0]
for i in range(1,N+1):
    if S[i-1]=='E':
        east.append(east[i-1]+1)
    else:
        east.append(east[i-1])
for i in range(N):
    candidate = i-east[i]+(east[N]-east[i+1])
    minimum = min(candidate, minimum)
print(minimum)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
input()
s = input()
r = s[1:].count("E")
t = r
for i in range(1,len(s)):
  t = t+1 if s[i-1] == "W" else t
  t = t-1 if s[i] == "E" else t
  r = min(r, t)
print(r)

Submission status: Accepted

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

ER=[]
WL=[]


w=0

for i in S:
    if i=='W':
        w+=1
    WL.append(w)

e=0

for i in reversed(S):
    if i=='E':
        e+=1
    ER.append(e)
    


result=N

for i in range(N):
    if i==0:
        result=min(result,ER[N-2])
    elif i==(N-1):
        result=min(result,WL[N-2])
    else:
        result=min(result,(ER[N-i-2]+WL[i-1]))
print(result)

Submission status: Accepted

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

j=s[1:].count("E")
t=[j]
for i in range(n-1):
	t.append(t[i]+s[i].count("W")-s[i+1].count("E"))
	if j>t[i+1]:
		j=t[i+1]
print(j)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
l = 0
r = S[1:].count('E')
ans = l + r
for i in range(1, N):
	if S[i - 1] == 'E':
		r -= 1
	if S[i] == 'W':
		l += 1
	ans = min(ans, l + r)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

w = [s[0].count('W')]
e = [s[0].count('E')]

for i in range(1, n):
    if s[i] == 'W':
        w.append(w[i-1]+1)
        e.append(e[i-1])
    else:
        w.append(w[i-1])
        e.append(e[i-1]+1) 

for i in range(n):
    if i > 0:
        cnt = w[i-1] + e[-1] - e[i]
    else:
        cnt = e[-1] - e[0]
    m = min(cnt, m)
print(m)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
res = n
W = [1 if s[0] == 'W' else 0] * n
E = [1 if s[0] == 'E' else 0] * n
for i in range(1,n):
    if s[i] == 'W':
        W[i] = W[i-1]+1
        E[i] = E[i-1]
    else:
        W[i] = W[i-1]
        E[i] = E[i-1] + 1
for i in range(n):
    cnt = W[i] + E[-1] - E[i]
    if s[i] == 'W':
        cnt -= 1
    res = min(cnt, res)
print(res)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
res = n
W = [0] * n
E = [0] * n 
if s[0] == 'W':
    W[0] = 1
    E[0] = 0
else:
    W[0] = 0
    E[0] = 1
for i in range(1,n):
    if s[i] == 'W':
        W[i] = W[i-1]+1
        E[i] = E[i-1]
    else:
        W[i] = W[i-1]
        E[i] = E[i-1] + 1
for i in range(n):
    cnt = W[i] + E[-1] - E[i]
    if s[i] == 'W':
        cnt -= 1
    res = min(cnt, res)
print(res)

Submission status: Accepted

Solution code:
# C attention
n = int(input())
s = input()

w = [0]*n # i までのWの数
e = [0]*n # i から n までのEの数
for i in range(n):
    if s[i] == 'W':
        w[i] = w[i-1] + 1 if i > 0 else 1
    else:
        w[i] = w[i-1] if i > 0 else 0
    
    if s[-i-1] == 'E':
        e[-i-1] = e[-i] + 1 if i < n else 1
    else:
        e[-i-1] = e[-i] if i < n else 0


ans = int(1e6)
for i in range(n):
    if i == 0: a = e[1]
    elif i == n-1: a = w[-2]
    else: a = w[i-1] + e[i+1]
    ans = min(ans, a)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

num_W = 0
num_E = 0
for i in range(1,N):
    if S[i] == "E":
        num_E += 1
min_ct = num_W + num_E

for i in range(1,N):
    if S[i-1] == "W":
        num_W += 1
    if S[i] == "E":
        num_E -= 1
    ct = num_W + num_E
    if min_ct > ct:
        min_ct = ct


print(min_ct)

Submission status: Accepted

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

def solve(N, S):
    dp = [[0 for i in range(N+1)] for j in range(N+1)]
    # dp[left][right] := 正しい方向の数
    # まずはdp[0][N]を考える
    for s in S:
        if s == "W":
            dp[0][N] += 1
    maxv = dp[0][N]
    # dpを埋める
    # dp[1][N-1], dp[2][N-2], ...
    for i in range(1, N+1):
        if S[i-1] == "E":
            diff = 1
        else:
            diff = -1
        dp[i][N-i] = dp[i-1][N-i+1] + diff
        if dp[i][N-i] >= maxv:
            maxv = dp[i][N-i]
    print(N - maxv)

def solve2(N, S):
    good = 0
    for s in S:
        if s == "W":
            good += 1
    maxv = good

    nowv = maxv
    for i in range(1, N+1):
        if S[i-1] == "E":
            diff = 1
        else:
            diff = -1
        nowv = nowv + diff
        if nowv >= maxv:
            maxv = nowv
    print(N - maxv)

if __name__ == "__main__":
    N = int(input())
    S = input()
    solve2(N, S)

Submission status: Accepted

Solution code:
N =  int(input())
S = input()
accw = [0]*(N+1)
acce = [0]*(N+1)
for i in range(N):
	if S[i] == 'E':
		acce[i+1] = acce[i]+1
		accw[i+1] = accw[i]
	else:
		accw[i+1] = accw[i]+1
		acce[i+1] = acce[i]


ans = acce[N]
for i in range(1,N-1):
	ans = min(ans,(acce[N]-acce[i+1]) + accw[i])
ans = min(accw[N-1],ans)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
s1 = S[1:]
b = []
b.append(s1.count('E'))
j = 1
while j < N:
  b.append(0)
  b[j] = b[j-1]
  if S[j] == 'E':
    b[j] += -1
  if S[j-1] == 'W':
    b[j] += 1
  j += 1
print(min(b))

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N = int(input())
    S = input()

    numE = S.count('E')
    numW = S.count('W')

    #index iの要素はiより左側にいてかつ西を向いている
    #人の人数を表す
    sumW = [0]
    for i in range(N):
        if S[i] == 'W':
            sumW.append(sumW[i] + 1)
        else:
            sumW.append(sumW[i])

    sumE = []
    for i in range(N):
        sumE.append(numE-i+sumW[i])
        if S[i] == 'E':
            sumE[i] -= 1
    att = []
    for i in range(N):
        att.append(sumW[i] + sumE[i])

    print(min(att))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
west = [0 for _ in range(n)]
if s[0] == 'W':
    west[0] = 1
for _ in range(1, n):
    if s[_] == 'W':
        west[_] = west[_-1] + 1
    else:
        west[_] = west[_-1]
east = [0 for _ in range(n)]
if s[n-1] == 'E':
    east[n-1] = 1
for _ in range(n-2, -1, -1):
    if s[_] == 'E':
        east[_] = east[_+1] + 1
    else:
        east[_] = east[_+1]
ans = [0 for _ in range(n)]
ans[0] = east[1]
for _ in range(1, n-1):
    ans[_] = west[_-1] + east[_+1]
ans[n-1] = west[n-2]

print(min(ans))

Submission status: Accepted

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

lE = [0] * N
lW = [0] * N
le = lw = 0
for i in range(N):
    if S[i] == "E":
        le += 1
    else:
        lw += 1
    lE[i] = le
    lW[i] = lw

rtn = lE[-1] - lE[0]

for i in range(1, N):
    tmp = lW[i-1] + lE[-1] - lE[i]
    if tmp <= rtn:
        rtn = tmp


print(rtn)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
left = 0
right = S[1:].count('E')
ans = left + right
for i in range(1, N):
	if S[i] == "W":
		left += 1
	if S[i - 1] == "E":
		right -= 1
	ans = min(ans, left + right)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = [x[:1] for x in input()]
ans = 0
count = 0

ruiE, ruiW = [], []
ruie,ruiw = 0, 0
for i in range(len(S)):
    if S[i] == "E":
        ruie += 1
    elif S[i] == "W":
        ruiw += 1

    ruiE.append(ruie)
    ruiW.append(ruiw)


def countW(j):
    return ruiW[j]

def countE(i):
    return ruiE[len(S) - 1] - ruiE[i-1]


#西端
ans = 0 + countE(1)

#間
for h in range(1, len(S)-1):
    count = countW(h-1) + countE(h+1)

    if count <= ans:
        ans = count


#東端
count = countW(len(S)-2) + 0

if count <= ans:
    ans = count

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = [x[:1] for x in input()]
ans = 0
count = 0

ruiE, ruiW = [], []
ruie,ruiw = 0, 0
for i in range(len(S)):
    if S[i] == "E":
        ruie += 1
    elif S[i] == "W":
        ruiw += 1

    ruiE.append(ruie)
    ruiW.append(ruiw)

#print(ruiE,ruiW)

def countW(j):
    return ruiW[j]

def countE(i):
    return ruiE[len(S) - 1] - ruiE[i-1]

#for i in range(1,5):
#    print(countW(i),countE(i))

#西端
ans = 0 + countE(1)

#間
for h in range(1, len(S)-1):
    count = countW(h-1) + countE(h+1)

    if count <= ans:
        ans = count

   # print(count,ans)

#東端
count = countW(len(S)-2) + 0

if count <= ans:
    ans = count

print(ans)

Submission status: Accepted

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

m = N
x = S.count('E')

for i in S:
    if i == 'E':
        x = x - 1

    if x < m:
        m = x

    if i == 'W':
        x = x + 1

print(m)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
left_w = 0
right_e = S[1:].count("E")
ans = left_w + right_e
for i in range(1, N):
	if S[i - 1] == "W":
		left_w += 1
	if S[i] == "E":
		right_e -= 1
	ans = min(ans, right_e + left_w)
print(ans)

Submission status: Accepted

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


dl = {}
dr = {}
acc = 0
for i, s in enumerate(S):
    dl[i] = acc
    if s == 'W':
        acc += 1

acc = 0
for i, s in enumerate(S[::-1]):
    dr[N - i - 1] = acc
    if s == 'E':
        acc += 1

result = N
for i in range(N):
    if result > dl[i] + dr[i]:
        result = dl[i] + dr[i]
print(result)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
t = sum([c == 'E' for c in s[1:]])
r = t
for i in range(1,n):
    t += (s[i-1] == 'W')
    t -= (s[i] == 'E')
    r = min(r,t)
print(r)

Submission status: Accepted

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

tmp_ans = S[1:].count("E")
ans = tmp_ans
# print(tmp_ans)
for i in range(1, N):
    if S[i-1] == "W":
        tmp_ans += 1
    if S[i] == "E":
        tmp_ans -= 1
    # print(tmp_ans)
    if tmp_ans < ans:
        ans = tmp_ans
print(ans)

Submission status: Accepted

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

ans = S[1:].count('E')
tmp = ans
for i in range(1, N):
  if S[i] == 'E':
    tmp -= 1
  if S[i-1] == 'W':
    tmp += 1
  if tmp<ans:
    ans=tmp
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
num = [0 for i in range(n)]
w = [0 for i in range(n)]
if s[0] == 'W':
    w[0] = 1
for i in range(1, n):
    if s[i] == 'W':
        w[i] = (w[i-1] + 1)
    else:
        w[i] = w[i-1]
e = [0 for i in range(n)]
if s[n-1] == 'E':
    e[n-1] = 1
for i in range(2, n+1):
    if s[n-i] == 'E':
        e[n-i] = (e[n-i+1] + 1)
    else:
        e[n-i] = e[n-i+1]
sol = [0 for i in range(n)]
sol[0] = e[0]
sol[n-1] = w[n-2]
for i in range(1, n-1):
    sol[i] = w[i-1] + e[i+1]
print(min(sol))

Submission status: Accepted

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

csum_w = [0] * n
if s[0] == 'W':
    csum_w[0] = 1
for i in range(1, n):
    cnt = csum_w[i - 1]
    if s[i] == 'W':
        cnt += 1
    csum_w[i] = cnt

ans = float('inf')
for i in range(n):
    buf = 0
    if i >0:
        buf += csum_w[i - 1]
    buf += (n - 1 - i) - (csum_w[n - 1] - csum_w[i])
    ans = min(buf, ans)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
temp = s[1:].count('E')
res = temp
for i in range(len(s)-1):
    if s[i+1] == "E":
        temp-=1
    if s[i] == "W":
        temp+=1
    res = min(res,temp)
    
print(res)

Submission status: Accepted

Solution code:
input()
s=input()
temp=s[1:].count("E")
res=temp
for i in range(len(s)-1):
    if s[i+1]=="E":
        temp-=1
    if s[i]=="W":
        temp+=1
    res=min(res,temp)
print(res)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
cntW = 0
cntE = s[1:].count("E")
ans = cntE
for i in range(1, n):
  if s[i-1] == "W":
    cntW+=1
  if s[i]=="E":
    cntE-=1
  ans=min(ans, cntE+cntW)
print(ans)

Submission status: Accepted

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

w_count = [0]
e_count = [0]

a_r = list(reversed(a))

for i in range(1, n+1):
    if a[i-1] == 'W':
        w_count.append(w_count[i-1] + 1)
    else:
        w_count.append(w_count[i-1])
    if a_r[i-1] == 'E':
        e_count.append(e_count[i-1] + 1)
    else:
        e_count.append(e_count[i-1])

w = w_count[1:]
e = list(reversed(e_count[1:]))

sum = 10000000
for i in range(0, n):
    if sum > w[i] + e[i]:
        sum = w[i] + e[i]

print(sum-1)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
e=[0]
w=[0]
for i, si in enumerate(s):
    if si=='W':
        w.append(w[i]+1)
    else:
        w.append(w[i])
for i, si in enumerate(reversed(s)):
    if si=='E':
        e.append(e[i]+1)        
    else:
        e.append(e[i])

ans=float('inf')
for i in range(n):
    ans=min(ans, w[i]+e[n-i-1])
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate

n, s = input(), input()

a = accumulate(c=='W' for c in 'X'+s[:-1])
b = reversed(list(accumulate(c=='E' for c in 'X'+s[-1:0:-1])))
print(min(x + y for x, y in zip(a, b)))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
ans = [0]
ans[0] = S[1:].count('E')
for i in range(N-1):
    if S[i] == 'E' and S[i+1] == 'E':
        ans.append(ans[i] -1)
    if S[i] == 'E' and S[i+1] == 'W':
        ans.append(ans[i])
    if S[i] == 'W' and S[i+1] == 'E':
        ans.append(ans[i] )
    if S[i] == 'W' and S[i+1] == 'W':
        ans.append(ans[i] +1)
print(min(ans))

Submission status: Accepted

Solution code:
input()
s = input()

#west = 西 = 左
#east = 東 = 右

result = 3*10**5

Wcount = 0  #左側で左を向いてる人をカウント
Ecount = s.count("E") #右側で右を向いてる人をカウント

for n,i in enumerate(s):
    Wflag = False
    if i == "W":
        Wflag = True
    else:
        Ecount -= 1
    if result > Wcount+Ecount:
        result = Wcount+Ecount
    if Wflag:
        Wcount += 1

print(result)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
C - Attention

"""
import sys
from sys import stdin
input = stdin.readline


def solve(N, S):
    ans = float('inf')
    left_w = -1
    left_e = 0
    right_w = S.count('W')
    right_e = S.count('E')

    prev_ch = 'W'
    for ch in S:
        if prev_ch == 'W':
            left_w += 1
        else:
            left_e += 1
        if ch == 'W':
            right_w -= 1
        else:
            left_w -= 1
        prev_ch = ch
        c = left_w + right_e
        ans = min(ans, c)
    return ans


def main(args):
    N = int(input())
    S = input().strip()
    ans = solve(N, S)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

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

Submission status: Accepted

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

min = -1

for i in range(n):
    if i == 0:
        turn = s[i+1:n].count("E")
    else:
        if s[i-1] == "W" and s[i] == "W":
            turn += 1
        elif s[i-1] == "W" and s[i] == "E":
            next
        elif s[i-1] =="E" and s[i] =="W":
            next
        else:
            turn -=1
    if min == -1 or min >= turn:
        min = turn
        
    

print(min)

Submission status: Accepted

Solution code:
N = int(input())
word = input().strip()

Wcnt = [0 for _ in range(N+2)]
for i in range(1, N+1):
    Wcnt[i] = Wcnt[i-1]
    if word[i-1] == 'W':
        Wcnt[i] += 1

Ecnt = [0 for _ in range(N+2)]
for i in range(N, 0, -1):
    Ecnt[i] = Ecnt[i+1]
    if word[i-1] == 'E':
        Ecnt[i] += 1


print(min([Wcnt[i-1]+Ecnt[i+1] for i in range(1,N+1)]))

Submission status: Accepted

Solution code:
n = int(input())

s = list(raw_input())

cs = []
for i in range(n):
	if s[i] == 'E':
		s[i] = 1
	else :
		s[i] = 0

cs.append(s[0])

for i in range(1,n):
	cs.append(cs[i-1]+s[i])

ans = cs[n-1]-cs[0]
for i in range(1,n):
	ans = min( cs[n-1]-cs[i] + i-cs[i-1],ans)

print ans

Submission status: Accepted

Solution code:
E = "E"  # ->
W = "W"  # <-

n = int(input().rstrip())

line = input().rstrip()

bef_change_count = line[1:].count(E)
min_change_count = bef_change_count

for leader_i in range(1, n):
    change_count = bef_change_count
    if line[leader_i] == E and line[leader_i - 1] == E:
        change_count -= 1
    elif line[leader_i] == W and line[leader_i - 1] == W:
        change_count += 1

    bef_change_count = change_count
    min_change_count = min(change_count, min_change_count)

print(min_change_count)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
ans = N
E = [0] * (N + 1)
for i in range(N):
    E[i+1] = E[i] + (S[i] == 'E')
for i in range(N):
    LR = i - E[i] + E[N] - E[i+1]
    if LR < ans: ans = LR
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
count_east=[0]*n
count_west=[0]*n
counts=[0]*n

for i in range(n-2,-1,-1):
    if s[i+1]=='E':
        count_east[i]=count_east[i+1]+1
    else:
        count_east[i]=count_east[i+1]
for i in range(1,n):
    if s[i-1]=='W':
        count_west[i]=count_west[i-1]+1
    else:
        count_west[i]=count_west[i-1]
for i in range(n):
    counts[i]=count_east[i]+count_west[i]

print(min(counts))

Submission status: Accepted

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

cntW, cntE = 0, S[1:].count('E')
ans = cntW + cntE
 
for i in range(N-1):
    if S[i+1] == 'E':
        cntE = cntE - 1 
    if S[i] == 'W':
        cntW = cntW + 1
    cnt = cntE + cntW
    ans = min(ans, cnt)

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N=int(input())
s=input()
west=[]
east=[]
for i in range(N):
    west.append(0)
    east.append(0)
    if(s[i]=='W'):
        west[i]=1
    else:
        east[i]=1
eastsum=np.zeros(N)
westsum=np.zeros(N)
westsum[0]=west[0]
eastsum[N-1]=east[N-1]
for i in range(1,N):
    eastsum[N-1-i]=eastsum[N-i]+east[N-1-i]
    westsum[i]=westsum[i-1]+west[i]
print(int(np.min(eastsum+westsum))-1)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
l = 0
r = s[1:].count('E')
result = l + r
for i in range(1, n):
  if s[i - 1] == "W":
    l += 1
  if s[i] == "E":
    r -= 1
  result = min(result, l + r)
print(result)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

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

west = [0]*n
east = [0]*n

cnt = 0
for i in range(1,n):
    cnt = cnt + 1 if s[i-1] == 'W' else cnt
    west[i] = cnt

cnt = 0
for i in range(n-2, -1, -1):
    cnt = cnt + 1 if s[i+1] == 'E' else cnt
    east[i] = cnt

cntMin = n
for i in range(0,n):
    cntMin = min(cntMin, west[i]+east[i])
    #print("cntMin:{0} \t west:{1} \t east:{2}".format(cntMin, west[i], east[i]))
    
print(cntMin)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
minChange = n
sumE = s.count('E')

w = [0 for i in range(n)]
e = [0 for i in range(n)]

for i in range(n):
    if s[i] == 'W':
        if i == 0:
            w[i] = 1
            e[i] = 0
        else:
            w[i] = w[i - 1] + 1
            e[i] = e[i - 1]
    else:
        if i == 0:
            e[i] = 1
            w[i] = 0
        else:
            e[i] = e[i - 1] + 1
            w[i] = w[i - 1]

for i in range(n):
    if s[i] == 'W':
        acW = w[i] - 1
    else:
        acW = w[i]
    racE = sumE - e[i]
    minChange = min(minChange, acW + racE)

print(minChange)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
P = [0]

for k in s:
    P.append(P[-1] + (k=="W"))

ans = n

for i in range(n):
    temp = (n-i-1) - (P[n] - P[i+1])
    temp += P[i]
    ans = min(ans,temp)
    
print(ans)

Submission status: Accepted

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

tmp = 0
for i in range(1, n):
    if s[i] == "E":
        tmp += 1
count = tmp

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

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
count = 0
for i in  range(1,n):
    if s[i] =="E":
        count+=1
ans = count
for i in range(1,n):
    if s[i-1] == "W":
        count += 1
    if s[i] == "E":
        count -= 1
    if ans > count:
        ans = count
print(ans)

Submission status: Accepted

Solution code:
N=int(input().strip())
S=input().strip()
w_num=[0]*N
e_num=[0]*N
for i in range(1,N):
    j=N-i-1
    w_num[i] = w_num[i-1] + int(S[i-1]=="W")
    e_num[j] = e_num[j+1] + int(S[j+1]=='E')
total =[i+j for i, j in zip(w_num,e_num)]
print(min(total))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#https://qiita.com/minemine0505/items/9aeae0f651f19f2ac1e2
N = int(input())
S = input()
min = S[1:N].count("E") #最小値を保存しておく
now = S[1:N].count("E") #i-1 を見ているときのそっぽ向いてる子の数

for i in range (1,N):
    if S[i-1] == "E" and S[i] == "E":
        now -= 1
    if S[i-1] == "W" and S[i] == "W":
        now += 1
    if now < min:
        min = now
print(min)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
 
c = 0
for i in range(1,n):
  if s[i] == "E":
    c+=1
 
min = c
 
 
for i in range(n-1):
  if s[i] == s[i+1]:
    if s[i] == "W":
      c+=1
    else:
      c-=1
  if c < min:
    min = c
 
print(min)

Submission status: Accepted

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

# import random
# N = 3 * 10 ** 5
# S = "".join(["WE"[random.randint(0, 1)] for _ in range(N)])

to_east = 0
to_west = S[1:].count("E")

min_turn = to_east + to_west
for i in range(1, N):
    if min_turn == 0:
        break
    if S[i - 1] == "W":
        to_east += 1
    if S[i] == "E":
        to_west -= 1
    min_turn = min(to_east + to_west, min_turn)

print(min_turn)

Submission status: Accepted

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

reS = S[::-1]

wSum = [0 for i in range(N)]
eSum = [0 for i in range(N)]

for i in range(N):
    if i == 0:
        if S[i] == 'W':
            wSum[i] = 1
        if reS[i] == 'E':
            eSum[i] = 1
    else:
        if S[i] == 'W':
            wSum[i] = wSum[i-1] + 1
        else:
            wSum[i] = wSum[i-1]
        if reS[i] == 'E':
            eSum[i] = eSum[i-1] + 1
        else:
            eSum[i] = eSum[i-1]

eSum = eSum[::-1]

cost = eSum[1]

for i in range(1,N):
    if i == N - 1:
        if cost > wSum[N-2]:
            cost = wSum[N-2]
    else:
        if cost > wSum[i-1] + eSum[i+1]:
            cost = wSum[i-1] + eSum[i+1]
print(cost)

Submission status: Accepted

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

reS = S[::-1]

wSum = [0 for i in range(N)]
eSum = [0 for i in range(N)]

for i in range(N):
    if i == 0:
        if S[i] == 'W':
            wSum[i] = 1
        if reS[i] == 'E':
            eSum[i] = 1
    else:
        if S[i] == 'W':
            wSum[i] = wSum[i-1] + 1
        else:
            wSum[i] = wSum[i-1]
        if reS[i] == 'E':
            eSum[i] = eSum[i-1] + 1
        else:
            eSum[i] = eSum[i-1]

eSum = eSum[::-1]

cost = eSum[1]

for i in range(1,N):
    if i == N - 1:
        if cost > wSum[N-2]:
            cost = wSum[N-2]
    else:
        if cost > wSum[i-1] + eSum[i+1]:
            cost = wSum[i-1] + eSum[i+1]
print(cost)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
#リーダーより左のWの個数+右のEの個数の最小化
total_W = S.count('W')
total_E = N-total_W
temp_West_W = 0
temp_East_E = total_E - S[0].count('E')
temp = temp_West_W + temp_East_E
for i in range(N-1):
    temp_West_W = temp_West_W + S[i].count('W')
    temp_East_E = temp_East_E - S[i+1].count('E')
    if temp > temp_West_W + temp_East_E:
        temp=temp_West_W + temp_East_E
print(temp)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
minv = 0
v = 0
minp = 0
i = 0
while i < n:
    if s[i] == 'W':
        v += 1
    else:
        v -= 1
    if v < minv:
        minv = v
        minp = i
    i += 1
ans = (minv - (-minp-1))/2 + ((n-minp-1) - (v - minv))/2
print(str(int(ans)))

Submission status: Accepted

Solution code:
n = input()
l = list(raw_input())
left = 0
right = l.count("E")
count = []

for i in range(0, n):
	if l[i] == "E":
		right -= 1

	count += [left + right]

	if l[i] == "W":
		left += 1

print min(count)

Submission status: Accepted

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

min = 3*10**5
cnt = 0

cnt = S.count("E")
min = cnt

for i in S:
  if i == "E":
    cnt -= 1
  elif i == "W":
    cnt += 1
  if min >= cnt:
    min = cnt

print(min)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
cumE = [0 for i in range(N+1)]

for i in range(N):
    if S[i] == "E":
        cumE[i+1] = cumE[i] + 1
    else:
        cumE[i+1] = cumE[i]

cumW = [i - cumE[i] for i in range(N)]
cumE2 = [cumE[N] - cumE[i+1] for i in range(N)]
print(min([cumW[i] + cumE2[i] for i in range(N)]))

Submission status: Accepted

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

w=[0]*n
i=1
while i<n:
    if s[i-1]=='W':
        w[i-1]+=1
    w[i]+=w[i-1]
    i+=1
i=n-1
e=[0]*n
while i>0:
    if s[i]=='E':
        e[i]+=1
    e[i-1]+=e[i]
    i-=1
    
ans=10**9

i=0
while i<n:
    ans=min(ans,e[i]+w[i]-1)
    i+=1
print(ans)