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:
n = int(input())
s = input()

ms = n
a = 0
b = s.count('E')
for i in range(n):
	if(i != 0 and s[i-1] == 'W'):
		a += 1
	if(s[i] == 'E'):
		b -= 1
	# print(s[0:i], s[i+1:n], s[0:i].count('W'), s[i+1:n].count('E'), a, b)
	# c = s[0:i].count('W')
	# c += s[i+1:n].count('E')
	c = a + b
	if(ms > c):
		ms = c

print(ms)

Submission status: Accepted

Solution code:
import math
import numpy as np

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

nW = [0 for i in range(N)] #iまでのWの数
nE = [0 for i in range(N)] #iまでのEの数

if S[0] == 'W':
    nW[0] = 1
else:
    nE[0] = 1

for i in range(1, N):
    if S[i] == 'W':
        nW[i] = nW[i-1] + 1
        nE[i] = nE[i-1]
    else:
        nW[i] = nW[i-1]
        nE[i] = nE[i-1] + 1

if nE[N-1] - nE[0] < nW[N-1]:
    count = nE[N-1] - nE[0]
else:
    count = nW[N-1]

for i in range(1,N-1):
    if nW[i-1] + nE[N-1] - nE[i] < count:
        count = nW[i-1] + nE[N-1] - nE[i]

print(count)

Submission status: Accepted

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

e = [0 for _ in range(n)]
w = [0 for _ in range(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]

res = [0 for _ in range(n)]

for i in range(n):
    res[i] = w[i] + e[i]

print(min(res))

Submission status: Accepted

Solution code:
# 解法：累積和
N = int(input())
S = input()
pos = 0
minpos = 0
mini = 0
# (1)WWWWEE
# (2)EEEWWW
# (3)WEWEEE
for i in range(N):
    if S[i] == "W":
        pos += 1
    else:
        pos -= 1
    if pos < minpos:
        minpos = pos
        mini = i

s1 = S[0:mini].count("W")
s2 = S[mini + 1:N].count("E")
print(s1 + s2)

Submission status: Accepted

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

wt = S.count("W")
et = S.count("E")
w = 0
e = 0
    
for i in S:
    if i == "W":
        w += 1
        times.append(w-1+et-e)
    else:
        e += 1
        times.append(w+et-e)
print(min(times))

Submission status: Accepted

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

count_l = [0]
count_r = [0]


for i in range(N):
    if S[i] == 'W':
        tmp = count_l[i] + 1
    else:
        tmp = count_l[i]
    count_l.append(tmp)
    if S[N-1-i] == 'E':
        tmp = count_r[i] + 1
    else:
        tmp = count_r[i]
    count_r.append(tmp)

count_r.reverse()

Ans = count_l

for i in range(N+1):
    Ans[i] = count_r[i] + count_l[i]

print(min(Ans))

Submission status: Accepted

Solution code:
if __name__ == '__main__':
	N = int(input())
	S = input()
	w = [0 for _ in range(N)]
	e = [0 for _ in range(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]

	res = [0 for _ in range(N)]
	for i in range(N):
		res[i] = w[i] + e[i]

	print(min(res))

Submission status: Accepted

Solution code:
if __name__ == '__main__':
	N = int(input())
	S = input()
	w = [0 for _ in range(N)]
	e = [0 for _ in range(N)]

#	if S[0] == 'W':
#		w[0] = 1

	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]


	res = [0 for _ in range(N)]
	for i in range(N):
		res[i] = w[i] + e[i]

	print(min(res))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

e = [0 for i in range(n)]
w = [0 for i in range(n)]
for i in range(0, n - 1):
    et = e[i - 1] if i != 0 else 0
    et += 1 if s[i] != "E" else 0
    e[i] = et
    wt = w[(n - 1 - i) + 1] if i != 0 else 0
    wt += 1 if s[n - 1 - i] != "W" else 0
    w[n - 1 - i] = wt
# import pdb; pdb.set_trace()
tmin = 0
for i in range(0, n):
    t = 0
    t += e[i - 1] if i != 0 else 0
    t += w[i + 1] if i != (n - 1) else 0
    if t < tmin or i == 0:
        tmin = t 
print(tmin)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
flag=1
result=S[1:].count("E")
res=result
for i in range(len(S)-1):
    if(S[i] == S[i+1]):
        if(S[i]=="E"):
            res-=1

            result=min(res,result)
        else:
            res+=1
            result=min(res,result)
print(result)

Submission status: Accepted

Solution code:
n = int(raw_input())
q = raw_input()

etow = []
wtoe = []

nowe = 0
noew = 0

for a in range(n):
  etow.append(noew)
  wtoe.append(nowe)
  if q[a] == "W":
    nowe += 1
  if q[n - 1 - a] == "E":
    noew += 1

wtoe = wtoe[::-1]
mi = 999999999

#print etow
#print wtoe

for a in range(n):
  #print etow[a] , wtoe[a]
  if etow[a] + wtoe[a] < mi:
    mi = etow[a] + wtoe[a]
print mi

Submission status: Accepted

Solution code:

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

    right_e = len([x for x in s if x == 'E'])
    left_w = 0

    m = right_e - 1 if s[0] == 'E' else right_e
    
    for i in range(1, len(s)):
        if s[i-1] == 'E':
            right_e -= 1
        else:
            left_w += 1

        if s[i] == 'E':
            m = min(m, right_e - 1 + left_w)
        else:
            m = min(m, right_e + left_w)


    print(m)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

w = [0] * (n + 1)
e = [0] * (n + 1)
bfW = 0
bfE = 0
for i in range(n+1):
    w[i] = bfW
    e[i] = bfE
    if s[i] == 'W':
        bfW += 1
    elif s[i] == 'E':
        bfE += 1

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

Submission status: Accepted

Solution code:
def main():
    """
    2 <= N <= 3 * 10^5
    |S| = N
    Si = {E, W}
    """
    N = int(input())
    S = input()

    f(N, S)


def f(N, S):
    """
    leaderにする人の東西について
    どちらを向いているか分かればよい
    """
    ans = N
    # 自分を含めて、自分より西にいる人が西を向いている人数
    w_cumsum = [0] * N
    w_cumsum[0] = int(S[0] == "W")
    for i in range(1, N):
        w = int(S[i] == "W")
        w_cumsum[i] = w_cumsum[i-1] + w

    # print(w_cumsum)
    for i in range(N):
        # 対象の外向き
        # 対象より(西and西を向いている人数)+(東and東を向いている人数)
        diff = w_cumsum[i] - int(S[i] == "W")
        diff += (N - (i+1)) - (w_cumsum[-1] - w_cumsum[i])
        ans = min(ans, diff)
        #same = N - 1 - diff
        #print(i, diff, same, ans)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from sys import stdin
import collections

N = int(stdin.readline().rstrip())
S = stdin.readline().rstrip()

people = []

for i in range(N):
    people.append(S[i])

tmp = collections.Counter(people[1:])
num_return = tmp['E']
minv = num_return

for i in range(1, N):
    if people[i] == 'E':
        num_return -= 1
    if people[i-1] == 'W':
        num_return += 1
    if num_return < minv:
        minv = num_return

print(minv)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
s = list(map(lambda x: 0 if x == 'W' else 1, s))
ans = 300000
beright = [0 for i in range(n)]
beleft = [0 for i in range(n)]
for i in range(1, n):
    beright[i] = beright[i-1] + abs(s[i-1] - 1)
    beleft[-i-1] = beleft[-i] + s[-i]

for br, bl in zip(beright, beleft):
    ans = min(ans, br + bl)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
ans = s.count('E')
r = 10**6
if s[0]=='E':
    ans -= 1

r = min(ans,r)

for i in range(1,n):
    if s[i-1]=='W' and s[i]=='W':
        ans += 1
    elif s[i-1]=='E' and s[i]=='E':
        ans -= 1
    r = min(ans,r)
print(r)

Submission status: Accepted

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

D = Counter(s[1:])["E"]
nums = [D]
for i in range (1,N):
    temp = nums[-1]
    if s[i-1] == "W":
        temp += 1
    if s[i] == "E":
        temp -= 1
    nums.append(temp)

print(min(nums))

Submission status: Accepted

Solution code:
# coding: utf-8

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

for (i, x) in enumerate(s):
    if x == "E":
        s[i] = 1
    elif x == "W":
        s[i] = 0

left = 0
right = sum(s[1:n])
center = s[0]
min_changed_num = right

for i in range(1, n):
    left = left + (1 - center)
    center = s[i]
    right = right - center
    changed_num = left + right
    if min_changed_num > changed_num:
        min_changed_num = changed_num

print(min_changed_num)

Submission status: Accepted

Solution code:
N = int(input())
s = input()
# 左側のWの数と右側のEの数を数える (和が小さいのが答え)
# min = 3 * 10**5
# 毎回list_aとかの中身を計算させているのが良くない
# (一回計算したらわかる話)
list_a_cnt = 0
list_b_cnt = 0
list_a = list(s[0:0])
#print("list_a",list_a)
list_b = list(s[0:])
#print("list_b",list_b)
list_b_cnt = list_b.count('E')
min = list_a_cnt + list_b_cnt
for i in range(N):
    if s[i] == "W":
        list_a_cnt += 1
    elif s[i] == "E":
        list_b_cnt -= 1
    #print(s[0:i],s[i:])
    #print(list_a_cnt,list_b_cnt)
    if list_a_cnt + list_b_cnt < min:
        min = list_a_cnt + list_b_cnt
print(min)

Submission status: Accepted

Solution code:
def main(num, string):
	e = string.count("E")
	w = num-e
	handol = e 
	minval = e 
	for s in string:
		if s =="E":
			handol -= 1
		else:
			handol += 1
		minval = min(minval, handol)
	return minval
	
	

num = int(input())
string = input()

print(main(num, string))

Submission status: Accepted

Solution code:
import sys

N = int(input())
S = input()
cw = [] # count W
cw.append(0)
for i, s in enumerate(S):
    i += 1
    if s == "W":
        cw.append(cw[i-1]+1)
    else:
        cw.append(cw[i-1])
#print(cw)

ans = 100000*3
for i in range(N):
    left = cw[i] # num of W
    right = (N-i-1)-(cw[N]-cw[i+1]) # num of E
    #print("left: %d, rw: %d, right: %d, %s, %s" % (left,cw[N]-cw[i+1],right,S[0:i],S[i+1:N]))
    tmp = left+right
    if ans>tmp:
        ans = tmp
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
x=len([s for s in S[1:] if s=='E'])
x_min=x

for i in range(1,N):
	if S[i-1]=='W':
		x+=1
	if S[i]=='E':
		x-=1
	if x<x_min:
		x_min=x

print(x_min)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
s = input()
r = dict(Counter(s))
if 'W' not in r:
    r['W'] = 0
if 'E' not in r:
    r['E'] = 0
l = {'W': 0, 'E': 0}
a = []
for c in s:
    l[c] += 1
    r[c] -= 1
    l[c] -= 1
    a.append(l['W'] + r['E'])
    l[c] += 1
print(min(a))

Submission status: Accepted

Solution code:
import bisect

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

changes = []

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

for leader in range(N):
    change = 0
    change += bisect.bisect_left(wests, leader)
    change += len(easts) - bisect.bisect_right(easts, leader)
    changes.append(change)

print(min(changes))

Submission status: Accepted

Solution code:
n = input()
n = input()
nl = list(n)
w = [0 for i in range(len(n))]
e = [0 for i in range(len(n))]
w[0] = 1 if nl[0] == "W" else 0
e[0] = 1 if nl[0] == "E" else 0
for i in range(len(n)-1):
    w[i+1] = w[i] + 1 if nl[i+1] == "W" else w[i]
    e[i+1] = e[i] + 1 if nl[i+1] == "E" else e[i]
wr = [0 for i in range(len(n))]
er = [0 for i in range(len(n))]
ans = [0 for i in range(len(n))]
for i in range(len(n)):
    wr[i] = w[len(n)-1] - w[i]
    er[i] = e[len(n)-1] - e[i]
for i in range(len(n)):
    temp = w[i] - 1 if nl[i] == "W" else w[i]
    ans[i] = temp + er[i]
# print(w)
# print(e)
# print(wr)
# print(er)
# print(ans)
print(min(ans))

Submission status: Accepted

Solution code:
N = int(input())

S = input()

result = 0
E_ = 0
W_ = 0
for i in range(1, N):
    if S[i] == "E":
        E_ += 1

result = E_ + W_

for i in range(1, N):
    if S[i] == "E":
        E_ -= 1

    if S[i - 1] == "W":
        W_ += 1

    result = min(result, E_ + W_)


print(result)

Submission status: Accepted

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

for i,s in enumerate(S):
    if i == 0:
        cnt_MIN = S[1:].count("E") #Westを向かなければならない人の数
        cnt = cnt_MIN
    else:
        if S[i-1] == "W":
            cnt += 1
        if S[i] == "E": 
            cnt -= 1
    if cnt < cnt_MIN:
        cnt_MIN = cnt
print(cnt_MIN)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
S = input()
c = Counter(S[1:])
E_count = c['E']
min_count = E_count

W_left = 0
E_right = E_count
for i in range(1, N):
    if S[i - 1] == 'W':
        W_left += 1
    if S[i] == 'E':
        E_right -= 1
    if W_left + E_right < min_count:
        min_count = W_left + E_right
print(min_count)

Submission status: Accepted

Solution code:
class dr_count(object):
    def __init__(self):
        self.w = 0
        self.e = 0


N = int(input())
S = input()
count = []
minn = 300000 
for x in range(0,N):  
    count.append(dr_count())
    if x == 0:
        if S[x] == 'E':
            count[x].e += 1
    else:
        if S[x] == 'E':
            count[x].e = count[x-1].e + 1
        else:
            count[x].e = count[x-1].e
a = N-1
while a>=0:
    if a == N-1:
        if S[a] == 'W':
            count[a].w += 1
    else:
        if S[a] == 'W':
            count[a].w = count[a+1].w + 1
        else:
            count[a].w = count[a+1].w
    a -= 1
for x in range(0,N):
    if x == 0:
        num = N - x - 1 - count[x+1].w
    elif x == (N-1):
        num = x - count[x-1].e
    else:
        num = N - x - 1 - count[x+1].w + x - count[x-1].e
    minn = min(num,minn)
print(minn)

Submission status: Accepted

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

    # S = "WWWWWEEE"
    # N = len(S)

    min_total_turns = N

    left_turns = [0 for _ in range(N)]
    for i in range(N - 1):
        left_turns[i + 1] = left_turns[i]
        if S[i] == "W":
            left_turns[i + 1] += 1

    right_turns = [0 for _ in range(N)]
    for i in range(N - 1, -1, -1):
        if i != 0:
            right_turns[i - 1] = right_turns[i]
            if S[i] == "E":
                right_turns[i - 1] += 1

        min_total_turns = min(min_total_turns, left_turns[i] + right_turns[i])

    # print(left_turns)
    # print(right_turns)
    print(min_total_turns)

Submission status: Accepted

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

nE = 0
n = 0
minn = 0
for a in range(1, N):
	wE = (1 if S[a] == 'E' else 0)
	n = n + (1 if S[a-1] == 'W' else 0) - wE
	minn = min(minn, n)
	nE = nE + wE
print(minn + nE)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
right = S[1:].count('E')
left = 0
answer = right
_reader = S[0]

for i in range(1, N):

    if _reader == "W":
        left += 1

    _reader = S[i]
    
    if _reader == "E":
        right -=1

    t = left + right
    answer = answer if answer <= t else t
        

print(answer)

Submission status: Accepted

Solution code:

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

#print(S)
#print(S[1:])
list1=[]

ST=S[1:].count("E")
list1.append(ST)
#print(list1)

for i in range(1,N):
    if S[i]=="E":
        ST=ST-1
    if S[i-1]=="W":
        ST=ST+1
    list1.append(ST)
#print(list1)
print(min(list1))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat May 26 21:19:35 2018

@author: maezawa
"""

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

dp = [None for _ in range(n)]

cnt = 0
for i in range(1,n):
    if s[i]=='E':
        cnt += 1
dp[0] = cnt
#print(dp[0])

for i in range(1,n):
    if s[i-1] == 'E':
        if s[i] == 'E':
            dp[i] = dp[i-1] - 1
        else:
            dp[i] = dp[i-1]
    elif s[i-1] == 'W':
        if s[i] == 'W':
            dp[i] = dp[i-1] + 1
        else:
            dp[i] = dp[i-1]

ans = min(dp)
print(ans)

Submission status: Accepted

Solution code:
import collections

n = int(input())
s = input()
s_w = s.count("W")
s_e = s.count("E")

w_array = [0 for x in range(n)]
e_array = [0 for x in range(n)]
w_tmp = 0
e_tmp = 0

for i in range(n):
    if s[i] == "W":
        w_tmp += 1
    else:
        e_tmp += 1
    w_array[i] += w_tmp
    e_array[i] += s_e - e_tmp

res = min(s[1:].count("E"), s[:-1].count("W"))

for count in range(1, n-1):
    if w_array[count]+e_array[count+1] < res:
        res = w_array[count]+e_array[count]

print(res)

Submission status: Accepted

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

lst1 = [0]
c = 0
for i in range(1,len(s)):
    if s[i-1] == "W":
        c += 1
    lst1.append(c)
    
lst2 = [0]
c = 0
for i in range(len(s)-2,-1,-1):
    if s[i+1] == "E":
        c += 1 
    lst2.append(c)
#print(lst1,lst2)

lst = []
for i in range(len(lst1)):
    lst.append(lst1[i] + lst2[n-i-1])

lst.sort()
#print(lst)
print(lst[0])

Submission status: Accepted

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

E_list = [0] * N
W_list = [0] * N

# print(S)

E_count = 0
for i in range(N):
    if i != 0:
        if S[i-1] == 'W':
            E_count = 1 + E_count
        E_list[i] = E_count

S.reverse()

W_count = 0
for i in range(N):
    if i != 0:
        if S[i-1] == "E":
            W_count += 1
        W_list[i] = W_count

W_list.reverse()

# print(E_list)
# print(W_list)

lll = [W_list[i] + E_list[i] for i in range(N)]
print(min(lll))

Submission status: Accepted

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

print(min(a))

Submission status: Accepted

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

ans = 3 * 10**7
w = [0]
e = [0]

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

for i, a in enumerate(s[::-1]):
    if a == 'E':
        e.append(e[-1]+1)
    else:
        e.append(e[-1])

e.reverse()

for i in range(n):
    ans = min(ans, w[i]+e[i+1])

print(ans)

Submission status: Accepted

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

print(m)

Submission status: Accepted

Solution code:
#!/Library/Frameworks/Python.framework/Versions/3.5/bin/python3

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

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

totalmin = float('inf')
for i in range(len(s) - 1, -1, -1):
    if i == len(s) - 1:
        e[i] = 0
    else:
        if s[i + 1] == 'E':
            e[i] = e[i + 1] + 1
        else:
            e[i] = e[i + 1]
    total = e[i] + w[i]
    totalmin = min(totalmin, total)

print(totalmin)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
w_count = 0
e_count = 0
count_sum = [0 for k in range(N)]
for i in range(1,N):
    if S[i - 1] == "W":
        w_count += 1
    count_sum[i] += w_count
for q in range(N-2,-1,-1):
    if S[q + 1] == "E":
        e_count += 1
    count_sum[q] += e_count
print(min(count_sum))

Submission status: Accepted

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

max_num = S[1:].count('W')
pre_num = max_num

for i in range(1, N):
    num = pre_num
    if S[i-1] == 'E':
        num += 1
    if S[i] == "W":
        num -= 1

    if num > max_num:
        max_num = num
    pre_num = num

print(N - max_num - 1)

Submission status: Accepted

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


score = s[1:].count("E")
result = score

for i in range(1,n):
    if(s[i]=="E"):
        score -= 1
    if(s[i-1]=="W"):
        score += 1
    
    if(score < result):
        result = score

print(result)

Submission status: Accepted

Solution code:
def m():
    n = int(input())
    s = str(input())
    w = 0
    e = 0
    for i in range(n):
        if s[i] == 'W':
            w +=1
        elif s[i] == 'E':
            e +=1

    ans = 99999999999
    lw = 0
    for j in range(n):
        if s[j] == 'W':
            w -= 1
            ans = min(e+lw, ans)
            lw +=1
        if s[j] == 'E':
            e -= 1
            ans = min(e+lw, ans)
    return ans

print(m())

Submission status: Accepted

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

num = S[1:].count('E')
score_list = []
score_list.append(num)

for i in range(N-1):
	if S[i]=='W':
		num+=1
	if S[i+1]=='E':
		num-=1
	score_list.append(num)
	
print min(score_list)

Submission status: Accepted

Solution code:
n = int(input())
l = list(str(input()))
m = []
W_all = 0
for i in l:
    if i == 'W':
        W_all += 1
    else:
        continue

W_left = 0
ans = 300000

for i in range(len(l)):
    if l[i] == 'W':
        W_left += 1    
    E_count = n - (i + 1) - (W_all-W_left)


    if l[i] == 'W':
        ans = min(ans, W_left -1 + E_count)
    else:
        ans =min(ans, W_left + E_count)
print(ans)

Submission status: Accepted

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

sum = 0
for i in range(1,len(S)):
    if S[i] == 'E':
        sum += 1
min = sum

for i in range(1,len(S)):
    if S[i] == 'E':
        sum -= 1
    if S[i-1] == 'W':
        sum += 1
    if min > sum:
        min = sum
print(min)

Submission status: Accepted

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

w_l = []
e_l = []

t = 0
for si in s:
    if si == 'W':
        t += 1
        w_l.append(t)
    else:
        w_l.append(t)

t = 0
for si in s:
    if si == 'E':
        t += 1
        e_l.append(t)
    else:
        e_l.append(t)

mn = 10000000000
res = 0

for i in range(1, n-1):
    res = w_l[i-1] + e_l[-1] - e_l[i]
    if res < mn:
        mn = res

if mn > e_l[-1]:
    mn = e_l[-1]

if mn > w_l[n-2]:
    mn = w_l[n-2]

print(mn)

Submission status: Accepted

Solution code:

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

w2e = [0] * n
e2w = [0] * n

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

for i in reversed(range(n)):
    j = n - i - 1
    if j == 0:
        if s[i] == 'E':
            e2w[j] = 1
    else:
        if s[i] == 'E':
            e2w[j] = e2w[j-1] + 1
        else:
            e2w[j] = e2w[j-1]

out = n
for i in range(n):
    if i == 0:
        temp = e2w[n-1]
    elif i == n-1:
        temp = w2e[n-1]
    else:
        temp = w2e[i-1] + e2w[n-i-1]
    if temp < out:
            out = temp

print (out)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
a, e = 0, s.count("E")
for c in s:
    if c == 'W':
        n = min(n, a+e)
        a += 1
    else:
        e -= 1
        n = min(n, a+e)
print(n)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

s2 = s[::-1]
ce = [0] * (n + 1)
for i in range(1, n + 1):
    if s2[i - 1] == 'E':
        ce[i] = ce[i - 1] + 1
    else:
        ce[i] = ce[i - 1]
ce = ce[::-1]

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
num = S.count('E')
min_num = num
for i in range(N-1):
    if S[i]=='W':
        num += 1
    if S[i+1]=='E':
        num -= 1
    min_num = num if min_num > num else min_num
print(min_num if S[0]=='W' else min_num-1)

Submission status: Accepted

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

Wnum = []
Wnum.append(0)
for c in S:
        if c == 'W':
                Wnum.append(Wnum[-1]+1)
        else:
                Wnum.append(Wnum[-1])

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

ans = N
for i in range(N):
        ans_temp = Wnum[i]+Enum[i]
        if ans_temp < ans:
                ans = ans_temp

print(ans)

Submission status: Accepted

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

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

    m = min(turn, m)

print(m)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    s = list(input())
    w = 0
    e = 0
    r = [0]*n
    r[0] = s[1:].count('E')
    for i in range(n-1):
        r[i+1] = r[i]
        if s[i] == 'W':
            r[i+1] += 1
        if s[i+1] == 'E':
            r[i+1] -= 1
    print(min(r))
main()

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
s = list(input())
num_w = 0
num_e = Counter(s[1:])['E']
ans = num_e
for i in range(1, n):
    if s[i-1] == 'W':
        num_w += 1
    if s[i] == 'E':
        num_e -= 1
    ans = min(ans, num_w + num_e)
print(ans)

Submission status: Accepted

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

    print(resolve(s))

def resolve(s):
    # count total
    total_east = 0
    total_west = 0

    for c in s:
        if c == 'E':
            total_east += 1
        else:
            total_west += 1
    
    ww = 0
    ee = total_east
    
    m = len(s) - 1
    for i, c in enumerate(s):
        if c == 'E':
            ee -= 1

        x = ee + ww
        m = min(m, x)

        if c == 'W':
            ww += 1
    
    return m


main()

Submission status: Accepted

Solution code:

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

a = []
t = 0
for i,s in enumerate(S):
    if s == "E":
        t += 1
        a.append(t)
    else:
        t -= 1
        a.append(t)

index = a.index(max(a))

X = S[:index]
Y = S[index+1:]

print(X.count("W")+Y.count("E"))

Submission status: Accepted

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

X = ""
Y = S[1:]

leader = S[0]
num_x = 0
num_y = Y.count("E")

num = num_y

for i in range(1, N):
    
    X_new = leader
    
    if X_new == "W":
        num_x += 1
    
    leader = S[i]
    
    if leader == "E": 
        num_y -= 1
    
    num = min(num, num_x+num_y)
    
print(num)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# 
import math
import sys
import itertools
import numpy as np

n =int(input())
s = list(input())
a=[]
k = 0
for i in range(1,n,1):
    if s[i] == 'E':
        k = k+1
a.append(k)

for i in range(1,n,1):
    if s[i] == 'E' and s[i-1] == 'E':
        a.append(a[i-1]-1)
    elif s[i] == 'E' and s[i-1] == 'W':
        a.append(a[i-1])
    elif s[i] == 'W' and s[i-1] == 'E':
        a.append(a[i-1])
    else :
        a.append(a[i-1]+1)

print(min(a))

Submission status: Accepted

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

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

for i in range(1, n):
    if s[i-1] == 0:
        count += 1
    if s[i] == 1:
        count -= 1
    if count < min:
        min = count
print(min)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
w_l = [0]*(n+1)
e_l = [0]*(n+1)
ans = float('Inf')
for i in range(0, len(s)):
  if s[i] == 'W':
    w_l[i+1] = w_l[i] + 1
    e_l[i+1] = e_l[i]
  else:
    e_l[i+1] = e_l[i] + 1
    w_l[i+1] = w_l[i]

for i in range(len(s)):
  ans = min(ans, e_l[-1] - e_l[i-1] + w_l[i-1])
print(ans)

Submission status: Accepted

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

dpL=[0 for _ in range(N)]
dpR=[0 for _ in range(N)]

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

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

ans=10**6
for i in range(N):
  ans=min(ans,dpR[i]+dpL[i])
  
print(ans)

Submission status: Accepted

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

a = []
count = 0

for i in range(n):
    if s[i] == "W":
        a.append(count)
    else:
        count += 1
        a.append(count)

num = 10**6

for i in range(n):
    if i == 0:
        num = a[n-1] - a[0]
    else:
        num = min(num, (a[n - 1] - a[i]) + (i - a[i - 1]))


print(num)

Submission status: Accepted

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

Submission status: Accepted

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

w = [0] * n
for i in range(n-1):
  w[i+1] = w[i] + int(s[i] == 'W')
    
    
e = [0] * n
for i in reversed(range(1, n)):
  e[i-1] = e[i] + int(s[i] == 'E')
    
print(min(ww+ee for ww, ee in zip(w, e)))

Submission status: Accepted

Solution code:
def c_attention(N, S):
    count_lookme = S.count('W')
    if S[0] == 'W':
        count_lookme -= 1
    ans = N - count_lookme - 1
    for k in range(1, N):
        if S[k] == 'W':
            count_lookme -= 1
        if S[k - 1] == 'E':
            count_lookme += 1
        ans = min(ans, N - count_lookme - 1)
    return ans

N = int(input())
S = input().strip()
print(c_attention(N, S))