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

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

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

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

print(ans)

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

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

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

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

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

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

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

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

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

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

print(min(ans_list))

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

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

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

print(min(ans_list) + e_cnt)

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

print(Ans)

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

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

Solution (Python):
n = int(input())
s = input()
e = s.count("E")
w = n - e
ans = 10**9
if s[0] == "W":
    cnt = e
else:
    cnt = e-1
num = cnt

for i in range(n-1):
    if s[i] == s[i+1] and s[i] == "E":
        cnt -= 1
    elif s[i] == s[i+1]:
        cnt += 1
    
    ans = min(ans,cnt,num)
    ans = max(ans,0)
print(max(ans,0))

Solution (Python):








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








































num_E = [0] * N
num_W = [0] * N
max_num = 0
leader_index = 0
for i, c in enumerate(S):
    if c == "W":
        num_W[i] = num_W[i - 1] + 1
        num_E[i] = num_E[i - 1]
    else:
        num_W[i] = num_W[i - 1]
        num_E[i] = num_E[i - 1] + 1
    tmp = num_E[i] - num_W[i]
    if max_num < tmp:
        leader_index = i
        max_num = tmp

print(S[:leader_index].count("W") + S[leader_index + 1:].count("E"))

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

e_count = 0
for s in S:
  if s == 'E':
    e_count += 1

min_inv = float('infinity')
    
for s in S:
  if s == 'E':
    e_count -= 1
  
  min_inv = min(e_count, min_inv)
  
  if s == 'W':
    e_count += 1

print(min_inv)

Solution (Python):
n = int(input())
s = input()
ecounter = [0 for _ in range(n)]
wcounter = [0 for _ in range(n)]
ecounter[0] = int(s[0]=='E')
wcounter[n-1] = int(s[n-1]=='W')
ans = n
for i in range(n-1):
    ecounter[i+1] = ecounter[i] + (s[i+1]=='E')
    wcounter[n-2-i] = wcounter[n-1-i] + (s[n-2-i]=='W')
for j in range(n):
    cnt = n - (ecounter[j]+wcounter[j])
    ans = min(cnt,ans)
print(ans)

Solution (Python):


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

W = [0] * N
E = [0] * N

W_pre = E_pre = 0
for i in range(N):
    if S[i] == "W":
        W[i] = W_pre + 1
        E[i] = E_pre
    else:
        W[i] = W_pre
        E[i] = E_pre + 1
    W_pre = W[i]
    E_pre = E[i] 

ans = N
for i in range(N):
    if i>0:
        res1 = W[i-1]
    else:
        res1 = 0
    res2 = E[N-1]-E[i]
    
    ans = min(ans,res1+res2)

print(ans)

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

dp = [0] * n

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

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

print(min(dp))

Solution (Python):

"""
Created on Sat Sep 19 20:18:57 2020

@author: liang
"""

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

tmp = 0

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

ans = 10**6
for i in range(N):
    if ans > tmp:
        ans = tmp
    if i == N-1:
        break
    if S[i] == "W":
        tmp += 1
    if S[i+1] == "E":
        tmp -= 1
    
        
print(ans)

Solution (Python):
n = int(input())
s = input()
e = [0] * n
w = [0] * (n + 1)
if s[0] == 'E':
        e[0] = 1
else:
        e[0] = 0
w[0] = 0
if s[0] == 'W':
        w[1] = 1
else:
        w[1] = 0
for i in range(1, n):
    if s[i] == 'E':
        e[i] = e[i - 1] + 1
        w[i + 1] = w[i]
    else:
        e[i] = e[i - 1]
        w[i + 1] = w[i] + 1
min = float("inf")
for i in range(n):
    if min > e[-1] - e[i] + w[i]:
        min = e[-1] - e[i] + w[i]
print(min)

Solution (Python):
n = int(input())
s = input()
e = [0] * n
w = [0] * (n + 1)
e[0] = 1 if s[0] == 'E' else 0
w[0] = 0
w[1] = 1 if s[0] == 'W' else 0
for i in range(1, n):
    if s[i] == 'E':
        e[i] = e[i - 1] + 1
        w[i + 1] = w[i]
    else:
        e[i] = e[i - 1]
        w[i + 1] = w[i] + 1
min = float("inf")
for i in range(n):
    if min > e[-1] - e[i] + w[i]:
        min = e[-1] - e[i] + w[i]
print(min)

Solution (Python):
n=int(input())
s=input()
x=[0]*n
t=0
for i in range(n):
    if s[i]=="E":
        t+=1
    x[i]=t
M=x[n-1]
c=0
ans=10**9
for i in range(n):
    ans=min(ans,i-c+M-x[i])
    c=x[i]
print(ans)

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-2, -1, -1):
    r[i] = r[i+1] + (s[i+1] == "E")
ans = n-1
for i in range(n):
    ans = min(ans, l[i]+r[i])
print(ans)

Solution (Python):
def count(left_w, right_e):
    return left_w + right_e


def main():
    n = int(input())
    s = str(input())

    w_count = s.count('W')
    e_count = n - w_count

    lst = []
    left = 0
    left_w = 0
    right = n
    right_e = e_count
    for i in range(n):
        s_alp = s[i]
        right -= 1
        if s_alp == 'E':
            right_e -= 1

        lst.append(count(left_w, right_e))

        left += 1
        if s_alp == 'W':
            left_w += 1

    minimum = min(lst)
    print(minimum)


if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
S=input()
RS=S[::-1]

west=[0]
east=[0]

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

for i in range(N):
  if RS[i]=="E":
    east.append(east[-1]+1)
  else:
    east.append(east[-1])
east.append(0)
east=east[::-1]

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

print(ans)

Solution (Python):

url = "https://atcoder.jp//contests/abc098/tasks/arc098_a"

def main():
    input()
    s = input()
    w_count = s.count('W')
    right_we = {"W": w_count, "E": len(s) - w_count}
    left_we = {"W": 0, "E": 0}
    ans = 10000000
    for v in s:
        right_we[v] -= 1
        ans = min(ans, right_we["E"] + left_we["W"])
        left_we[v] += 1
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
EW=0
ans=99999999999999999999999999999999

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

for i in range(N):
    if i==0:
        pass
    elif S[i]=="E":
        EW+=1
if ans>EW:
    ans=EW

for j in range(1,N):
    if S[j]=="E":
        EW-=1
    if S[j-1]=="W":
        EW+=1

    if ans>EW:
        ans=EW
print(ans)

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

A = [0]*N
B = [0]*N

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

print(min(map(lambda x: x[0]+x[1], zip(A, B))))

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

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

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

Solution (Python):

import sys


def solve(N: int, S: str):
    w = [0] * N
    e = [0] * N
    wt = 0
    et = 0
    a = [0] * N
    for i in range(N):
        s = S[i]
        w[i] = wt
        if s == "W":
            wt += 1
        if s == "E":
            et += 1
        e[i] = et
    for i in range(N):
        a[i] = w[i] + et - e[i]
    print(min(a))




    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 math
from math import gcd,pi,sqrt
INF = float("inf")

import sys
sys.setrecursionlimit(10**6)
import itertools
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]


def main():
    n = i_input()
    s = input()

    ans = 0
    for i in s[1:]:
        if i == "E":
            ans += 1
    l = [ans]

    for i in range(n-1):
        trial = 0
        if s[i] == "W":
            trial += 1
        if s[i+1] == "E":
            trial -= 1
        ans += trial
        l.append(ans)

    print(min(l))


if __name__=="__main__":
    main()

Solution (Python):
N = int(input())
S = input()
x = S.count('E') - (1 if S[0] == 'E' else 0)
ans = x
for s1, s2 in zip(S, S[1:]):
    if s1 == 'W':
        x += 1
    if s2 == 'E':
        x -= 1
    ans = min(ans, x)
print(ans)

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

Solution (Python):
def schange(s):
    if s=="E":
        return 1
    else:
        return 0

n = int(input())
s = list(map(schange,input()))
ssum = [0]*(n+1)

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

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

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

row = [0]*(N+2)
for i in range(N):
    if S[i] =='E':
        row[i+1] = row[i] + 1
    else:
        row[i+1] = row[i]
answer = [0]*N
for i in range(N):
    answer[i] += (i - row[i])
    a = row [N] - row[i+1]
    answer[i] += a



print(min(answer))

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
S = list(input())
from itertools import accumulate

Es = [0]*(N+1)
Ws = [0]*(N+1)
for i, s in enumerate(S, 1):
    if s=='E':
        Es[i] = 1
    else:
        Ws[i] = 1
Es = list(accumulate(Es))
Ws = list(accumulate(Ws))

ans = 10**18
for i in range(1, N+1):
    w = Ws[i-1]
    e = (Es[-1]-Es[i])
    ans = min(ans, w+e)
print(ans)

Solution (Python):
n = int(input())
s = input()
s_w = [0] * (n)
s_e = [0] * (n)
ans = [0] * (n)

t = 0
for i in range(n-1):
    if s[i] == "W":
        t += 1
    s_w[i+1] += t
t = 0
for i in range(1,n):
    
    if s[-i] == "E":
        t += 1
    s_e[i] += t
s_e.reverse()

for i in range(n):
    ans[i] += s_w[i] + s_e[i]

print(min(ans))

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

E = [0]*(n)
eCount = 0
for i in range(n):
    E[i] = eCount
    if s[i] == 'W':
        eCount += 1

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

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

print(ans)

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


cnt = s.count('E')

ans = cnt
for i in range(n):
    if s[i] == 'E':
        cnt -= 1
        if ans > cnt:
            ans = cnt
    
    else:
        cnt += 1

print(ans)

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


cnt = s.count('E')

ans = cnt
for i in range(n):
    if s[i] == 'E':
        cnt -= 1
        if ans > cnt:
            ans = cnt
    
    else:
        cnt += 1

print(ans)

Solution (Python):
n=int(input())
s=list(input())
sw=[0]
se=[0]
swc=0
sec=0
for i in range(n):
    if s[i]=='W':
        swc+=1
    else:
        sec+=1
    sw.append(swc)
    se.append(sec)
m=n
for i in range(n+1):
    m=min(m,sw[i]+se[-1]-se[i])
print(m)

Solution (Python):
n=int(input())
s=list(input())
ans=0
for i in range(1,n):
    if s[i] == "E":
        ans += 1
        
ans_=ans
for i in range(1,n):
    if s[i-1] == "W":
        ans_+= 1
    if s[i] == "E":
        ans_+= -1
    ans = min(ans,ans_)
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-1] == "W":
    cnt += 1
  if S[i] == "E":
    cnt -= 1
  ans = min(ans, cnt)
  
print(ans)

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

Solution (Python):
N = int(input())
S = list(input())
allW = S.count('W')
allE = S.count('E')
numW = S.count('E')
numE = 0
ans = 3*10**5
for i in range(N):
    if S[i] == 'E':
        numW -= 1
    ans = min(ans,numW + numE)
    if S[i] == 'W':
        numE += 1   
print(ans)

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

sl_E = []
sl_W = []
for i, s in enumerate(S):
    E = 1 if s == "E" else 0
    W = 1 if s == "W" else 0
    s_E = sl_E[i - 1] + E if i != 0 else E
    s_W = sl_W[i - 1] + W if i != 0 else W
    sl_E.append(s_E)
    sl_W.append(s_W)

_min = sl_E[-1]
for i in range(1, N):
    count = sl_W[i - 1]
    count += sl_E[-1] - sl_E[i]
    if count < _min:
        _min = count

print(_min)

Solution (Python):

n = int(input())
A =input()
e = A.count('E')
cnt = e

for i in A:
    if i == 'E':
        cnt -= 1
    else:
        cnt += 1
    e = min(e,cnt)

print(e)

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

ans = []
t = [0]*(N+1)

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

for j in range(N):
    ans.append(j + t[N] - t[j]  - t[j+1])

print(min(ans))

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

dp_w=[0]
dp_e=[0]

for i in range(n):
    if i!=0:
        dp_w.append(dp_w[-1])
        dp_e.append(dp_e[-1])
    if s[i]=="W":
        dp_w[-1]+=1
    else:
        dp_e[-1]+=1

def migi(i):
    if i==n-1:
        return 0
    return dp_e[n-1]-dp_e[i]
def hidari(i):
    if i==0:
        return 0
    return dp_w[i-1]
ans=1000000000000000
for i in range(n):
    right=migi(i)
    left=hidari(i)
    ans=min(right+left,ans)
print(ans)

Solution (Python):
inf = float('inf')
n = int(input())
s = input()

right_e = s.count("E")
left_w = 0
cnt = inf

for e in s:
  if e == "W":
    cnt = min(cnt, right_e + left_w)
    left_w += 1
  else:
    right_e -= 1
    cnt = min(cnt, right_e + left_w)
print(cnt)

Solution (Python):
N = int(input())
S = input().rstrip()
A, B, c1, c2 = [], [], 0, 0
for s in S:
    if s=="W": c1 += 1
    A.append(c1)
for s in reversed(S):
    if s=="E": c2 += 1
    B.append(c2)
B.reverse()
res = float("inf")
A += [0]
B += [0]
for i in range(N):
    res = min(A[i-1] + B[i+1], res)
print(res)

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

Solution (Python):
import math
import sys
import os
from operator import mul
import numpy as np

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")


N = I()
S = list(_S())
ans = N

Sn = np.array(S)
S_cum = np.zeros(N+1,dtype='int')
S_cum[1:] = np.cumsum(Sn=='W')

Snr=np.flip(Sn, 0)
S_cumr = np.zeros(N+1,dtype='int')
S_cumr[1:] = np.cumsum(Snr=='E')




for i in range(N):
    attention = S_cum[i] + S_cumr[N-(i+1)]
    
    ans = min(ans, attention)

print(ans)

Solution (Python):
n=int(input())
s=list(input())
w=s.count("W")
e=s.count("E")
x=[0 for i in range(n)]
for i in range(1,n):
    if s[i-1]=="W":
        x[i]=x[i-1]+1
    else:
        x[i]=x[i-1]
ans=n
for i in range(n):
    z=2*x[i]+e-i
    if s[i]=="E":
        z-=1
    ans=min(ans,z)
print(ans)

Solution (Python):
N = int(input())
s = input()
mn=N
lw=0
re=s[1:].count("E")
for i in range(N):
    if i-1>=0 and s[i-1]=="W":
        lw+=1
    if i-1>=0 and s[i]=="E":
        re-=1
    
    sm=lw+re
    if mn>sm:
        mn=sm
print(mn)

Solution (Python):
n = int(input())
s = input()
res = n
e = s.count("E")

l_w = 0
l_e = 0
for i in range(n):
    if i == n - 1 and s[i] == "E":
        l_e += 1
    tmp = l_w + (e - l_e)
    res = min(tmp, res)
    if s[i] == "W":
        l_w += 1
    else:
        l_e += 1
print(res)

Solution (Python):
n = int(input())
s = input()
res = n
w = 0
e = 0
for i in range(n):
    if s[i] == "W":
        w += 1
    else:
        e += 1

l_w = 0
l_e = 0
for i in range(n):
    if i == n - 1 and s[i] == "E":
        l_e += 1
    tmp = l_w + (e - l_e)
    res = min(tmp, res)
    if s[i] == "W":
        l_w += 1
    else:
        l_e += 1
print(res)

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

w = [0]*n
e = [0]*n

if s[0] == "W": w[0] += 1
else: 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

ans = float("inf")
if e[n-1]-e[0] < ans: ans = e[n-1]-e[0]
if w[n-1] < ans: ans = w[n-1]

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

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

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

Solution (Python):


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

total_E = s.count('E')
total_W = n-total_E

right_E = []
tmp_E = total_E
tmp_W = 0 
for i in range(n):
    if s[i] == 'E':
       tmp_E -= 1 
    right_E.append(tmp_E)

left_W = [0] 
for i in range(1,n):
    if s[i-1] == 'W':
       tmp_W += 1 
    left_W.append(tmp_W)

candidates = []
for i in range(n):
    candidates.append(right_E[i]+left_W[i])




ans = min(candidates)
print(ans)

Solution (Python):
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
from math import gcd,ceil,floor,factorial

from functools import reduce
from pprint import pprint
from statistics import mean,median,mode

INF = float("inf")

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

def I():
    return int(input())

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

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

def CI(n):
    return [ int(input()) for _ in range(n) ]

def LI(n):
    return [ list(map(int,input().split())) for _ in range(n) ]

def S():
    return input()

def MS():
    return input().split()

def RS():
    return list(input())

def CS(n):
    return [ input() for _ in range(n) ]

def LS(n):
    return [ list(input()) for _ in range(n) ]







n = I()
s = RS()

cw = 0
lw = []
for i in range(n):
    if s[i]=='W':
        cw += 1
        lw.append(cw)
    else:
        lw.append(cw)


sr = list(reversed(s))

ce = 0
le = []
for i in range(n):
    if sr[i]=='E':
        ce += 1
        le.append(ce)
    else:
        le.append(ce)
le = list(reversed(le))


ls = []
for i in range(n):
    k = lw[i] + le[i] - 1
    ls.append(k)


ans = min(ls)
print(ans)

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

dp_f=[0 for _ in range(N)]
dp_b=[0 for _ in range(N)]
for n in range(1,N):
    if S[n-1]=="W":
        tmp=1
    else:
        tmp=0
    dp_f[n]=dp_f[n-1]+tmp

for n in reversed(range(N-1)):
    if S[n+1]=="E":
        tmp=1
    else:
        tmp=0
    dp_b[n]=dp_b[n+1]+tmp

MIN=N
for n in range(N):
    if n==N-1:
        MIN=min(MIN,dp_f[N-1])
    elif n==0:
        MIN=min(MIN,dp_b[0])
    else:
        MIN=min(MIN,dp_f[n]+dp_b[n])
print(MIN)

Solution (Python):
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
S = [_ for _ in input()]

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

    ans = min(ans, left + right)

print(ans)

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N = INT()
    S = INPUT()

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

        ans = min(ans, left + right)

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
S = input()
west = S.count('W')
east = N - west 
Q = [0,0,west,east] 
ans = N-1
for i in range(N):
    if S[i]=='W':
        Q[2]-=1
        cnt = Q[0]+Q[3]
        Q[0]+=1
    else:
        Q[3]-=1
        cnt = Q[0]+Q[3]
        Q[1]+=1
    ans = min(ans,cnt)
print(ans)

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

Solution (Python):
def main():
    N = int(input())
    S = input()
    lst = [0] * N
    ans = N

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

    for i in range(N):
        if i == 0:
            left = 0
        else:
            left = i - lst[i - 1]
        right = lst[N - 1] - lst[i]

        if left + right < ans:
            ans = left + right

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):




import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """5
WEEWW"""
        output = """1"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """12
WEWEWEEEWWWE"""
        output = """4"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """8
WWWWWEEE"""
        output = """3"""
        self.assertIO(input, output)


def resolve():
    N = int(input())
    S = list(input())











    W = [0] * N
    E = [0] * N
    L = 0
    R = 0

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

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


if __name__ == "__main__":
    
    resolve()

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

e_all = s.count("E")
w_all = s.count("W")
e_l = 0
w_l = 0

unti = [0] * n

for i in range(n):
    if s[i] == "E":
        e_l += 1
        unti[i] = w_l + (e_all - e_l) 
    if s[i] == "W":
        unti[i] = w_l + (e_all - e_l) 
        w_l += 1



print(min(unti))

Solution (Python):
N = int(input())
S = input()
e = S.count("E")
cnt = e
for i in S:
    if i == "E":
        cnt -= 1
    else:
        cnt += 1
    e = min(e, cnt)
print(e)

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]=="E" and s[i]=="E":
    a[i]=a[i-1]-1
  if s[i-1]=="W" and s[i]=="E":
    a[i]=a[i-1]
  if s[i-1]=="E" and s[i]=="W":
    a[i]=a[i-1]
  if s[i-1]=="W" and s[i]=="W":
    a[i]=a[i-1]+1
print(min(a))

Solution (Python):
n=int(input())
s=list(input())
t=[]
for i in s:
  if i=="W":
    t.append(1)
  else:
    t.append(0)

from itertools import accumulate
t=[0]+list(accumulate(t))
ans=float("INF")
for i in range(1,n+1):
  ans=min(ans,t[i-1]+n-i-(t[n]-t[i]))

print(ans)

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

ans = [0]*(n+1)
for i in range(n):
  if s[i] == 'E':
    ans[i+1] = ans[i] + 1
  else:
    ans[i+1] = ans[i]
    
print(min(i-ans[i]+ans[n]-ans[i+1] for i in range(n)))

Solution (Python):


from itertools import accumulate
n = int(input())
s = [0 if i == "E" else 1 for i in str(input())]

s_cumsum = list(accumulate(s))
s_cumsum_rev = list(accumulate(reversed(s)))

ans = 10**10

for i in range(1, n-1):

    a_1 = s_cumsum[i-1]
    a_0 = i - a_1
    b_1 = s_cumsum_rev[-2-i]
    b_0 = (n-1-i) - b_1

    
    ans = min(ans, a_1+b_0)

ans = min(ans, s_cumsum[-2])
ans = min(ans, (n-1)-s_cumsum_rev[-2])
print(ans)

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