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):
N=int(input())
S=list(input())
RS=S[::-1]
W=[0]
E=[0]
for i in range(N):
    if S[i]=="W":
        W.append(W[-1]+1)
    else:
        W.append(W[-1])
W.append(0)
for i in range(N):
    if RS[i]=="E":
        E.append(E[-1]+1)
    else:
        E.append(E[-1])
E.append(0)
E=E[::-1]


ans=10**9
for i in range(1,N+1):
    if W[i-1]+E[i+1]<ans:
        ans=W[i-1]+E[i+1]
print(ans)

Solution (Python):
N=int(input())
S=input()
X=[1 if s=='W' else 0 for s in S]
import itertools
cumsumW = list(itertools.accumulate([0]+X))
ans=N
for i in range(1,N+1):
  _=cumsumW[i-1]+(N-i)-(cumsumW[-1]-cumsumW[i])
  if _<ans:
    ans=_
print(ans)

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


ans = int(1e9)
for i in range(n):
    if w[i] + (e[-1] - e[i+1]) < ans:
        ans = w[i] + (e[-1] - e[i+1])
print(ans)

Solution (Python):
n = int(input())
s = input()
now = 0
for i in range(1, n):
    if s[i] == "E":
        now += 1
ans = now
for i in range(1, n):
    if s[i] == "E":
        now -= 1
    if s[i-1] == "W":
        now += 1
    ans = min(ans, now)
print(ans)

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

sumE=[0]*(N+1)

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

ans=10**9
for i in range(N):
    temp=(i-sumE[i])+(sumE[-1]-sumE[i+1])
    ans=min(ans,temp)
    
print(ans)

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

E_lst = []
cnt = 0
for i in range(N):
    if S[i] == 'E':
        cnt += 1
    E_lst.append(cnt)
min = N + 1
for i in range(N):
    nw = 0 
    if i > 0:
        nw = i - E_lst[i - 1]
    ne = E_lst[N - 1] - E_lst[i]
    if nw + ne < min:
        min = nw + ne
print(min)

Solution (Python):
def main():
    people_num = int(input())
    directions = input()
    east_direction = [0]
    for i in range(people_num):
        if directions[i] == "E":
            east_direction.append(east_direction[i] + 1)
        else:
            east_direction.append(east_direction[i])
    answer = float("inf")
    for i in range(1, people_num + 1):
        change_to_east = i - 1 - east_direction[i - 1]
        change_to_west = east_direction[-1] - east_direction[i]
        answer = min(answer, change_to_east + change_to_west)
    print(answer)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
s = input()
a = [0] * (n + 1)

for i, j in enumerate(s):
    if j == "W":
        a[i + 1] = a[i] + 1
    else:
        a[i + 1] = a[i]

ans = n
zz = n - a[n]
for i in range(1, n + 1):
    b = a[i - 1] + zz - (i - a[i])
    ans = min(b, ans)
print(ans)

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


W = [0] * (N + 1)
E = [0] * (N + 1)


for i in range(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


ans = 3 * 10 **5
for i in range(N):
    
    change_west_to_east = W[i-1]
    
    change_east_to_west = E[N-1] - E[i]
    
    change = change_east_to_west + change_west_to_east
    
    ans = min(ans, change)

print(ans)

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

ans = 3*(10**5)+1
right_E = S.count('E')
W_left = 0

for n in range(N):
  _ = S[n]
  if _ == 'E':
    right_E -= 1
    cnt = W_left+right_E
  elif _ == 'W':
    W_left += 1
    cnt = W_left+right_E-1
  ans = min(ans,cnt)

print(ans)

Solution (Python):


import sys
import math
import time

import collections
from collections import deque
import queue
import copy


def gcd(m,n):
  x = max(m,n)
  y = min(m,n)
  if x%y == 0:
    return y
  else:
    while x%y != 0:
      z = x%y
      x = y
      y = z
    else:
      return z


def bfs(SS, x, y, H, W):
  x_next = [0, 0, -1, 1]
  y_next = [1, -1, 0, 0]
  tempx = x
  tempy = y
  mlength = 0
  q = deque()
  xytuple = (x, y, 0)
  q.append(xytuple)


  while q:
    xytuple = q.popleft()
    SS[xytuple[0]][xytuple[1]] = "x" 
    for i in range(4): 
      tempx = xytuple[0] + x_next[i]
      tempy = xytuple[1] + y_next[i]
      
      
      if(0 <= tempx and tempx < H and 0 <= tempy and tempy < W and SS[tempx][tempy] == "."):
        temptuple = (tempx, tempy, xytuple[2]+1)
        SS[temptuple[0]][temptuple[1]] = "x" 
        mlength = max(mlength, xytuple[2] +1)
        q.append(temptuple)
      
  return mlength


def prime_factorize(n):
  a = []
  while n % 2 == 0:
    a.append(2)
    n //= 2
  f = 3
  while f * f <= n:
    if n % f == 0:
      a.append(f)
      n //= f
    else:
      f += 2
  if n != 1:
    a.append(n)
  return a


def has_duplicates(seq):
  return len(seq) != len(set(seq))







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

c = S.count("E")

if(S[0] == "E"):
  c -= 1

ans = 0

temp = c

for i in range(1, N):
  if(S[i-1] == "E"):
    if(S[i] == "E"):
      c -= 1
      if(c<temp):
        ans = i
        temp = c
  else:
    if(S[i] == "W"):
      c += 1




print(temp)

Solution (Python):

import math
import heapq
import bisect
import numpy as np
from collections import Counter


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

left_W = [0]*N
right_E = [0]*N

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

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

ans = N
for i in range(N):
    ans = min(ans, left_W[i]+right_E[i])

print(ans)

Solution (Python):
import numpy as np
import sys
buf = sys.stdin.buffer
N = int(buf.readline())
L = list(input())

tE = L.count("E")
tW = N-tE
ce,cw = 0,0
ans = 3*(10**5)
for i in range(N):
    if L[i] == "E":
        ce +=1
    elif L[i] == "W":
        cw +=1
    if L[i] == "E":
        ans = min(ans,cw+(tE-ce))
    elif L[i] == "W":
      
        ans = min(ans,cw-1+(tE-ce))
print(ans)

Solution (Python):
N=int(input())
S=input()
A=[0]*N
A[0]=S[1:].count('E')
for i in range(1,N):
    if S[i-1]=='W':
        A[i]=A[i-1]+1
    else:
        A[i]=A[i-1]
    if S[i]=='E':
        A[i]-=1
print(min(A))

Solution (Python):
n = int(input())
s = input()
a = [0] * n
a[0] = s[1:].count("E")
for i in range(1,n):
    if s[i-1] == "W":
        a[i] = a[i-1] + 1
    else:
        a[i] = a[i-1]
    if s[i] == "E":
        a[i] -= 1
print(min(a))

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

curr_change_cnt = min_change_cnt = sum(c == 'E' for c in s[1:])
for i in range(1, n):
    if s[i - 1] == 'W':
        curr_change_cnt += 1

    if s[i] == 'E':
        curr_change_cnt -= 1

    min_change_cnt = min(curr_change_cnt, min_change_cnt)

print(min_change_cnt)

Solution (Python):
from collections import Counter

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

C_E = Counter(S[1:])
C_W = Counter()
ans = C_E['E']

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

Solution (Python):
N = int(input())
S = input()
a = [0] * N
a[0] = S[1:].count("E")
for i in range(1, N):
    if S[i - 1] == "W":
        a[i] = a[i - 1] + 1
    else:
        a[i] = a[i - 1]
    if S[i] == "E":
        a[i] -= 1
print(min(a))

Solution (Python):
n = int(input())
s = input()
e = [0] * (n + 1)
w = [0] * (n + 1)

for i in range(len(s)):
    if s[i] == "E":
        e[i+1] = e[i] + 1
        w[i+1] = w[i]
    else:
        w[i+1] = w[i] + 1
        e[i+1] = e[i]

ans = 10 ** 9
for i in range(0, n+1):
    t = w[i] + e[n] - e[i]
    ans = min(ans, t)

print(ans)

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

Solution (Python):

n=int(input())
s=str(input())
e_cnt=s.count('E')
num=[0]*n 
num2=[0]*(n+1) 
count=0
count2=0
for i in range(n):
    if s[i]=='W':
        num[i]=count
        num2[i]=e_cnt-count2
        count+=1
    else:
        num[i]=count
        num2[i]=e_cnt-count2 
        count2+=1
    
   


ans=[0]*n

for i in range(n):
    ans[i]=abs(num[i]+num2[i+1])
print(min(ans))

Solution (Python):

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

east = [0]*(N+1)
west = [0]*(N+1)


count = 0
for i, s in enumerate(S):
    if s == 'E':
        count+=1
    east[i+1] = count

count = 0
for i, s in enumerate(S):
    if s == 'W':
        count+=1
    west[i+1] = count

minimum = 10**6

for i in range(N):
    tmp = (west[i]-west[0]) + (east[N]-east[i+1])
    
    if tmp < minimum:
        minimum = tmp

print(minimum)

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

tmp = S[1:].count('E')
  
minimum = tmp

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

print(minimum)

Solution (Python):

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

tmp = 0

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

minimum = tmp

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

print(minimum)

Solution (Python):
from itertools import accumulate

def main():
    N = int(input())
    people = list(input())

    wests = list(accumulate([0]+people, lambda total, x: total + (1 if x == "W" else 0)))[1:]
    easts = list(accumulate([0]+people, lambda total, x: total + (1 if x == "E" else 0)))[1:]

    
    def linearsearch():
        easts_total = easts[-1]
        for i in range(N):
            yield (0 if i == 0 else wests[i-1]) + easts_total - easts[i]

    print(min(linearsearch()))

main()

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

counter = Counter(S)
E = counter.get('E', 0)
W = counter.get('W', 0)


ans = 10**6
e, w = 0, 0
for i in range(N):
    v = w + E - e
    if S[i] == 'E':
        v -= 1
        e += 1
    else:
        w += 1
    
    ans = min(ans, v)
print(ans)

Solution (Python):
n = int(input())
s = input()
ans = 10**9
w = s.count('W')
e = s.count('E')
w_count = 0
e_count = 0
for i in range(n):
    if s[i] == 'W':
        w -= 1
        ans = min(ans,e+w_count)
        w_count += 1
    else:
        e -= 1
        ans = min(ans,e+w_count)
        e_count += 1

print(ans)

Solution (Python):
import itertools
n = int(input())
s = input()
ans = 0
wl = []
el = []
for ss in s:
    if ss == 'W':
        wl.append(1)
        el.append(0)
    else:
        wl.append(0)
        el.append(1)
el.reverse()
wll = list(itertools.accumulate(wl))
ell = list(itertools.accumulate(el))
ell.reverse()

INF = float("inf")
ans = INF

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

print(ans-1)

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

W=[0]*N
w = 0
for i in range(N):
    if S[i] == "W":
        w += 1
    W[i] = w

E = [0]*N
e = 0
for i in range(N):
    if S[N-1-i] == "E":
        e += 1
    E[N-1-i] = e
ans = 10**9
for i in range(N):
    count = W[i] + E[i]
    ans = min(ans,count)

print(ans-1)

Solution (Python):


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

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

    
    ans = 10**6
    for a, b, in zip(l, r):
        ans = min(a+b, ans)
    print(ans)
    

if __name__ == "__main__":
    main()

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

l = [0]*n
r = [0]*n
for i in range(1,n):
    l[i] = l[i-1]+(s[i-1]=="W")
for i in range(n-1)[::-1]:
    r[i] = r[i+1]+(s[i+1]=="E")

ans = 10**6
for a,b in zip(l,r):
    ans = min(a+b,ans)
print(ans)

Solution (Python):
def solve():
    N = int(input())
    S = input()
    attention = sum(s != 'W' for s in S[1:])
    min_val = attention
    
    for i in range(1,N):
        if S[i] == 'E': attention -= 1
        if S[i-1] != 'E':
            attention += 1
        
        min_val = min(min_val, attention)
    
    
    print(min_val)
    
solve()

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

left = [0] * N
right = [0] * N
temp_left = 0
temp_right = 0

for i in range(N):
    left[i] = temp_left
    if S[i] == "W":
        temp_left += 1

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

ans = float("inf")
for l, r in zip(left, right):
    ans = min(ans, l + r)

print(ans)

Solution (Python):
n = int(input())
l = list(input())
left = []
right = []
left.append(0)
lc = 0
rc = 0
for i in range(n):
    if l[i] == "W":
        lc += 1
    if l[-1-i] == "E":
        rc += 1
    left.append(lc)
    right.append(rc)
right.append(0)
count = 300000

for i in range(n):
    a = left[i]+right[n-2-i]
    count = min(a,count)
print(count)

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

east_to_west = [0]*len(S)
west_to_east = [0]*len(S)

e = 0
w = 0
for i, s in enumerate(S):
  west_to_east[i] = e
  if s == 'W':
    e += 1

index = len(S) - 1
for i, s in enumerate(reversed(list(S))):
  east_to_west[index] = w
  index -= 1
  if s == 'E':
    w += 1

ans = 3*(10**5)
for n in range(N):
  sum = west_to_east[n] + east_to_west[n]
  if sum < ans:
    ans = sum
    
print(ans)

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

e_count=0
w_count=0
s_e=[]
s_w=[]



for i in range(n) :
    if s[i] == "W" :
        w_count += 1
        s_e.append(e_count)
        s_w.append(w_count)
    elif s[i] == "E" :
        e_count += 1
        s_e.append(e_count)
        s_w.append(w_count)



min = n+1

for i in range(n) :
    if i == 0 :
        sum = s_e[n-1] - s_e[i]
    elif i == n-1 :
        sum = s_w[i-1]
    else :
        sum = s_e[n-1] - s_e[i] + s_w[i-1]

    if min > sum :
        min = sum

print(min)

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


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

min_change = n + 1
current_e = 0
current_w = 0
for i in range(n):
    temp_change = 0
    migi_e = cumsum_e - current_e
    if s[i] == 'E':
        migi_e -= 1
    temp_change = current_w + migi_e

    if temp_change < min_change:
        min_change = temp_change

    if s[i] == 'E':
        current_e += 1
    else:
        current_w += 1

print(min_change)

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

l = [0]*N
r = [0]*N

for i in range(1,N):
	l[i] = l[i-1]+(s[i-1]=='W')
for i in range(N-1)[::-1]:
	r[i] = r[i+1]+(s[i+1]=='E')

ans = 10**6
for a,b in zip(l,r):
	ans = min(a+b, ans)

print(ans)

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

l=[0] 
for i in range(1,n):
  w=l[-1]
  if s[i-1]=='W':
    w+=1
  l.append(w)
r=[0] 
for i in reversed(range(n-1)):
  e=r[-1]
  if s[i+1]=='E':
    e+=1
  r.append(e)
r.reverse()


ans=n
for i in range(n):
  ans=min(r[i]+l[i],ans)
print(ans)

Solution (Python):
import numpy as np
N = int(input())
S = input()
LW = np.array([0]*N)
RE = np.array([0]*N)
for i in range(1,N):
    LW[i] = LW[i-1] + int(S[i-1]=="W")
    RE[-(i+1)] = RE[-i] + int(S[-i]=="E")
P = LW + RE
print(min(P))

Solution (Python):
n = int(input())
s = input()
olle = s.count("E")
cw,ce,p=0,0,10**6
for i in s:
    if i == "E":
        ce += 1
        p = min(p,cw+olle-ce)
    else:
        cw += 1
        p = min(p,cw-1+olle-ce)
print(max(p,0))

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

w = 0
e = 0

w_list = [0]
e_list = [0]
for i in range(n):
  if s[i] == "W":
    w += 1
  else:
    e += 1
  w_list.append(w)
  e_list.append(e)

ans = 10**10
for i in range(1, n+1):
  ans = min(ans, w_list[i-1] + (e_list[-1] - e_list[i]))

print(ans)

Solution (Python):
n = int(input())
west_east_chars = input()
west_direction_num = [0] * n
east_direction_num = [0] * n

for i in range(1, n):
    if west_east_chars[i-1] == 'W':
        change = 1
    else:
        change = 0
    west_direction_num[i] += west_direction_num[i-1] + change

for i in range(n-2, -1, -1):
    if west_east_chars[i+1] == 'E':
        change = 1
    else:
        change = 0
    east_direction_num[i] += east_direction_num[i+1] + change

ans = 10 ** 10
for a, b in zip(west_direction_num, east_direction_num):
    ans = min(ans, a+b)
print(ans)

Solution (Python):
n = int(input())
s = input()
e = s.count('E')
w = 0
for i in s:
    if i == 'W':
        n = min(n, w + e)
        w += 1
    else:
        w -= 1
        n = min(n, w + e)
print(n)

Solution (Python):
N=int(input())
S=list(input())
A=[0]*N
B=[0]*N
E=0
W=0
for i in range(1,N):
  if S[i-1]=="W":
    W+=1
  if S[N-1-i+1]=="E":
    E+=1
  A[i]+=W
  A[N-1-i]+=E
  
  
print(min(A))

Solution (Python):
n = int(input())
s = [int(si == "E") for si in input()]
a = [0] * (n + 1)
for i in range(n):
  a[i + 1] = a[i] + s[i]
ans = n
for i in range(n):
  ans = min(a[-1] - a[i + 1] + i - a[i], ans)
print(ans)

Solution (Python):
def main():
    N, S = open(0).read().split()
    pre = S.count("E")
    cur = pre
    for Si in S:
        if cur < pre:
            pre = cur
        if Si == "E":
            cur -= 1
        else:
            cur += 1
    print(pre if pre < cur else cur)
    return


main()

Solution (Python):
def main():
    N, S = open(0).read().split()
    pre = S.count("E")
    cur = pre
    for Si in S:
        if pre < cur:
            cur = pre
        if Si == "E":
            pre -= 1
        else:
            pre += 1
    print(pre if pre < cur else cur)
    return


main()

Solution (Python):
def main():
    N, S = open(0).read().split()
    pre = S.count("E")
    ans = pre
    for Si in S:
        if pre < ans:
            ans = pre
        if Si == "E":
            pre -= 1
        else:
            pre += 1
    print(pre if pre < ans else ans)
    return


main()

Solution (Python):
n = int(input())
s = input()
w_count = s.count('W')
e_count = s.count('E')
w_count_cum = 0
e_count_cum = 0
ans = n

for i, s_i in enumerate(s):
  if s_i == 'W':
    w_count_cum += 1
    l_w = max(0, w_count_cum-1)
  elif s_i == 'E':
    e_count_cum += 1
    l_w = max(0, w_count_cum)
 
  r_e = max(0, e_count-e_count_cum)
  
  ans = min(ans, l_w+r_e)
    
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
from collections import Counter



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

    c = Counter(S)

    ans = float("inf")

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

    print(ans)





if __name__ == '__main__':
    main()

Solution (Python):
def main():
    n = int(input())
    s = input()
    w = [0]
    e = [sum([i=="E" for i in list(s)[1:]])]
    for i in range(1,n):
        appear = s[i-1]
        delete = s[i]
        ne = e[-1]
        nw = w[-1]
        if delete == "E":
            ne -= 1
        if appear == "W":
            nw += 1
        w.append(nw)
        e.append(ne)
    ans = [i+j for i,j in zip(w,e)]
    print(min(ans))  
    
if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
l = input()
count0 = l[1:].count("E")
count=count0
mi=count0
for i in range(1, N):
    if l[i-1]=="W":
        count+=1
    if l[i]=="E":
        count-=1
    mi=min(mi,count)
print(mi)

Solution (Python):
N = int(input())
S = input()
num_W = [0 for _ in range(N)]
for i in range(N):
    if i == 0:
        if S[i] == 'W':
            num_W[i] = 1
    else:
        if S[i] == 'W':
            num_W[i] = num_W[i-1] + 1
        else:
            num_W[i] = num_W[i - 1]

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

ans = float('inf')
for i in range(N):
    if i == 0:
        ans = min(ans, num_E_rev[i + 1])
    elif i == N - 1:
        ans = min(ans, num_W[-1])
    else:
        ans = min(ans, num_W[i-1] + num_E_rev[i+1])
print(ans)

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

left_w_count = 0
right_e_count = lis[1:].count("E")
ans = right_e_count
for i in range(1,n):
    if lis[i-1] == "W":
        left_w_count += 1
    if lis[i] == "E":
        right_e_count -= 1
    ans = min(ans, left_w_count + right_e_count)

print(ans)

Solution (Python):
N = int(input())
S = input()
left_w_count = 0
right_e_count = S[1:].count("E")
result = right_e_count
for i in range(1, N):
    if S[i - 1] == "W":
        left_w_count += 1
    if S[i] == "E":
        right_e_count -= 1
    r = left_w_count + right_e_count
    if r < result:
        result = r
print(result)

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

sum_e = s.count("E")
sum_w = s.count("W")

cnt_e, cnt_w = 0, 0

ans = 10 ** 6
for x in s:
    if x == "E":
        cnt_e += 1
        ans = min(ans, cnt_w + (sum_e - cnt_e))
    if x == "W":
        ans = min(ans, cnt_w + (sum_e - cnt_e))
        cnt_w += 1

print(ans)

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

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

ans = 10 ** 6
for i in range(n):
    if s[i] == "W":
        r = cumsum[-1][1] - cumsum[i][1]
        l = cumsum[i][0] - 1
        ans = min(ans, r+l)
    elif s[i] == "E":
        r = cumsum[-1][1] - cumsum[i][1]
        l = cumsum[i][0]
        ans = min(ans, r+l)

print(ans)

Solution (Python):
n = int(input())
s = input()
e = s.count("E")
w = s.count("W")
cnt1 = 0
cnt2 = 0
ans = 10e9
for i in s:
    if i=="E":
        cnt1 += 1
        ans = min(ans,cnt2+e-cnt1)
    else:
        ans = min(ans,cnt2+e-cnt1)
        cnt2 += 1
print(ans)

Solution (Python):
n=int(input())
s=input()
cnt=s[1:].count('E')
ans=cnt
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)

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

W = [0] * (N + 1)
w = 0

for index, i in enumerate(li):
  if i == 'W':
    w += 1
  W[index + 1] = w

newLi = reversed(li)

E = list(reversed(W))
e = 0

for index, i in enumerate(newLi):
  if i == 'E':
    e += 1
  E[index + 1] += e

print(min(E))

Solution (Python):
from itertools import accumulate
 
n = int(input())
s = [c == 'W' for c in input()]
l = accumulate([0] + s[:-1])
r = accumulate([0] + [not c for c in reversed(s[1:])])
print(min(e + w for e, w in zip(l, reversed(list(r)))))

Solution (Python):
n=int(input())
s=input()
w=0
et=s.count('E')
ans=float('INF')
for i in range(n):
    a=w
    if s[i]=='W':
        w+=1
    e=et-(i+1-w)
    ans=min(ans,a+e)
print(ans)

Solution (Python):

def Attention():
    N = int(input())
    S = input()
    ans = 300000

    Wcount = [0]
    Ecount = [0]
    cnt = 0
    for i in range(N):
        if S[i] == 'W':
            cnt += 1
        Wcount.append(cnt)
    cnt = 0
    for i in range(N):
        if S[(i + 1) * -1] == 'E':
            cnt += 1
        Ecount.append(cnt)

    for i in range(N):
        cnt = Wcount[i] + Ecount[(i + 2) * -1]
        ans = min(cnt, ans)
    print(ans)

Attention()

Solution (Python):
from itertools import accumulate

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

turn_E = [0] + list(accumulate([s=="W" for s in S[:-1]]))
turn_W = list(reversed(list(accumulate([s=="E" for s in S[:0:-1]])))) + [0]

ans = [e+w for e, w in zip(turn_E, turn_W)]
print(min(ans))

Solution (Python):
from itertools import accumulate
from operator import add
N = int(input())
S = input()

cnt_w = [0] * (N+1) 
for i in range(N): 
  if S[i] == "W":
    cnt_w[i+1] = cnt_w[i] + 1
  else:
    cnt_w[i+1] = cnt_w[i]
  
cnt_e = [0] * (N+1) 
for i in range(N-1, -1, -1):
  if S[i] == "E":
    cnt_e[i] = cnt_e[i+1] + 1
  else:
    cnt_e[i] = cnt_e[i+1]
    
ans = [w+e for w, e in zip(cnt_w, cnt_e)]
print(min(ans))

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

lsw = [0] * n
cntw = 0

lse = [0] * n
cnte = 0

for i in range(n):
    lsw[i] = cntw
    lse[n - 1 - i] = cnte

    if s[i] == "W":
        cntw += 1
    
    if s[n - 1 - i] == "E":
        cnte += 1

min = 3 * (10 ** 5)

for i in range(n):
    wa = lsw[i] + lse[i]
    if wa < min:
        min = wa

print(min)

Solution (Python):
from itertools import accumulate
from operator import add
N = int(input())
S = input()
E = list(accumulate("E"==s for s in "W"+S[:0:-1]))[::-1]
W = list(accumulate("W"==s for s in "E"+S[:-1]))
print(min(map(add, E, W)))

Solution (Python):
N = int(input())
S = input()
E = [0 for i in range(N)]
W = [0 for i in range(N)]
for i in range(N):
    if S[i] == 'E':
        E[i] = 1
    else:
        W[i] = 1
for i in range(1,N):
    E[i] += E[i-1]
for i in range(N-2,-1,-1):
    W[i] += W[i+1]
ans = [W[i]+E[i]-1 for i in range(N)]
print(N-1-max(ans))

Solution (Python):
N = int(input())
S = input()
num = [0 for _ in range(N)]

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

for i in range(1, N):
  diff = 0
  if S[i-1] == 'W':
    diff += 1
  if S[i] == 'E':
    diff -= 1
  num[i] = num[i-1] + diff
  
print(min(num))

Solution (Python):
from collections import Counter
N = int(input())
S = list(input())
c = Counter(S[1:])
ans = 0+c["E"]
now = S[0]
e = c["E"]
w = 0
for i in range(1,N):
    if now == "W":
        w += 1
    now = S[i]
    if now == "E":
        e -= 1
    if w+e < ans:
        ans = w+e
print(ans)

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

lft = [0]
rgt = [0]

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

cnt = 0
for i in range(1,n):
    if s[(i)*(-1)] == "E":
        cnt += 1
    rgt.append(cnt)

ans = 10**9
for i in range(n):
    ans = min(ans,lft[i]+rgt[(i+1)*(-1)])
print(ans)

Solution (Python):
N = int(input())
S = input()
x = 0
a = 0
idx = 0

for i in range(N):
    if S[i] == "W":
        x += 1
    else:
        x -= 1

    if x < a:
        a = x
        idx = i

w = S[0:idx].count("W")
e = S[idx + 1 : N].count("E")
print(w + e)

Solution (Python):
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)

Solution (Python):
from itertools import accumulate
n = int(input())
s = input()
aa = {'E':1, 'W':0}
e = list(map(aa.get, s))
aa = {'E':0, 'W':1}
w = list(map(aa.get, s))

ae = list(accumulate(e[::-1]))[::-1]
aw = list(accumulate(w))

ans = float('INF')
for i in range(1, n-1):
    if ans > ae[i+1] + aw[i-1]:
        ans = ae[i+1] + aw[i-1]
print(min([ans, ae[1], aw[-2]]))

Solution (Python):

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

all_W_E = [0, 0]
for s in S:
    if s == "W":
        all_W_E[0] += 1
    else:
        all_W_E[1] += 1

now_W = 0
now_E = 0
ans_list = [0] * N

for i in range(N):
    if S[i] == "W":
        ans_list[i] = (now_W + all_W_E[1] - now_E)
        now_W += 1
    else:
        ans_list[i] = (now_W + all_W_E[1] - now_E - 1)
        now_E += 1


print(min(ans_list))

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





    
    
    
    
        


minimum = 3000000
numEast = [0] * (N+1)
numWest = [0] * (N+1)
for i in range(N):
    if S[i] == 'E':
        numEast[i+1] = numEast[i] + 1
        numWest[i+1] = numWest[i]
    else:
        numEast[i+1] = numEast[i]
        numWest[i+1] = numWest[i] + 1

for i in range(N):
    tmpMin = numWest[i] + (numEast[N]-numEast[i+1])
    if tmpMin < minimum:
        minimum = tmpMin

print(minimum)

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


cumulative_e = [0] * (n+1)
cumulative_w = [0] * (n+1)
for i in range(1, n+1):
  if s[i-1] == 'W':
    cumulative_w[i] = cumulative_w[i-1] + 1
    cumulative_e[i] = cumulative_e[i-1]
  elif s[i-1] == 'E':
    cumulative_e[i] = cumulative_e[i-1] + 1
    cumulative_w[i] = cumulative_w[i-1]


result = [0] * n
for j in range(n):
  result[j] = cumulative_w[j] + ( cumulative_e[n] - cumulative_e[min(n,j+1)])

print(min(result))

Solution (Python):
n = int(input())
s = input()
cnt = [0]*n
cnt_e = [0]*n
cnt_w = [0]*n
e = 0
w = 0
for i in range(n):
    if s[i] == "E":
        e += 1
    else :
        w += 1
    cnt_e[i] = e
    cnt_w[i] = w

cnt[0] =  e - cnt_e[0]
for i in range(1,n):
    cnt[i] = cnt_w[i-1] + e - cnt_e[i]

print(min(cnt))

Solution (Python):
N = int(input())
S = list(input())
cnt = (S[1:]).count("E")
ans = cnt
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)

Solution (Python):

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

W = [0]
E = [0]

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


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

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

w_count = [0] * (n+1)
e_count = [0] * (n+1)

e = 0
w = 0
for i, s in enumerate(S):
    if s == 'E':
        e += 1
    elif s == 'W':
        w += 1

    w_count[i] = w
    e_count[i] = e

ans = 10**6
for i in range(n):
    w_on_my_left = w_count[i-1]
    e_on_my_right = e - e_count[i]
    candidate = w_on_my_left + e_on_my_right
    ans = min(ans, candidate)

print(ans)

Solution (Python):
import collections
n = int(input())
lst1 = list(input())
c = collections.Counter(lst1) 
right = c["E"]
left = 0
ans = 10**9

for i in range(n):
    if lst1[i] == "E":
        right -=1
    if i >= 1 and lst1[i-1] == "W":
        left += 1
    ans = min(ans,left+right)
    

print(ans)

Solution (Python):

import sys


def solve(N: int, S: str):
    
    accum_W = [0]*N 
    accum_E = [0]*N 

    for i in range(1,N):
        accum_W[i] = accum_W[i-1]+int(S[i-1]=="W")

    for i in range(N-2,-1,-1):
        accum_E[i] = accum_E[i+1] + int(S[i+1] == "E")
    
    answer = N
    for i in range(N):
        a = accum_W[i]+accum_E[i]
        answer = min(answer,a)
    print(answer)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    S = next(tokens)  
    solve(N, S)

if __name__ == '__main__':
    main()

Solution (Python):
import sys

n = int(sys.stdin.readline().rstrip())
s = sys.stdin.readline().rstrip()

def main():
    l = 0
    r = s.count('E')
    res = r
    for i in range(n):
        cur = s[i]
        if cur == 'E':
            r -= 1
        res = min(res, l + r)
        if cur == 'W':
            l += 1
    return res

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