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 = input()





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





ans = []
for i in range(N):
    ans.append(sum_up_W[i] + N - (i+1) - (sum_up_W[N] - sum_up_W[i+1])    )
print(min(ans))
'''


for i in range(len(S)):
    if(S[i] == 'E'):
        s.append(1)
    else:
        s.append(0)



E_count = []



for i in range(len(s)):
    E_count.append(  len(s[:i]) - sum(s[:i])  +   sum(s[i+1:]))
    
    




    
print(min(E_count))
'''
'''

def count_tern_new(s, leader):
    counter = 0
    
    counter += len(s[:leader]) - sum(s[:leader])
    
    counter += sum(s[leader+1:])
    return counter

answer_list_new = []
for i in range(len(s)):
    answer_list_new.append(count_tern_new(s, i))
print(min(answer_list_new))
'''


'''

def count_tern(s, leader): 
    counter = 0
    for i in range(leader):
        if(s[i] == 'W'):
            counter += 1            
            
    
    for j in range(leader+1, len(s)):
        if(s[j] == 'E'):
            counter += 1
            
    return counter


answer_list = []
for i in range(len(S)):
    answer_list.append(count_tern(S, i))


    
    
    
        

print(min(answer_list))
'''

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

E = [0] * (N + 1)
W = [0] * (N + 1)
for i, s in enumerate(S):
    E[i + 1] = E[i] + (s == 'E')
    W[i + 1] = W[i] + (s == 'W')

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

print(ans)

Solution (Python):
n = int(input())
s = input()
east = [0] * n
west = [0] * n
before_east, before_west = 0, 0

for i in range(n):
    if s[i] == 'E':
        east[i] = before_east+1
        west[i] = before_west
        before_east = east[i]
        before_west = west[i]
    else:
        east[i] = before_east
        west[i] = before_west+1
        before_east = east[i]
        before_west = west[i]

total_east = east[-1]
total_west = west[-1]

mini = float('inf')
for i in range(n):
    if i==0:
        left_east, left_west = 0, 0
        if s[0]=='E':
            right_east = total_east-1
            right_west = total_west
        else:
            right_east = total_east
            right_west = total_west-1
    else:
        left_east = east[i-1]
        left_west = west[i-1]
        right_east = total_east-east[i]
        right_west = total_west-west[i]

    tmp = left_west+right_east

    if tmp<mini:
        mini = tmp

print(mini)

Solution (Python):
n=int(input())
s=input()
cntW=[0]*n
cntE=[0]*n
for i in range(1,n):
  if s[i-1]=='E':
    cntE[i]=cntE[i-1]+1
  else:
    cntE[i]=cntE[i-1]
for i in range(n-2,-1,-1):
  if s[i+1]=='W':
    cntW[i]=cntW[i+1]+1
  else:
    cntW[i]=cntW[i+1]
ans=10**9
ans=min(ans,n-1-cntW[0],n-1-cntE[n-1])
for i in range(1,n-1):
  ans=min(ans,n-1-cntE[i]-cntW[i])
print(ans)

Solution (Python):
n=int(input())
s=input()
num=s.count("E")
length=len(s)

ans=num
for i in range(length):
    if s[i]=="W":num +=1
    elif s[i]=="E":num -=1
    ans=min(ans,num)

print(ans)

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

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



res = 9999999999
for i in range(1, n + 1):
    res = min(res, sdatw[i-1] +  sdate[n] - sdate[i])
print(res)

Solution (Python):
n = int(input())
s = input()
w_cnt = [0] * (n+1)
e_cnt = [0] * (n+1)
w_cnt[0], e_cnt[0] = 0, 0
for i in range(1, n+1):
    if s[i-1] == 'W':
        w_cnt[i] = w_cnt[i-1] + 1
        e_cnt[i] = e_cnt[i-1]
    else:
        w_cnt[i] = w_cnt[i-1]
        e_cnt[i] = e_cnt[i-1] + 1


ans = n
for i in range(1, n+1):
    ans = min(ans, w_cnt[i-1] + (e_cnt[n] - e_cnt[i]))
print(ans)

Solution (Python):
def resolve():
    n = int(input())
    S = tuple(input())

    c_e = S.count('E')
    c_w = 0
    ans = n

    for s in S:
        if s == 'E':
            c_e -= 1

        ans = min(ans, c_w + c_e)

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

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

Solution (Python):
N = int(input())
S = input()
ES, WS= [0]*(N+1), [0]*(N+1)
ES[0], WS[0] = 0, 0
for i in range(1, N+1):
  if S[i-1] == "E":
    ES[i] = ES[i-1] + 1
    WS[i] = WS[i-1]
  elif S[i-1] == "W":
    WS[i] = WS[i-1] + 1
    ES[i] = ES[i-1]
ans = 10**18
for i in range(1, N+1):
  ans = min(ans, WS[i-1] + ES[N] - ES[i])
print(ans)

Solution (Python):
def resolve():
    n = int(input())
    S = list(input())
    min_c = n
    count_e = S.count('E')
    count_w = 0

    for s in S:
        if s == 'E':
            count_e -= 1
        min_c = min(min_c, count_e + count_w)
        if s == 'W':
            count_w += 1

    print(min_c)

if __name__ == '__main__':
    resolve()

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

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

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

ans = 10**9
for i in range(N):
    ans = min(ans, w_cnt[i] + e_cnt[i])

print(ans)

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

N = int(input())
S = input().rstrip('\r\n')
counts = [0] * N
count = 0
for i, s in enumerate(S):
    counts[i] += count
    if s == 'W':
        count += 1
count = 0
for i, s in enumerate(S[::-1]):
    counts[N-1-i] += count
    if s == 'E':
        count += 1
ans = min(counts)
print(ans)

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

Solution (Python):
N = int(input())
S = input()
left = 0
right = S.count("E")
ans = False
for i in range(N):
    if i > 0 and S[i - 1] == "W":
        left += 1
    if S[i] == "E":
        right -= 1
    if ans is not False:
        ans = min(ans, left + right)
    else:
        ans = left + right
print(ans)

Solution (Python):
N = int(input())
S = input()
left = 0
right = S.count("E")
ans = False
for i in range(N):
    if i > 0:
        left += 1 if S[i - 1] == "W" else 0
    right -= 1 if S[i] == "E" else 0
    if ans is not False:
        ans = min(ans, left + right)
    else:
        ans = left + right
print(ans)

Solution (Python):
import numpy as np
N = int(input())
S = input()

left = [0]*N
right = [0]*N
for i in range(1,N):
    if S[i-1]=='W':
        left[i] += left[i-1] + 1
    else:
        left[i] += left[i-1]
left = np.array(left)

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

ans = (left+right).min()
print(ans)

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

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

print(ans)

Solution (Python):
n=int(input())
s=list(input())
w=[0]*n
for i in range(n):
    if s[i]=="E":
        w[i]=1
from itertools import accumulate    
d=list(accumulate([0]+w))
ans=n
cnt=0
for i in range(n):
    cnt=i-d[i]+d[-1]-d[i+1]
    ans=min(ans,cnt)
print(ans)

Solution (Python):
n = int(input())
s = input()
e = [0]*n
w = [0]*n
ans = float("inf")
for i in range(n-1):
  if s[i] == "W":
    e[i+1] += 1
  e[i+1] += e[i]

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

print(min([e[i] + w[i] for i in range(n)]))

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

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

arr=[0]*n
for i in range(n):
  if i!=0:
    arr[i]+=wnum[i-1]
  arr[i]+=n-i-1-(wnum[-1]-wnum[i])

print(min(arr))

Solution (Python):
import collections
N = int(input())
S = input()
dp = [0]*N
for i in range(N-1):
  if S[i] == "W":
    dp[i+1] = dp[i]+1
  else:
    dp[i+1] = dp[i]
dpE = [0]*N
for i in range(1,N)[::-1]:
  if S[i] == "E":
    dpE[i-1] = dpE[i]+1
  else:
    dpE[i-1] = dpE[i]
ret = float("inf")
for i in range(N):
  ret = min(ret,dp[i]+dpE[i])
print(ret)

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

S,T=[],[]
x,y=0,0
for i in range(n):
    if s[i]=='W':
        x+=1
        S.append(x)
    else:
        S.append(x)
    
    if t[i]=='E':
        y+=1
        T.append(y)
    else:
        T.append(y)

S=[0]+S
T=[0]+T

T.reverse()

z=3*(10**5)
for j in range(n):
    z=min(z,S[j]+T[j+1])

print(z)

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


S,T=[],[]
x,y=0,0
for i in range(n):
    if s[i]=='W':
        x+=1
        S.append(x)
    else:
        S.append(x)
    
    if t[i]=='E':
        y+=1
        T.append(y)
    else:
        T.append(y)

S=[0]+S
T=[0]+T

T.reverse()


z=3*(10**5)
for j in range(n):
    z=min(z,S[j]+T[j+1])
    

print(z)

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


def calculate(S):
    SSS = []

    totalENum = S.count("E")

    for index, value in enumerate(S):
        if index == 0:
            obj = {}
            if value == "W":
                obj.__setitem__("W", 1)
                obj.__setitem__("E", 0)
            else:
                obj.__setitem__("E", 1)
                obj.__setitem__("W", 0)
            SSS.append(obj)
        else:
            obj = {}
            prevW = SSS[index - 1].__getitem__("W")
            prevE = SSS[index - 1].__getitem__("E")
            if value == "W":
                obj.__setitem__("W", prevW + 1)
                obj.__setitem__("E", prevE)
            else:
                obj.__setitem__("E", prevE + 1)
                obj.__setitem__("W", prevW)

            SSS.append(obj)

    resVal = len(S)
    for index,value in enumerate(S):
        if value == "W":
            resVal = min(resVal,SSS[index].__getitem__("W") - 1 + totalENum - SSS[index].__getitem__("E"))
        else:
            resVal = min(resVal, SSS[index].__getitem__("W") + totalENum - SSS[index].__getitem__("E"))

    return resVal


result = calculate(S)

print(result)

Solution (Python):
"""
まあまずは愚直にOrder（N*N）でやろう
これナイーブ計算というらしい
→無理でした。

前処理でWの累積和を計算する
そのあとにインデックス回して最小値を計算する
確かに前処理がO(N)でできるなら、
O(N)　＋　O（N）で計算可能になる！！
"""

INF = 1000000
N = int(input())
S = input()
ans = INF

sum_n = [0]
add_n = 0


for s in S:
  if s == "W":
    add_n += 1
  sum_n.append(add_n)


for i in range(N):
  
  if i > 0:
    b_i = sum_n[i] - sum_n[0]
  else:
    b_i = 0

  
  if i < N:
    f_i = (N - (i + 1)) - (sum_n[N] - sum_n[i+1])
  else:
    f_i = 0
  
  tmp = b_i + f_i
  ans = min(ans, tmp)

print(ans)

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


A = []
for i in range(N):
  if S[i] == "W":
    A.append(1)
  elif S[i] == "E":
    A.append(-1)
    
R = [A[0]]
for i in range(1,N):
  R.append(R[-1] + A[i])


saidai = 0
for i in range(N):
  if i == 0:
    if saidai < R[N-1] - R[0]:
      saidai = R[N-1] - R[0]
      flag = i
  elif i == N-1:
    if saidai < -R[N-2]:
        saidai = -R[N-2]
        flag = i
  else:
    if saidai < (R[N-1] - R[i]) - R[i-1]:
        saidai = (R[N-1] - R[i]) - R[i-1]
        flag = i  
    
ans = A[:flag].count(1) + A[flag+1:].count(-1)

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 = list(input())
ans = s.count("E")
count = ans
for i in s:
  if i == "E":
    count -= 1
  else:
    count += 1
  ans = min(ans,count)
print(ans)

Solution (Python):
N = int(input())
S = input()
lc, rc = 0, 0
for i in range(1, N):
  if S[i] == 'E':
    rc += 1
m = lc + rc
for i in range(N-1):
  if S[i] == 'W':
    lc += 1
  if S[i+1] == 'E':
    rc -= 1
  if lc + rc < m:
    m = lc + rc
print(m)

Solution (Python):
n = int(input())
s = list(input())
A = [0]
B = [0]
for i in range(n-1):
    if s[i] == "W":
        A.append(A[i]+1)
    elif s[i] == "E":
        A.append(A[i])
    if s[n-i-1] == "E":
        B.append(B[i]+1)
    elif s[n-i-1] == "W":
        B.append(B[i])
m = n
for i in range(n):
    if A[i] + B[n-i-1] < m:
        m = A[i] + B[n-i-1]
print(m)

Solution (Python):
n=int(input())
s=input()
left=[0]*(n+1)
right=[0]*(n+1)
for i in range(n):
    if s[i]=="W":
        left[i+1]=left[i]+1
    else:
        left[i+1]=left[i]
 
for i in range(n-1,0,-1):
    if s[i]=="E":
        right[i-1]=right[i]+1
    else:
        right[i-1]=right[i]


print(min([left[i]+right[i] for i in range(n)]))

Solution (Python):
N=int(input())
S=input()
cnt = 0
for i in range(1,N):
  if S[i] == 'E': cnt += 1
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 = float('inf')
w_left = [0]
e_right = [0]*n

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

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

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

ans = float('inf')
w_left = [0]*n
e_right = [0]*n

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

for i in range(n):
    if ans > w_left[i]+e_right[i]:
        ans = w_left[i]+e_right[i]
print(ans)

Solution (Python):

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

sw=[0 for i in range(n)]
for i in range(n):
    if i==0:
        sw[0]+=(s[i]=='W')
    else:
        sw[i]=sw[i-1]+(s[i]=='W')
        
ans=10**6
for i in range(n):
    res=(n-i-1)-(sw[-1]-sw[i])
    if i>0:
        res+=sw[i-1]
    ans=min(ans,res)
print(ans)

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

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

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 = list(input())

tmp = s[1:].count("E")
ans = tmp
for i in range(n-1):
    if s[i] == "W":
        tmp += 1
    if s[i+1] == "E":
        tmp -= 1
    ans = min(ans,tmp)
print(ans)

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

cnt = s[1:].count('E')
low = cnt
for i in range(n-1):
  if s[i] == 'W':
    cnt += 1
  if s[i+1] == 'E':
    cnt -= 1
  if cnt < low:
    low = cnt
print(low)

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

cntw=[0]*n
cnte=[0]*n

if s[0]=="W":
    cntw[0]=1
    cnte[0]=0
else:
    cntw[0]=0
    cnte[0]=1

for i in range(1,n):
    if s[i]=="W":
        cntw[i]=cntw[i-1]+1
        cnte[i]=cnte[i-1]
    else:
        cntw[i]=cntw[i-1]
        cnte[i]=cnte[i-1]+1

nmin=cnte[n-1]-cnte[0]

for i in range(n):
    cntwi=cntw[i-1]    
    cntei=cnte[n-1] -cnte[i]    
    if nmin>cntwi+cntei:
        nmin=cntwi+cntei
print(nmin)

Solution (Python):


n = int(input().rstrip())
s = input().rstrip()

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

west_count = [0 for i in range(n)]
west_count[0] = 1 if s[0] == "W" else 0

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

ans = n
for i in range(n):
    tmp = 0
    if i < n-1:
        tmp += east_count[i+1]
    if i > 0:
        tmp += west_count[i-1]

    ans = min(ans,tmp)

print(ans)

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

counter = [0] * n

counter[0] = s[1:].count('E')

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

print(min(counter))

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


l = 0

r = S[1:].count('E')
ans = l + r
for i in range(1, N):
    if S[i-1] == 'W':  
        l += 1
    if S[i] == 'E':  
        r -= 1
    cnt = l+r
    ans = min(cnt, ans)  
print(ans)

Solution (Python):

n = int(input())
s = input()
e = [0]*n
w = [0]*n
for i in range(n):  
    if s[n-1-i] == "E":e[i] = e[i-1]+1
    else: e[i] = e[i-1]
    if s[i] == "W":w[i] = w[i-1]+1
    else: w[i] = w[i-1]

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

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

cnt = 0
ans = 10 ** 10

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

ans = min(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()

chg = [0]*n

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

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

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


l = 0
r = S[1:].count('W')
ans = l + r
for i in range(1, N):
    if S[i-1] == 'E':  
        l += 1
    if S[i] == 'W':  
        r -= 1
    cnt = l+r
    ans = max(cnt, ans)  
print(N - ans -1)

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

l=0
r=s[1:].count("E")
ans=r
MIN=r

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

Solution (Python):
n = int(input())
s = input()
S = []
for i in s:
    S.append(i)

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

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

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

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

def E_W():
    score = []
    s = 0
    
    for i in range(N):
        if (S[i] == 'W'):
            s -= 1
        else:
            s += 1
        score.append(s)
    
    max_index = score.index(max(score))
    
    cnt = S[:max_index].count('W') + S[max_index + 1 :].count('E')
    
    
    return (cnt)

ans = E_W()
print(ans)

Solution (Python):
n = int(input())
s = list(input())
result = [s[1:].count("E")]
for i in range(n-1):
    if s[i:i+2] == ["W","W"]:
        result.append(result[-1]+1)
    elif s[i:i+2] == ["E","E"]:
        result.append(result[-1]-1)
    else:
        continue
print(min(result))

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


a = [0] * n
b = [0] * n
c = [0] * n

for i in range(1, n):
    a[i] = a[i - 1]
    if s[i - 1] == 'W':
        a[i] += 1

for i in range(1, n):
    b[n - i - 1] += b[n - i]
    if s[n - i] == 'E':
        b[n - i - 1] += 1



for i in range(n):
    c[i] = a[i] + b[i]

print(min(c))

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

Wlist = [0]*(N+1) 
Elist = [0]*(N+1) 

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

ans = []

for i in range(1,N+1):
    ans.append((Elist[-1] - Elist[i]) + (Wlist[i-1]))
    
print(min(ans))

Solution (Python):
n=int(input())
S=input()
s=[]
s0=S.count("E",1,n)
slast=S.count("W",0,n-1)
s.append(s0)
for i in range(1,n):
    si=s[i-1]
    if S[i-1:i+1]=="EE":
        si+=-1
    if S[i-1:i+1]=="WW":
        si+=1
    s.append(si)
s.append(slast)
print(min(s))

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


E_right,W_left=s.count('E'),0
mi=n 
for i in range(n):
    if s[i]=='E': E_right-=1 
    if W_left+E_right<mi: mi=W_left+E_right 
    if s[i]=='W': W_left+=1 
print(mi)

Solution (Python):
n = int(input())
s = input()
l = 0
r = s[1:].count('E')
ans = l + r

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

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

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

        ans = min(cum, ans)
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
s=input()
wc=[0]
ec=[0]
for i in range(n):
    if s[i]=="W":
        wc.append(wc[i]+1)
        ec.append(ec[i])
    else:
        wc.append(wc[i])
        ec.append(ec[i]+1)
ans=10**6
for i in range(n+1):
    tmp=0
    
    tmp+=wc[i]
    tmp+=ec[n]-ec[i]
    ans=min(tmp,ans)
print(ans)

Solution (Python):
n = int(input())
s = input()
cnt = s.count('E',1,n)
ans = [cnt]

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

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

def main():
    N = int(input())
    S = list(input())
    e_right = collections.Counter(S)["E"]
    w_left = 0
    vals = set([])
    for i in range(N):
        if S[i] == "E":
            e_right -= 1
        vals.add(w_left + e_right)
        if S[i] == "W":
            w_left += 1

    print(min(vals))

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
S = input()
cnt = S.count('E',1,N)
ans = [cnt]

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

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

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

a, b = [], []
for i in range(n):
    if s[i] == 'W':
        a.append(1)
        b.append(0)
    else:
        a.append(0)
        b.append(1)        
        
import itertools
accum_a = [0] + list(itertools.accumulate(a))
accum_b = list(itertools.accumulate(b))

ans = float('inf')
for i in range(n):
    ans = min(ans, accum_a[i] + abs((accum_b[-1] - accum_b[i])))
print(ans)

Solution (Python):
n,s=open(0);a=t=s[1:].count('E')
for i in range(int(n)-1):t+=(s[i]>'F')-(s[i+1]<'F');a=min(a,t)
print(a)

Solution (Python):
input();s=input();a=t=s[1:].count('E')
for i in range(len(s)-1):t+=(s[i]>'F')-(s[i+1]<'F');a=min(a,t)
print(a)

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

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

tem = S[1:].count("E")
ans = min(N,tem)

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

print(ans)

Solution (Python):
from sys import maxsize

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

count_e = [0]
count_w = [0]
for i in range(1, n+1):
    if s[i-1] == 'E':
        count_e.append(count_e[i-1]+1)
        count_w.append(count_w[i-1])
    else:
        count_e.append(count_e[i-1])
        count_w.append(count_w[i-1]+1)

min = maxsize
for i in range(n):
    val = count_w[i] + count_e[n] - count_e[i+1]
    if val < min:
        min = val

print(min)

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

l = 0
r = S[1:].count('W')
ans = l + r
for i in range(1, N):
    if S[i-1] == 'E':  
        l += 1
    if S[i] == 'W':  
        r -= 1
    cnt = l+r
    ans = max(cnt, ans)  
print(N - ans -1)

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

Solution (Python):
import sys
 
stdin = sys.stdin
inf = 1 << 60
mod = 1000000007
 
ni = lambda: int(ns())
nin = lambda x: [ni() for _ in range(x)]
na = lambda: list(map(int, stdin.readline().split()))
nan = lambda x: [na() for _ in range(x)]
ns = lambda: stdin.readline().rstrip()
nsn = lambda x: [ns() for _ in range(x)]
nas = lambda: stdin.readline().split()

n = ni()
s = ns()

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

ans = inf
for i in range(n):
    ans = min(ans, cs[i] + ((n - i - 1) - (cs[n] - cs[i + 1])))

print(ans)

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

Solution (Python):



N = int(input().strip())
A_list = list(input().strip())

W_member_sum_0_to_N = [0]*len(A_list)
E_member_sum_N_to_0 = [0]*len(A_list)

cnt=0
for i in range(len(A_list)):
    if A_list[i] == "W":
        cnt += 1
    W_member_sum_0_to_N[i] += cnt

cnt=0
for i in range(len(A_list)-1,-1,-1):
    if A_list[i] == "E":
        cnt += 1
    E_member_sum_N_to_0[i] += cnt
        

count = N
for i_leader in range(len(A_list)):
    if i_leader == 0:
        turn_member = E_member_sum_N_to_0[i_leader+1]
    elif i_leader == len(A_list)-1:
        turn_member = W_member_sum_0_to_N[i_leader-1]
    else:
        turn_member = W_member_sum_0_to_N[i_leader-1] + E_member_sum_N_to_0[i_leader+1]

    if turn_member < count:
        count = turn_member


print(count)

Solution (Python):
n = int(input())
s = input()
total = s[1:].count('E')
ans = total
for i in range(n-1):
    if s[i]=='W':
        total += 1
    if s[i+1]=='E':
        total -= 1
    if ans>total:
      ans = total
print(ans)

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

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

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

Solution (Python):
n=int(input())
s=list(input())
c=[0 for _ in range(n)]
c[0]=s[1:].count('W')
for i in range(1,n):
	if s[i-1] =='W' and s[i]=='W':
		c[i]=c[i-1]-1
	elif s[i-1]=='E' and s[i]=='E':
		c[i]=c[i-1]+1
	else:
		c[i]=c[i-1]
print(n-1-max(c))

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+1] = W[i]+1
    else:
        W[i+1] = W[i]
        
for i in reversed(range(N)):
    if S[i]=='E':
        E[i] = E[i+1] + 1
    else:
        E[i] = E[i+1]

INF = 1<<30
ans = INF

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

print(ans)

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

Solution (Python):
import itertools
import math
import sys
from collections import Counter
from fractions import gcd
from functools import reduce

n = int(input())
s = input()
leader = 1
ans = s[1:].count("E")
old = ans
while leader < n:
    num = old
    if s[leader - 1] == "W":
        num += 1
    if s[leader] == "E":
        num -= 1
    ans = min(ans, num)
    old = num
    leader += 1
print(ans)

Solution (Python):
N = int(input())
s = input()
numE = s.count("E")
numW = 0
ans = 10**10
for i in range(N):
    if s[i] == 'W':
        ans = min(ans, numE + numW)
        numW += 1
    else:
        numE -= 1
        ans= min(ans, numE+numW)

print(ans)

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

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

num_E=s.count('E')
num_W=0
ans=float('inf')
for i in range(n):
    if s[i]=='W':
        ans=min(ans,num_W+num_E)
        num_W+=1
    else:
        num_E+=-1
        ans=min(ans,num_W+num_E)
print(ans)

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

sum_e=0
for tmp in s:
    if tmp=="E":
        sum_e+=1

ans=sum_e
for tmp in s:
    if tmp=="E":
        sum_e-=1
    else:
        sum_e+=1
    if ans > sum_e:
        ans=sum_e

print(ans)

Solution (Python):
n = int(input())
s = input()
c = s.count("E")
d = c
for i in s:
  if i == "E":
    d -= 1
  else:
    d += 1
  c = min(c,d)
print(c)

Solution (Python):
N=int(input())
S=input()
L=[0]*N
R=[0]*N
Lcnt=0
Rcnt=0
for i in range(1,N):
    if(S[i-1]=='W'):
        Lcnt+=1
    L[i]=Lcnt
for i in reversed(range(0,N-1)):
    if(S[i+1]=='E'):
        Rcnt+=1
    R[i]=Rcnt
        
ans=10e+9
for i in range(N):
    ans=min(ans,(L[i]+R[i]))
    
print(ans)

Solution (Python):
n = int(input())
s = input()
left_l = [0]
right_l = [0]
lcnt = 0
for i in range(1, n):
    if s[i - 1] == "W":
        lcnt += 1
    else:
        pass
    left_l.append(lcnt)
rcnt = 0
for j in range(1,n):
    if s[-j] == "E":
        rcnt += 1
    else:
        pass
    right_l.append(rcnt)
right_l.reverse()
minVal = n
for k in range(n):
    if left_l[k] + right_l[k] <= minVal:
        minVal = left_l[k] + right_l[k]
    else:
        continue
print(minVal)

Solution (Python):
N = int(input())
S = input()
W = [0]
E = [0]
ans = float('inf')
for s in S:
    if s == "W":
        W.append(W[-1]+1)
        E.append(E[-1])
    else:
        W.append(W[-1])
        E.append(E[-1]+1)
for i in range(1, N+1):
    ans = min(ans, W[i-1]+E[-1]-E[i])
print(ans)