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().rstrip('\n'))
cumsum = [[0, 0] for i in range(n)]
cnt_1 = 0
cnt_2 = 0
cnt = [-1] * n
for i in range(n):
    if s[i] == 'E':
        cnt_1 += 1
    else:
        cnt_2 += 1
    cumsum[i][0] = cnt_1
    cumsum[i][1] = cnt_2
for i in range(n):
    if s[i] == 'E':
        cnt[i] += 1
    cnt[i] += cumsum[n - 1][0] - cumsum[i][0] + cumsum[i][1]
print(min(cnt))

Solution (Python):
from collections import Counter

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

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

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

ret = sum(s == 'E' for s in S[1:]) 
variation = ret
for i in range(N-1):
    if S[i] == 'W':
        variation += 1
    if S[i+1] == 'E':
        variation -= 1
    if ret > variation:
        ret = variation
        
print(ret)

Solution (Python):
from collections import Counter

N = int(input())
S = input()
count = Counter(S)
min_ = count['E'] if S[0]=='W' else count['E']-1
total = min_
for i in range(1,N):
    plus = 1 if S[i-1]=='W' else 0
    plus += -1 if S[i]=='E' else 0
    total += plus
    if total < min_:
        min_ = total
print(min_)

Solution (Python):
n = int(input())
s = input()
ans = 1001001001
ll = [0]
rl = [0]
l = 0
r = 0
for i in range(1, len(s)):
    if s[i - 1] == 'W':
        l += 1
    ll.append(l)
    if s[-i] == 'E':
        r += 1
    rl.append(r)
rl.reverse()




for a, b in zip(ll, rl):
    ans = min(ans, a + b)
print(ans)

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

ws = []
count = 0
for v in s:
  ws.append(count)
  if v == 'W':
    count += 1
  

count = 0
es = []
for v in s[::-1]:
  es.append(count)
  if v == 'E':
    count += 1

  
es = list(reversed(es))
ans = 10**9
for w,e in zip(ws,es):
  ans = min(ans, w+e)
  
print(ans)

Solution (Python):
n, S = int(input()), input().strip()

scores = [sum(c == 'E' for c in S[1:])]
for i in range(1, n):
    scores.append(scores[i-1] + (S[i-1]=='W') - (S[i]=='E'))
print(min(scores))

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

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

ans = 1e9
for i in range(N):
  turn_w = cusum_w[i]
  turn_e = cusum_e[N] - cusum_e[i+1]
  ans = min(ans, turn_w + turn_e)
  

print(ans)

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

W = list(range(N))
E = list(range(N))
w, e = 0, 0

if S[0] == 'W':
    W[0] = 1
    E[0] = 0
else:
    W[0] = 0
    E[0] = 1

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

    else:
        W[i] = W[i-1]
        E[i] = E[i-1] + 1

min_cost = float('inf')
for i in range(N):
    cost = 0
    if i-1 != 0:
        cost += W[i-1]
    cost += E[N-1] - E[i]
    min_cost = min(min_cost, cost)
    

print(min_cost)

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

W = 0
E = A.count("E")
ans = n+1

for i in A:
    if i == "W":
        W += 1
    else:
        E -= 1
    if W + E < ans:
        ans = W + E
print(min(ans, A.count("E"), A.count("W")))

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

left=[0]*N
right=[0]*N
to_change=[0]*N

if S[0]=="E":
    right[0]=1
else:
    left[0]=1

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

to_change[0]=right[-1]-right[0]
to_change[-1]=left[-2]

for j in range(1,N-1):
    to_change[j]=(right[-1]-right[j])+(left[j-1])

print(min(to_change))

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

print(ans)

Solution (Python):
import numpy as np
n = int(input())
s = list(input())
for i in range(n):
    if s[i] == 'E':
        s[i] = 0
    else:
        s[i] = 1
cs = np.cumsum(s)
minturn = n
for i in range(n):
    if i == 0:
        turn = n-cs[n-1]+cs[0]-1
    else:
        turn = cs[i-1]+n-i-cs[n-1]+cs[i]-1
    minturn = min(turn,minturn)
print(minturn)

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

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

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

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

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

ans = s[1:].count("E")
tmp = ans
for i in range(1, n):
    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 = list(input())

we = []
west = 0
east = 0
for i in range(N):
    if S[i] == "W":
        west += 1
    elif S[i] == "E":
        east += 1
    we.append([west,east])

ans = 1e9
for i in range(N):
    if i == 0:
        left = 0
    else:
        left = we[i][0]
    right = we[N-1][1] - we[i][1]
    ans = min(ans,left+right)

print(ans)

Solution (Python):
N=int(input())
S=input()
data=[0 for i in range(0,N)]
for i in range(1,N):
    if S[i]!='W':
        data[0]+=1
for i in range(1,N):
    x=0
    y=0
    if S[i-1]!='E':
        x=1
    if S[i]!='W':
        y=1
    data[i]=data[i-1]+x-y
print(min(data))

Solution (Python):
from collections import Counter
n = int(input())
s = input()
c = Counter(s)
tmp = c['E']
best = tmp - (1 if s[0] == 'E' else 0)
for i in range(1, n):
    if s[i - 1] == 'W':
        tmp += 1
    else:
        tmp -= 1
    if tmp - (1 if s[i] == 'E' else 0) < best:
        best = tmp - (1 if s[i] == 'E' else 0)
print(best)

Solution (Python):
import os
import sys

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


N = int(sys.stdin.buffer.readline())
S = sys.stdin.buffer.readline().decode().rstrip()

S = np.array(list(S), dtype='U1')
wc = (S == 'W').cumsum()
ec = (S == 'E')[::-1].cumsum()[::-1]

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

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

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

ans=wcnt
for i in range(N-1):
    if S[i] == "E" and S[i+1]=="E":
        wcnt-=1
    if S[i] == "W" and S[i+1]=="W":
        wcnt+=1
    if ans > wcnt:
        ans=wcnt
print(ans)

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

wnums = [0]
enums = [0]
for i in range(0, N-1):
    if S[i] == 'W':
        wnums.append(wnums[i]+1)
    else:
        wnums.append(wnums[i])

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

enums.reverse()

ans = 10**9
for i in range(N):
    num = wnums[i] + enums[i]
    if ans > num:
        ans = num
print(ans)

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

wnums = [0]
enums = [0]
for i in range(0, N-1):
    if S[i] == 'W':
        wnums.append(wnums[i]+1)
    else:
        wnums.append(wnums[i])

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

ans = 10**9
for i in range(N):
    num = wnums[i] + enums[N-i-1]
    if ans > num:
        ans = num
print(ans)

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

def check_l(x):
    if x=='W':
        r = 1
    else:
        r = 0
    return r
def check_r(x):
    if x=='E':
        r = 1
    else:
        r = 0
    return r
    
for i in range(1,n):
    lm[i+1] = lm[i] + check_l(s[i-1])
for i in range(n-1,0,-1):
    rm[i] = rm[i+1] + check_r(s[i])
ans = float('inf')
for i in range(1,n+1):
    ans = min(ans,lm[i]+rm[i])
print(ans)

Solution (Python):
N = int(input())
S = input()
CW = [0]*N
CS = [0]*N
cw = 0
cs = 0
for i in range(N-1):
    if S[i] == 'W':
        cw += 1
    CW[i + 1] = cw
    if S[N - i - 1] == 'E':
        cs += 1
    CS[N - i - 2] = cs

C = [0]*N
for i in range(N):
    C[i] = CW[i] + CS[i]
ans = min(C)

print(ans)

Solution (Python):

import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def roundUp(a, b):
        return -(-a // b)

    @staticmethod
    def toUpperMultiple(a, x):
        return Math.roundUp(a, x) * x

    @staticmethod
    def toLowerMultiple(a, x):
        return (a // x) * x

    @staticmethod
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

    @staticmethod
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


MOD = int(1e09) + 7


def main():
    
    N = Scanner.int()
    S = Scanner.string()
    e, w = 0, 0
    e = S.count("E")
    ans = int(1e09)
    for i in range(N):
        if S[i] == "E":
            e -= 1
        ans = min(ans, e+w)
        if S[i] == "W":
            w += 1
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
s = input().strip()
d = {"W":0,"E":0}
x=[]
for i, c in enumerate(s):
    x.append(d["W"])
    d[c]+=1
d = {"W":0,"E":0}
r = N
for i, c in enumerate(s[::-1]):
    r = min(r, x[N-i-1]+d["E"])
    d[c]+=1
print(r)

Solution (Python):
import sys
import math
import functools
stdin = sys.stdin
mod = 10**9 + 7

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
sa = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]

n = ni()
s = ns()

now = s[1:].count("E")
ans = now
for spre, si in zip(s[:-1], s[1:]):
    if spre == "W":
        now += 1
    if si == "E":
        now -= 1
    ans = min(ans, now)

print(ans)

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

Solution (Python):
def examC():
    N = I()
    S = SI()
    E = S.count("E"); W = N-E
    curW = 0; curE = 0
    ans = N-1
    for i in range(N):
        if S[i]=="W":
            curW +=1
        cur = N -1 - (W-curW) - curE
        if S[i]=="E":
            curE +=1
        ans = min(ans,cur)

    print(ans)
    return

def examD():
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,ALPHABET
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]
ALPHABET = [chr(ord('A') + i) for i in range(26)]

if __name__ == '__main__':
    examC()

Solution (Python):
N = int(input())
S = input()
L = [0] * N
e = 0

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

ans = min(L) + e
print(ans)

Solution (Python):
from itertools import accumulate
 
N = int(input())
S = list(input())
 
A = [0]*(N+1) 
 

for i in range(1, N+1):
    j = 0
    if S[i-1] == "W":
        j = 1
    A[i]=A[i-1] +j


 

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

Solution (Python):
n = int(input())
s = input()
e_ls = [0]
w_ls = [0]
cnt = 0
for _s in s[:-1]:
    if _s == "E":
        cnt += 1
    e_ls.append(cnt)

cnt = 0
for _s in s[::-1][:-1]:
    if _s == "W":
        cnt += 1
    w_ls.append(cnt)
w_ls.reverse()

print(n - max([e + w for e, w in zip(e_ls, w_ls)]) - 1)

Solution (Python):
N = int(input())
S = input()
lw = [0] * N
re = [0] * N
for i in range(1, N):
  lw[i] = lw[i-1] + (S[i-1] == "W")
  re[-i-1] = re[-i] + (S[-i] == "E")
ans = 10**6
for i in range(N):
  ans = min(ans, lw[i] + re[i])
print(ans)

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

Solution (Python):
n=int(input())
S=input()
w,e='W','E'
if e not in S or w not in S:print(0);exit()
ecnt=len([1 for i in list(S) if i==e])
p0,p1=0,ecnt-(S[0]==e)
ans=p0+p1
p=S[0]
for i in S[1:]:
  p0,p1=(p0+(p==w),p1-(i==e))
  
  ans=min(ans,p0+p1)
  p=i
print(ans)

Solution (Python):
n=int(input())
S=input()
w,e='W','E'
if e not in S or w not in S:print(0);exit()
ecnt=len([1 for i in list(S) if i==e])
p0,p1=0,ecnt-int(S[0]==e)
ans=p0+p1
p=S[0]
for i in S[1:]:
  p0,p1=(p0+int(p==w),p1-int(i==e))
  
  ans=min(ans,p0+p1)
  p=i
print(ans)

Solution (Python):
n=int(input())
S=input()
w,e='W','E'
if e not in S or w not in S:print(0);exit()
ecnt=len([1 for i in list(S) if i==e])
p0,p1=0,ecnt-int(S[0]==e)
ans=p0+p1
for i in range(1,n):
  p0,p1=(p0+int(S[i-1]==w),p1-int(S[i]==e))
  
  ans=min(ans,p0+p1)
print(ans)

Solution (Python):
n=int(input())
S=input()
w,e='W','E'
if e not in S or w not in S:print(0);exit()
ecnt=len([1 for i in list(S) if i==e])
l=[0 for i in range(n)]
l[0]=(0,ecnt-int(S[0]==e))
p0,p1=l[0]
for i in range(1,n):
  l[i]=(p0+int(S[i-1]==w),p1-int(S[i]==e))
  
  p0,p1=l[i]
print(min([a+s for a,s in l]))

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

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

print(ans)

Solution (Python):
n = int(input())
s = list(input())
e = 0
for i in s:
    if i == 'E':
        e += 1
left = 0
right = e
ans = left + right
for i in range(n):
    if s[i] == 'E':
        right -= 1
    ans = min(ans,left+right)
    if s[i] == 'W':
        left += 1
print(ans)

Solution (Python):
from itertools import accumulate
n = int(input())
s = str(input())
tt = [1 if i  == "W" else 0 for i in s]
B = [0] + tt
B = list(accumulate(B))
ans = float('Inf')


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

Solution (Python):
from itertools import accumulate
n = int(input())
s = str(input())
tt = [1 if i  == "W" else 0 for i in s]
B = [0] + tt
B = list(accumulate(B))

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

Solution (Python):
def main():
    N = int(input())
    S = list(input())
    S = list(map(lambda x: 0 if x == "W" else 1, S))

    E = 0
    W = sum(S[1:N])

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

    print(p)


main()

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

West=[0]
East=[0]
for i in range(1,N+1):
    if S[i-1]=="W":
        West.append(West[-1]+1)
        East.append(East[-1])
    else:
        West.append(West[-1])
        East.append(East[-1]+1)
ans=N
for j in range(N):
    p=West[j]+East[-1]-East[j+1]
    if ans>p:
        ans=p
print(ans)

Solution (Python):
from itertools import accumulate

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

A = [0]*(N+1) 


for i in range(1, N+1):
    j = 0
    if S[i-1] == "W":
        j = 1
    A[i]=A[i-1] +j




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

print(ans)

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

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

ans = 10**9
for w,e in zip(west, east[::-1]):
    if ans > w + e:
        ans = w+e
print(ans)

Solution (Python):
from collections import Counter

n=int(input())
s=['n']+list(input())
ans=10**6
c=Counter(s)
le,lw=0,0
for i in s:
    if i=='E':
        le+=1
    elif i=='W':
        lw+=1
    ans=min(ans,lw+c['E']-le)
print(ans)

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

w=[0]*(N+1)
e=[0]*(N+1)
for i,s in enumerate(S):
    w[i+1]+=w[i]
    if s=="W": w[i+1]+=1
    e[i+1]+=e[i]
    if s=="E": e[i+1]+=1
ans=N
for i in range(N):
    now = w[i]+(e[N]-e[i+1])
    if now<ans: ans=now
print(ans)

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

A=[[0]*(N+1) for i in range(3)]

C=0
D=0
for i in range (1,N+1):
    if(S[i-1]=="W"):
        D+=1
    A[0][i]=D

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

for i in range (N+1):
    A[2][i]=A[1][i]+A[0][i]


print(min(A[2]))

Solution (Python):
n = int(input())
s = input()
A = [0 for i in range(n+2)]
A2 = [0 for i in range(n+2)]
for i in range(0,n):
  ndA = i +1
  ndA2 = n-i
  A[ndA] = A[ndA-1] + 1 if s[i]=="W" else A[ndA-1]
  A2[ndA2] = A2[ndA2+1] + 1 if s[n-i-1]=="E" else A2[ndA2+1]
A[n+1]=A[n]
A2[0]=A2[1]


print(min([A[i-1]+A2[i+1] for i in range(1,n+1)]))

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

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

Solution (Python):

import sys


def solve(N: int, S: str):
    C = {'E': 0, 'W': 0}
    D = {'E': 0, 'W': 0}
    for c in S:
        C[c] += 1
    a = 1e20
    for c in S:
        C[c] -= 1
        a = min(a, C['E'] + D['W'])
        D[c] += 1
    return a


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)
    print(solve(N, S))

if __name__ == '__main__':
    main()

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

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

c_e = [0]
t = 0
for c in s:
    if c == 'E':
        t += 1
    c_e.append(t)

ans = n
for leader in range(n):
    ans = min(ans, (leader - c_e[leader]) + (c_e[n] - c_e[leader + 1]))
print(ans)

Solution (Python):
N = int(input())
S = input()
cntE = [0 for _ in range(N)]
cntW = [0 for _ in range(N)]
for i in range(N):
    if S[i] == "E":
        cntE[i] = cntE[i-1]+1
        cntW[i] = cntW[i-1]
    else:
        cntE[i] = cntE[i-1]
        cntW[i] = cntW[i-1]+1
sumE = cntE[N-1]
sumW = cntW[N-1]
ans = sumE-cntE[0]
for i in range(1,N):
    ans = min(ans,cntW[i-1]+(sumE-cntE[i]))
print(ans)

Solution (Python):





def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a%b)
def lcm(a, b):
    return((a*b)//gcd(a,b))
def div(x):
    div=[1]
    check=2
    while(x!=1 and check <= int(x**0.5)+2):
        if(x%check==0):
            div.append(check)
            while(x%check==0):
                x=x//check
        check+=1
    if x != 1:
      div.append(x)
    return div
def div2(x):
    div2=[]
    check=2
    while(x!=1 and check <= int(x**0.5)+2):
        while x%check==0:
            div2.append(check)
            x/=check
        check+=1
    if x != 1:
      div2.append(x)
    return div2

def main():
    
    
    
    
    
    
    
    
    n=int(input())
    s=list(input())
    m=n
    cnt=0
    for i in range(n-1):
        if s[i+1]=="E":
            cnt+=1
    m=min(m,cnt)
    for i in range(n-1):
        if s[i+1]=="E":
            cnt-=1
        if s[i]=="W":
            cnt+=1
        if m > cnt:
            m=cnt
    print(m)
main()

Solution (Python):
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    s = str(input().rstrip('\n'))
    e = [0] * n
    w = [0] * n
    for i in range(n):
        if s[i] == "W":
            w[i] += 1 if i == 0 else w[i-1] + 1
        else:
            w[i] += 0 if i == 0 else w[i-1]

        if s[-i-1] == "E":
            e[-i-1] += 1 if i == 0 else e[-i] + 1
        else:
            e[-i - 1] += 0 if i == 0 else e[-i]
    mt = 10 ** 10
    for i in range(n):
        mt = min(mt, e[i] + w[i] - 1)
    print(mt)


if __name__ == '__main__':
    slove()

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

dp=[0]*N
dp[0]=S[1:].count("W")
for i in range(1,N):
  dpi=dp[i-1]
  if S[i]=="W":
    dpi-=1
  if S[i-1]=="E":
    dpi+=1
  dp[i]=dpi

print(N-max(dp)-1)

Solution (Python):
N = int(input())
S = input()
W = S.count('W')
E = S.count('E')
counter = len(S)
cost = E
for i in range(len(S)):
  if S[i] == 'E':
    cost -= 1
    if cost < counter:
      counter = cost
  else:
    if cost < counter:
      counter = cost
    cost +=1
print(counter)

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

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

print(min_n)

Solution (Python):
N = int(input())
S = input()
count_e = [0] * N
count_w = [0] * N
for i in range(N-1):
    if S[i] == "W":
        count_w[i+1] = count_w[i] + 1
    else:
        count_w[i+1] = count_w[i]

    if S[N-i-1] == "E":
        count_e[i+1] = count_e[i] + 1
    else:
        count_e[i+1] = count_e[i]

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

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

s = [0]*(N+1)

for i in range(1, N+1):
    k = 0
    if S[i-1] == "W":
        k = 1
    s[i] = s[i-1] + k

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


print(ans)

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

s = [0]*(N+1)
e = [0]*(N+1)
for i in range(1, N+1):
    k = 0
    if S[i-1] == "W":
        k = 1
    s[i] = s[i-1] + k

for i in reversed(range(0, N)):
    k = 0
    if S[i] == "E":
        k = 1
    e[i] = e[i+1] + k

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


print(ans)

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

l=[]
c=s.count('E',1)
for i in range(1,n):
    if s[i-1]=='W':
        c+=1
    if s[i]=='E':
        c-=1
    l.append(c)
print(min(min(l),s.count('E')))

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


if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
S=input()
E=[0]*N
W=[0]*N

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

EW=[0]*N
for i in range(N):
  EW[i]=E[i]+W[i]
print(N-1-(max(EW)-1))

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

E_num=S.count('E')
W_num=0
ans=n
for i in range(n):
  if S[i]=='E':
    E_num-=1
    ans=min(ans,E_num+W_num)
  else:
    ans=min(ans,E_num+W_num)
    W_num+=1
print(ans)

Solution (Python):
N = int(input())
S = input()
E = S.count('E')
W = N - E
result = N
l = 0
r = E
for i in S:
  if i == 'E':
    r-=1
    result = min(result, l+r)
  elif i == 'W':
    result = min(result, l+r)
    l+=1

print(result)

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

rw = 0
re = 0
lw = 0
le = 0
ans = 10**9

for i in range(n):
    if s[i:i+1] == "W":
        rw += 1
    else:
        re += 1

if s[0:1] == "W":
    rw -= 1
else:
    re -= 1
if re+lw < ans:
    ans = re + lw

for i in range(1,n):
    if s[i:i+1] == "W":
        rw -= 1
    else:
        re -= 1
    if s[i-1:i] == "W":
        lw += 1
    else:
        le += 1
    if re+lw < ans:
        ans = re + lw
print(ans)

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

Solution (Python):
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    s = str(input().rstrip('\n'))
    w = [0] * n
    e = [0] * n
    wc = 0
    ec = 0
    for i in range(n):
        if s[i] == "W":
            wc += 1
        if s[-i-1] == "E":
            ec += 1
        w[i] = wc
        e[-i-1] = ec
    m_c = 10 ** 10
    for i in range(n):
        m_c = min(m_c, w[i] + e[i] - 1)
    print(m_c)


if __name__ == '__main__':
    slove()

Solution (Python):
from collections import defaultdict, deque
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate
import sys


def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007



n = I()
li = S()
cnt = li[1:].count('E')
ret = cnt
for i in range(n - 1):
    if li[i + 1] == 'E':
        cnt -= 1
    if li[i] == 'W':
        cnt += 1
    ret = min(ret, cnt)



print(ret)

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

lW = 0
rE = S[1:N].count("E")
mi = lW + rE
for i in range(1, N):
    if S[i - 1] == "W":
        lW += 1
    if S[i] == "E":
        rE -= 1
    if lW + rE < mi:
        mi = lW + rE

print(mi)

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

x = [0] * N
if S[0] == "E":
    x[0] = 1

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

z = x[N-1]
if S[0] == "E":
    ans = z-1
else:
    ans = z

for i in range(N-1):
    tmp = z - 2 * x[i]
    if S[i+1] == "E":
        tmp = tmp - 1
    tmp += (i+1)
    if ans > tmp:
        ans = tmp

print(ans)

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

Solution (Python):


import sys
import math

n=int(input())


s=input()

sumw=[]
sumw.append(0)
for i in range(1,n+1):
	if(s[i-1]=="W"):
		tmp = sumw[i-1]+1
	else:
		tmp = sumw[i-1]
	sumw.append(tmp)
sume=[]
sume.append(0)
for i in range(1,n+1):
	if(s[n-i]=="E"):
		tmp = sume[i-1]+1
	else:
		tmp = sume[i-1]
	sume.append(tmp)

mini=sume[n-1]
for i in range(2,n+1):
	if(i == n):
		tmp=sumw[n-1]
	else:
		tmp=sumw[i-1]+sume[n-i+1]
	
	mini=min(mini,tmp)





print(mini)

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


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):
  if(s[i]=='W'):
    a[i]=a[i-1]+1
  else:
    a[i]=a[i-1]
ans=min(n-a[n-1]+a[0]-1,a[n-2])
for j in range(1,n-1):
  x=a[j-1]+(n-j-1)-(a[n-1]-a[j])
  if(x<ans):
    ans=x
print(ans)

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

E_left = 0
W_left = 0
E_right = 0
W_right = 0

count_list = []

for i in range(N):
    if i == 0:
        E_right = S[i+1:].count('E')
        W_right = S[i+1:].count('W')
        count = E_right
        count_list.append(count)
    elif 1 <= i <= N-1:
        if S[i] == 'E':
            E_right -= 1
        if S[i] == 'W':
            W_right -= 1
        if S[i-1] == 'E':
            E_left += 1
        if S[i-1] == 'W':
            W_left += 1
        count = E_right + W_left
        count_list.append(count)


ans = min(count_list)
print(ans)