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

Solution (Python):
import sys
from collections import deque
import copy


def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [int(input_raw[0])]
    input_raw = read_func().strip().split()
    [S] = [(input_raw[0])]

    min_count = 0
    for i in range(1, N):
        if S[i] != "W":
            min_count += 1
    prev_count = min_count

    for i in range(1, N):

        if S[i - 1] == "E" and S[i] == "E":
            prev_count -= 1
        elif S[i - 1] == "W" and S[i] == "W":
            prev_count += 1
        if prev_count < min_count:
            min_count = prev_count

    print min_count


if __name__ == '__main__':
    main()

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

d = {0:0}
l = [0]*N
for i in range(1,N+1):
    if S[i-1] == "E":
        d[i] = d[i-1]+1
    else:
        d[i] = d[i-1]
    l[i-1] = i-1-d[i-1]-d[i]

print(min(l)+d[N])

Solution (Python):
import functools
a = int(input())
S = input()
l = [0] * (a+1)
r = [0] * (a+1)
for i, s in enumerate(S):
  l[i+1] = l[i] + 1 if s=="W" else l[i]
for i,s in enumerate(S[::-1]):
  r[i+1] = r[i] + 1 if s=="E" else r[i]
b = [l[i]+r[a-1-i] for i in range(a)]
c = functools.reduce(lambda x, y: min(x, y), b)
print(c)

Solution (Python):

"""
Contents   : ARC098 C問題
Author     : (製作者の名前) (製作者の所属)
LastUpdate : (更新日YYYY/MM/DD)
Since      : (作成日YYYY/MM/DD)
"""
N = int(input())
S = input()
cnt = 0
for s in S[1:]:
    if s == "E":
        cnt += 1
cnt_list = []
cnt_list.append(cnt)
for i in range(len(S)-1):
    if S[i] == "E" and S[i+1] == "E":
        cnt -= 1
        cnt_list.append(cnt)
    elif S[i] == "E" and S[i+1] == "W":
        cnt_list.append(cnt)
    elif S[i] == "W" and S[i+1] == "E":
        cnt_list.append(cnt)
    else:
        cnt += 1
        cnt_list.append(cnt)
print(min(cnt_list))

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



W_people = [0]
W_psn = 0
for i in range(N-1):
    if S[i] == 'W':
        W_psn += 1
    W_people.append(W_psn)

E_people = [0]
E_psn = 0
S.reverse()
for i in range(N-1):
    if S[i] == 'E':
        E_psn += 1
    E_people.append(E_psn)
S.reverse()
E_people.reverse()




_min = N
for i in range(N):
    _min = min(E_people[i] + W_people[i], _min)
    
print(_min)

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

numE_R = S.count('E')
numW_R = N - numE_R
numE_L, numW_L = 0, 0

ans = float('inf')
for Si in S:
    if Si == 'E':
        numE_R -= 1
    else:
        numW_R -= 1

    ans = min(ans, numW_L + numE_R)

    if Si == 'E':
        numE_L += 1
    else:
        numW_L += 1

print(ans)

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, S):

    W = 0
    E = S.count('E')
    ans = E + W
    for s in S:
        if s == 'E':
            E -= 1

        ans = min(E + W, ans)

        if s == 'W':
            W += 1

    return ans


def main():
    N = read_int()
    S = read_str()
    print(slv(N, S))


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
S = input()
L = []
L.append(S[1:].count('E'))
for i in range(1,N):
    if S[i-1] =='E' and S[i]=='E':
        a = L[-1] -1 
    elif S[i-1] =='W' and S[i]=='W':
        a = L[-1] +1 
    else:
        a = L[-1]
    L.append(a)
print(min(L))

Solution (Python):
n = int(input())
lis = [0]
s = str(input())
cou = 0
co  = 0
for i in range(n-1):
    if s[i] == "W":cou += 1
    lis.append(cou)
for k in range(1,n):
    if s[n-(k)] == "E":co += 1
    lis[n-(k+1)] += co
print(min(lis))

Solution (Python):
n = int(input())



s = input()

W = []
S = []

if s[0] == 'W':
    W.append(1)
    S.append(0)
else:
    W.append(0)
    S.append(1)

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


min = min(S[n - 1] - S[0], W[n - 2])


for i in range(1, n - 1):
    tmp = W[i - 1] + S[n - 1] - S[i]
    if min > tmp:
        min = tmp




print(min)

Solution (Python):
input()
S = input()
tmp = sum([1 for s in S[1:] if s == "E"])
ans = tmp
for i in range(1, len(S)):
    if S[i-1] == "W":
        tmp += 1
    if S[i] == "E":
        tmp -= 1
    ans = min(ans, tmp)
print(ans)

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

W = S.count('W')
E = N - W

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

print(ans)

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

e = s.count('E')
w = 0
c = 10**6
for i in range(n):
    if s[i] == 'E':
        e -= 1
        c = min(c, w + e)
    else:
        c = min(c, w + e)
        w += 1

print(c)

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


count = 0
for e,i in enumerate(s):
    if e == 0:
        continue
    if i == 'E':
        count += 1

mincount = count
for e,i in enumerate(s):
    if e == len(s)-1:
        break
    if e == 0 and i == 'E':
        continue
    if i == 'W':
        count+=1
    else:
        count-=1

    if s[e+1] == 'E':
        mincount = min(count-1,mincount)
    else:
        mincount = min(count,mincount)

print(mincount)

Solution (Python):
from collections import Counter

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

right_EW = Counter(S)
left_EW = {"E":0,"W":0}

ans = len(S)
for s in S:
    right_EW[s] -= 1
    
    temp = left_EW["W"] + right_EW["E"]
    if temp < ans: ans = temp
    left_EW[s] += 1
    
print(ans)

Solution (Python):
n=int(input())
s=input()
w,e=[],[]
a,b=0,0
for i in range(n):
    if s[i]=='W':
        a+=1
    else:
        b+=1
    w.append(a)
    e.append(b)
x=n
for i in range(n):
    if s[i]=='W':
        if x>e[-1]-e[i]+w[i]-w[0]:
            x=e[-1]-e[i]+w[i]-w[0]
    else:
        if x>e[-1]-e[i]+w[i]:
            x=e[-1]-e[i]+w[i]
print(x)

Solution (Python):
n = int(input())
s = list('N' + input())
all_e = s.count('E')
all_w = s.count('W')
now_e, now_w = 0, 0
ans = float('inf')
me = ''
for i in s:
    me = i
    if i == 'E':
        now_e += 1
    elif i == 'W':
        now_w += 1
    need = (all_e - now_e) + now_w
    if ans > need:
        ans = need
print(ans)

Solution (Python):
N = int(input())
queue = [x for x in input()]

n_wests = 0
n_easts = queue.count('E') - (1 if queue[0] == 'E' else 0)
min_i = 0
min_n_changes = n_easts
for i in range(1, N):
    prev_dir = queue[i - 1]
    cur_dir = queue[i]
    if prev_dir == 'W':
        n_wests += 1
    if cur_dir == 'E':
        n_easts -= 1
    n_changes = n_wests + n_easts
    if n_changes < min_n_changes:
        min_n_changes = n_changes
        min_i = i

print(min_n_changes)

Solution (Python):


n = int(input())
acc_e = [0]
acc_w = [0]

for (i,s) in enumerate(list(input())):
    if s=="W":
        acc_w.append(acc_w[i]+1)
        acc_e.append(acc_e[i])
    else:
        acc_w.append(acc_w[i])
        acc_e.append(acc_e[i]+1)




ret = 999999
for i in range(n):
    r = acc_w[i] + (acc_e[-1]-acc_e[i+1])
    if r<ret:
        ret = r

print(ret)

Solution (Python):
n = int(input())
s = input()
cnt_w = [0]
for i in range(n):
    cnt_w.append(cnt_w[i] + (1 if s[i] == 'W' else 0))
cnt_w.append(cnt_w[n])
min_cnt = cnt_w[n-1]
for i in range(n):
    min_cnt = min(min_cnt, cnt_w[i] + (n - 1 - i) - (cnt_w[n] - cnt_w[i + 1]))
print(min_cnt)

Solution (Python):
n = input()
s = list(input())
line = [0 for _ in range(0,len(s)+1)]
for i in range(0,len(s)):
    if s[i] =="W":
        line[0] += 1
        line[i+1] -= 1
    if s[i] =="E":
        line[i] += 1
        line[len(s)] -= 1
for i in range(0,len(line)-1):
    line[i+1] += line[i]
print(len(s) - max(line))

Solution (Python):


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

west_w = 0
west_e = 0
east_w = S[1:].count("W")
east_e = S[1:].count("E")

min_change = west_w + east_e
for i in range(1, N):
    if S[i-1] == "W":
        west_w += 1
    else:
        west_e += 1
    if S[i] == "W":
        east_w -= 1
    else:
        east_e -= 1
    change = west_w + east_e
    if change < min_change:
        min_change = change

print(min_change)

Solution (Python):

n = int(input())
s = input()
ans = float('inf')
E_total = s.count('E')
W_total = s.count('W')
cnt = {'E': 0, 'W': 0}
for i in range(n):
    turn = cnt['W'] + (E_total - cnt['E'])
    if s[i] == 'E':
        turn -= 1
    ans = min(ans, turn)
    cnt[s[i]] += 1
print(ans)

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

first = 0

for i in range (n-1):
    if s[i+1] == "E":
        first += 1
mini = first

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

print(mini)

Solution (Python):
def solve():
    N = int(input())
    S = input()

    AW = [0]*(N+1)
    AE = [0]*(N)

    wcount = 0
    ecount = 0
    for i in range(N):
        if S[i] == "W":
            wcount += 1
        else:
            ecount += 1
        AW[i] = wcount
        AE[i] = ecount

    MIN = 1000000

    for i in range(N):
        tmp = 0
        tmp += AW[i-1]
        tmp += AE[-1] - AE[i]
        MIN = min(MIN, tmp)
    print(MIN)

if __name__ == "__main__":
    solve()

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

accLeft = [0]
accRight = [0]

tmp = 0
for c in S:
  tmp += int(c == 'W')
  accLeft.append(tmp)
accLeft.append(0)

tmp = 0
for c in S[::-1]:
  tmp += int(c == 'E')
  accRight.append(tmp)
accRight.append(0)
accRight = accRight[::-1]

print(min([accLeft[i-1] + accRight[i+1] for i in range(1, N+1)]))

Solution (Python):
N = int(input())
s = input()
cnt = 0
acce = [0]*N
accw = [0]*N
if s[0] == 'E':
	acce[0] = 1
else:
	accw[0] = 1
for i in range(1,N):
	if s[i] == 'E':
		acce[i] = acce[i-1]+1
		accw[i] = accw[i-1]
	else:
		accw[i] = accw[i-1]+1
		acce[i] = acce[i-1]

m = acce[N-1] - acce[0]
for i in range(1,N):		
	if m > (accw[i-1]+(acce[N-1]-acce[i])):
		m = (accw[i-1]+(acce[N-1]-acce[i]))


print(m)

Solution (Python):
N = int(input())
S = input()
cnt = 0
for i in S[1:]:
    if i == 'E':
        cnt += 1
min_cnt = cnt
for i in range(N-1):
    if S[i] == 'E' and S[i + 1] == 'E':
        cnt -= 1
    elif S[i] == 'W' and S[i + 1] == 'W':
        cnt += 1
    if cnt < min_cnt:
        min_cnt = cnt
print(min_cnt)

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

change = []
s0 = 0
for i in range(1, n):
    if s[i] == "E":
        s0 = s0 + 1
change.append(s0)

for i in range(1, n - 1):
    if s[i - 1] == "W":
        s0 = s0 + 1
    if s[i] == "E":
        s0 = s0 - 1
    change.append(s0)

s0 = 0
for i in range(n - 1):
    if s[i] == "W":
        s0 = s0 + 1
change.append(s0)

print(min(change))

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

ecount = [0]*N
if S[0] == "E":
    ecount[0] = 1
for i in range(1, N):
    if S[i] == "E":
        ecount[i] = ecount[i-1] + 1
    else:
        ecount[i] = ecount[i-1]
ans = float("INF")
for i in range(N):
    west = 0
    if i > 1:
        west = i+1 - ecount[i]
    east = 0
    if i < N-1:
        east = ecount[N-1] - ecount[i]
    ans = min(ans, west+east)
print(ans)

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

ans = n - 1
w = [0]
for i in range(n - 1):
    w.append(w[-1])
    if s[i] == 'W':
        w[-1] += 1
e = [0 for _ in range(n)]
for i in range(n - 1, 0, - 1):
    e[i - 1] = e[i]
    if s[i] == 'E':
        e[i - 1] += 1
for i in range(n):
    ans = min(ans, e[i] + w[i])
print(ans)

Solution (Python):
N = int(input())
S = input()
W, E = [0], [0]
for s in S[::-1]:
    if s == "E":
        E.append(E[-1]+1)
    else:
        E.append(E[-1])

for s in S:
    if s == "W":
        W.append(W[-1]+1)
    else:
        W.append(W[-1])

ans = float('inf')
E = E[::-1]
for i in range(N):
    ans = min(ans, E[i-1]+W[i-1])

print(ans)

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

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

print(a)

Solution (Python):
n = input()
s = input()


e_cnt = 0
for i in range(1, len(s)):
    if s[i] == 'E':
        e_cnt += 1
res = []
res.append(e_cnt)
for i in range(1, len(s)):
    add = 0
    if s[i-1] == 'W':
        add += 1
    if s[i] == 'E':
        add -= 1
    res.append(res[i-1] + add)

print(min(res))

Solution (Python):
def main():
    n = int(input())
    s = input().strip()
    w = []
    count = 0
    for c in s:
        w.append(count)
        if c == 'W':
            count += 1
    e = []
    count = 0
    for c in s[::-1]:
        e.append(count)
        if c == 'E':
            count += 1
    e = e[::-1]
    print(min(i + j for i, j in zip(w, e)))


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
S = input()
es = [0]
for c in S:
    es.append(es[-1] + (c=='W'))

ans = N
for i in range(N):
    tmp = (N-i-1) - (es[N] - es[i+1])
    tmp += es[i]
    ans = min(ans,tmp)
print(ans)

Solution (Python):
import numpy as np


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


def lii():
    return list(map(int, input().split(' ')))


def lvi(N):
    l = []
    for _ in range(N):
        l.append(ii())
    return l


def lv(N):
    l = []
    for _ in range(N):
        l.append(input())
    return l


def main():
    N = ii()
    S = input()
    e = 0
    for v in S:
        if v == 'E': e += 1

    min_n_turn = N
    ec = 0
    for i in range(N):
        ew = (N - i - 1) - (e - ec - int(S[i] == 'E'))
        n_turn = N - 1 - ec - ew
        if S[i] == 'E':
            ec += 1
        
        min_n_turn = min(min_n_turn, n_turn)

    return min_n_turn

if __name__ == '__main__':
    print(main())

Solution (Python):
N = int(input())
s = input()
w = 0
e = 0
for i in s[1:]:
  if i == "E":
    e += 1
ans = e
tmp = e
for i in range(1,N):
    if s[i-1] == "E" and s[i] == "E":
        tmp -= 1
    elif s[i-1] == "W" and s[i] == "E":
        pass
    elif s[i-1] == "E" and s[i] == "W":
        pass
    elif s[i-1] == "W" and s[i] == "W":
        tmp += 1
    if ans > tmp:
        ans = tmp
print(ans)

Solution (Python):


def main():
	n = int(input())
	s = list(input())
	d = {'W':0, 'E':1}
	nums = [d[x] for x in s]

	left = 0
	right = sum(nums[1:])
	min_cost = right

	for i in range(1, n):	
		left = left + (1 - nums[i-1])
		right = right - nums[i]
		min_cost = min(min_cost, left + right)

	print(min_cost)



if __name__ == '__main__':
	main()

Solution (Python):
n = int(input())
s = input()
a = [0] * n
if s[0] == 'W': a[0] = 1
for i in range(1, n):
    c = s[i]
    if c == 'E':
        a[i] = a[i - 1]
    else:
        a[i] = a[i - 1] + 1
b = [0] * n
if s[n - 1] == 'E': b[n - 1] = 1
for i in range(n - 2, -1, -1):
    c = s[i]
    if c == 'W':
        b[i] = b[i + 1]
    else:
        b[i] = b[i + 1] + 1
m = n
for i in range(n):
    m = min(m, a[i] + b[i])
print(m - 1)

Solution (Python):
N=int(input())
S=input()
Cost=0
for i in range(1,N):
    if S[i]=="E":
        Cost+=1
Costtmp=Cost
for i in range(1,N):
    if S[i-1]==S[i]:
        if S[i]=="W":
            Costtmp+=1
        else:
            Costtmp-=1
    if Costtmp<Cost:
        Cost=Costtmp
print(Cost)

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

tmp = 0
left = []
for c in S:
    if c == 'W':
        tmp += 1
    left.append(tmp)

tmp = 0
right = []
for c in reversed(S):
    if c == 'E':
        tmp += 1
    right.append(tmp)

right = list(reversed(right))

ans = min(right[0], left[N - 1])

for i in range(1, N - 1):
    ans = min(ans, left[i - 1] + right[i + 1])
print(ans)

Solution (Python):
from collections import Counter

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

cnt = Counter(S[1:])["E"]
ret = cnt
for i in range(1, N):
    if S[i] == "E":
        cnt -= 1
    if S[i-1] == "W":
        cnt += 1
    ret = min(ret, cnt)
print(ret)

Solution (Python):
N = input()
S = raw_input()
lcnt = [0]*N
rcnt = [0]*N
run = 0
for i,c in enumerate(S):
    lcnt[i] = run
    run+=c=='W'
run=0
for i,c in enumerate(reversed(S)):
    rcnt[N-i-1] = run
    run+=c=='E'

print min(map(lambda i:lcnt[i]+rcnt[i],range(N)))

Solution (Python):
n = int(raw_input())
line = raw_input()

west = [0] * n

for i in range(n - 1):
    west[i + 1] = west[i]
    if line[i] == 'W':
        west[i + 1] += 1

east = [0] * n

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

min_change = 1e10

for i in range(n):
    if west[i] + east[i] < min_change:
        min_change = west[i] + east[i]

print min_change

Solution (Python):




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

    s = 0
    for c in S[1:]:
        if c == 'E':
            s += 1

    ANS = s
    for i in range(1, N):
        if S[i] == 'E':
            s -= 1
        if S[i - 1] == 'W':
            s += 1
        ANS = min(ANS, s)

    print(ANS)


if __name__ == '__main__':
    main()

Solution (Python):

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


res = 0
for i in range(1, N):
    if S[i] == "E":
        res += 1
res_min = res
for i in range(1, N):
    if S[i-1] == "W":
        res += 1
    if S[i] == "E":
        res -= 1
    res_min = min(res, res_min)
print(res_min)

Solution (Python):

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

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

R = [0]*(N)
L = [0]*(N)
for i in range(N-1):
    R[i+1] = R[i] + (S[i] == "W")
    L[-(i+1)-1] = L[-(i+1)] + (S[-(i+1)] == "E")

ans = 10**6

for i in range(N):
    ans = min(ans, R[i] + L[i])

print(ans)

Solution (Python):



import sys

def rl(proc=None):
    if proc is not None:
        return proc(sys.stdin.readline())
    else:
        return sys.stdin.readline().rstrip()

def srl(proc=None):
    if proc is not None:
        return map(proc, rl().split())
    else:
        return rl().split()

def main():
    rl()
    A = rl()
    right = A.count('E')
    if A[0] == 'E':
        right -= 1
    r = right
    left = 0
    for i in xrange(1, len(A)):
        if A[i] == 'E':
            right -= 1
        if A[i-1] == 'W':
            left += 1
        r = min(r, left + right)
    print r


if __name__ == '__main__':
    main()

Solution (Python):
def ec(s):
    ans = [0]
    cur = 0
    for c in s:
        if c == 'W':
            cur += 1
        ans.append(cur)
    return ans


def main():
    n = int(input())
    s = input().strip()
    fr = ec(s)
    ba = ec(reversed(s))
    ans = n
    for i in range(n):
        ans = min(ans, fr[i] + (n - i - 1) - ba[n - 1 - i])
        
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n = I()
    s = S()
    c = collections.Counter(s)
    d = collections.defaultdict(int)
    r = n - 1
    for i in range(n):
        c[s[i]] -= 1
        t = c['E'] + d['W']
        if r > t:
            r = t
        d[s[i]] += 1

    return r




print(main())

Solution (Python):
def makelist(n, m):
    return [[0 for _ in range(m)] for _ in range(n)]


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

cnt = 0
ans = int(1e9)
for c in S:
    if c == "E":
        cnt += 1

for c in S:
    if c == "E":
        cnt -= 1
        ans = min(ans, cnt)
    else:
        ans = min(ans, cnt)
        cnt += 1
print(ans)

Solution (Python):

import sys

input=sys.stdin.readline

n=int(input())

s=input().strip()

ans=s[1:].count('E')
t=ans

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

Solution (Python):
def main():
    n = int(raw_input())
    s = raw_input().strip()
    e = [0] * (n + 1)
    for i, c in enumerate(s):
        e[i+1] = e[i] + (c == 'E')
    ans = n
    x = 0
    for i, c in enumerate(s):
        if ans > x + e[n] - e[i+1]:
            ans = x + e[n] - e[i+1]
        x += c == 'W'
    print ans
main()