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()
E = S.count('E')
W = 0
Min = N
for i in range(N):
  if S[i]=='E':
    E -= 1
  if i>0 and S[i-1]=='W':
    W += 1
  Min = min(Min,E+W)
print(Min)

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

def main():
    N = int(readline())
    S = readline().rstrip()
    e_right = S.count('E')
    w_left = 0
    res = N
    
    for i in range(N):
        if S[i] == 'E':
            e_right -= 1
            res = min(res, e_right + w_left)
        else:
            res = min(res, e_right + w_left)
            w_left += 1
    
    print(res)

if __name__ == '__main__':
    main()

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

def main():
    N = int(readline())
    S = readline().rstrip()
    e_right = S.count('E')
    w_right = S.count('W')
    e_left, w_left = 0, 0
    res = N
    for i in range(N):
        if S[i] == 'E':
            e_right -= 1
            res = min(res, e_right + w_left)
            e_left += 1
        else:
            w_right -= 1
            res = min(res, e_right + w_left)
            w_left += 1
    
    print(res)

if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
s=input()
lCurrent=rCurrent=0
l=[0]*n
r=[0]*n
ans=[0]*n

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

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

ans=[x+y for x,y in zip(l,r)]
print(min(ans))

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

w = [0]*(n+1)
e = [0]*(n+1)


for i, d in enumerate(s):
    if d == "W":
        w[i+1] = w[i] + 1
        e[i+1] = e[i]
    else:
        e[i+1] = e[i] + 1
        w[i+1] = w[i]

mnum = n
for i, d in enumerate(s):
    mnum = min(mnum, w[i] + e[n] - e[i+1])

print(mnum)

Solution (Python):
n = int(input())
s = input()
west = []
east = []
count_w = 0
count_e = 0

for i in s:
    if i == "W":
        count_w+=1
    west.append(count_w)
for j in reversed(s):
    if j == "E":
        count_e +=1
    east.append(count_e)
r_east = [k for k in reversed(east)]
   
attention = [west[i] + r_east[i] for i in range(n)]
print(min(attention)-1)

Solution (Python):
from sys import stdin


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    N = int(_in[0])  
    S = list(_in[1])   
    
    dp = [0]*N
    for i in range(N):
        if i==0:
            dp[i] = len([s for s in S[1:] if s=='E'])
        else:
            dp[i] = dp[i-1] + (1 if S[i-1]=='W' else 0) - (1 if S[i]=='E' else 0)
    cnt = min(dp)
    
    print(cnt)

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

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

e = 0
ans = w = S.count('E')
for i in S:
    if i == 'E':
        w -= 1
    else:
        e += 1
    ans = (min(ans, e+w))

print(ans)

Solution (Python):
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

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

left = [0] * (N + 2)
right = [0] * (N + 2)

for i in range(N):
    left[i + 1] = left[i] + (1 if S[i] == 'E' else 0)
    right[-i - 2] = right[-i - 1] + (1 if S[-i - 1] == 'W' else 0)

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

print(ans)

Solution (Python):
n = int(input())
s = input()
change = s.count("E")
ans = change
i_o = "dum"
for i in s:
    
    if i == "E":
        change -= 1
    if i_o == "W":
        change += 1
    ans = min(change,ans)
    i_o = i
print(ans)

Solution (Python):
def main():
    from itertools import accumulate
    n = int(input())
    s = input()
    s = s.replace("W", "1")
    s = s.replace("E", "0")
    s = [int(x) for x in s]

    t = list(accumulate(s))
    u = t[-1]
    ans = n

    for i, (j, k) in enumerate(zip(s, t)):
        m = (k - j) + (n - 1 - i) - (u - k)
        ans = min(ans, m)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    from itertools import accumulate
    n = int(input())
    f = lambda x: 1 if x == "W" else 0
    *s, = map(f, list(input()))
    t = list(accumulate(s))
    u = t[-1]
    ans = n

    for i, (j, k) in enumerate(zip(s, t)):
        m = (k - j) + (n - 1 - i) - (u - k)
        ans = min(ans, m)
    print(ans)


if __name__ == '__main__':
    main()

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

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

to_east = 0
to_west = s.count('E', 1)
ans = to_east+to_west

for i in range(1, n):
  if s[i-1]=='W':
    to_east += 1
  if s[i]=='E':
    to_west -= 1
  ans = to_east+to_west if ans>to_east+to_west else ans
  
print(ans)

Solution (Python):
n = int(input())
s = [p for p in input()]
w = [0]
e = [0]
mn = float("inf")
for i in range(n):
  if s[i] == 'W':
  	w.append(w[i]+1)
  else:
    w.append(w[i])
  e.append(i+1-w[i+1])
for i in range(n):
  if w[i]+e[n]-e[i+1] < mn:
    mn = w[i]+e[n]-e[i+1]
print(mn)

Solution (Python):
n = int(input())
s = input()
t1 = [0]*n
t2 = [0]*n
for i in range(1, n):
  t1[i] = t1[i-1] + (s[i-1] == 'W')
  t2[n-i-1] = t2[n-i] + (s[n-i] == 'E')
ans = n
for i in range(n):
  ans = min(ans, t1[i] + t2[i])
print(ans)

Solution (Python):
from sys import stdin
def main():
    
    readline=stdin.readline
    N=int(readline())
    S=readline().strip()

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

    res=0
    min_res=N
    for i in range(N):
        res=abs(t_left-left)+abs(t_right-right)
        min_res=min(min_res,res)
        if i!=N-1:
            t_left+=1
            t_right-=1
            if S[i]=="E":
                left+=1
            if S[i+1]=="W":
                right-=1

    print(min_res)
if __name__=="__main__":
    main()

Solution (Python):
n=int(input())
s=input()
e=s.count("E")
w=n-e
cnt_e=0
cnt_w=0
maxi=0
for i in range(n):
  if s[i]=="E":
    if maxi<=(w-cnt_w)+cnt_e:
      maxi=(w-cnt_w)+cnt_e
    cnt_e+=1
  else:
    if maxi<=(w-cnt_w-1)+cnt_e:
      maxi=(w-cnt_w-1)+cnt_e
    cnt_w+=1
print(n-(maxi+1))

Solution (Python):
'''
制約
2 ≤ N ≤ 3×10^5(300,000)

入力例 3
8
WWWWWEEE

出力例 3
3

考え方：
Eを0, Wを1と置くと、
0000000X11111111 と並ぶようにすればよい。
そのために、値を変える数が最小になるリーダーの位置を調べればよい

-> 左端のW(West), 右端のE(East)はほぼ必ず向きを変える必要がある。


A. 全パターン試す
ある地点より左のWの数 + 右のEの数　の合計が最小になるパターンを求める（文字一致でカウント）
-> TLE

B. 全パターン
str.count('W')でやる
-> TLE

C. 「累積和」(cumulative sum)というアルゴリズムを使わないと解けない問題。学習して、使う！


'''




import sys
input = sys.stdin.readline

is_debug = False


def main():
    '''メイン処理'''
    n = int(input())
    sss = input()

    s_list = list(sss)

    W_sums = [0] * n 
    E_sums = [0] * n 
    W_sum = 0 
    E_sum = 0 
    
    if is_debug:
        print(W_sums)
    
    for i in range(n):
        if s_list[i] == 'W':
            W_sum += 1
        W_sums[i] = W_sum
        if s_list[n-i] == 'E':
            E_sum += 1
        E_sums[(n-1)-i] = E_sum

    if is_debug:
        print(W_sums)
        print(E_sums)
    
    
    min_cost = float('inf')
    for i in range(n):
        
        if i == 0:
            W_cost = 0
        else:
            W_cost = W_sums[i-1]
        
        if i == n-1:
            E_cost = 0
        else:
            E_cost = E_sums[(i-1)+1]

        if is_debug:
            print(i, W_cost, E_cost)

        cost = W_cost+E_cost
        if cost < min_cost:
            min_cost = cost
    
    print(min_cost)

if __name__ == "__main__":
    
    main()

Solution (Python):

import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial
from bisect import bisect_left 
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
def pri(x): print('\n'.join(map(str, x)))

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

wl = [0]*N
el = [0]*N
wr = [0]*N
er = [0]*N

if S[0] == 'W':
    wl[0] = 1
    el[0] = 0
else:
    wl[0] = 0
    el[0] = 1

if S[-1] == 'W':
    wr[0] = 1
    er[0] = 0
else:
    wr[0] = 0
    er[0] = 1

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

    if S[-i-1] == 'W':
        wr[i] = wr[i-1] + 1
        er[i] = er[i-1]
    else:
        wr[i] = wr[i-1]
        er[i] = er[i-1] + 1






res = N
for i in range(N):
    if i == 0:
        val = er[N-i-1]
    elif i == N-1:
        val = wl[i-1] 
    else:
        val = wl[i-1] + er[N-i-1]
    
    if res > val:
        res = val

print(res)

Solution (Python):
N = int(input())
S = input()
x = S.count('E')
ans = x
for s in S:
    if s == 'E':
        x -= 1
        if ans > x:
            ans = x
    else:
        x += 1
print(ans)

Solution (Python):
N = int(input())
S = input()
x = S.count('E')
ans = x
for s in S:
    if s == 'E':
        x -= 1
        if ans > x:
            ans = x
    else:
        x += 1
    if ans > x:
        ans = x
print(ans)

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

ans = ans_cur = S.count("E")
for c in S:
    if c == "E":
        ans_cur -= 1
    else:
        ans_cur += 1
    ans = ans if ans <= ans_cur else ans_cur
print(ans)

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

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

    ans = ans if ans <= l + r else l + r

print(ans)

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

print(min(dp))

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

accum=list(itertools.accumulate(d))
ans=10**9
count=0
for i in range(1,n+1):
  count+=accum[i-1]-accum[0]
  
  count+=(n-i)-(accum[-1]-accum[i])
  
  if ans>count:
    ans=count
  count=0
print(ans)

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

    all_east = 0
    all_west = 0

    for i in range(n):
        if s[i] == 'W':
            all_west += 1
        else:
            all_east += 1

    count = 10 ** 6

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

    print(count)

if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np
N = int(input())
S = input()
pre_s = S[0]
sum1 = sum(s == 'E' for s in S)
res = sum1
if S[0] =='E':
    sum1-=1
    
for i in range(1,len(S)):
    if pre_s =='W':
        sum1 +=1
    if S[i]=='E':
        sum1 -=1
    if sum1 < res:
        res =sum1
    pre_s = S[i]
print(res)

Solution (Python):
n=int(input())
A=[0]*n
S=input()

for i in range(n):
    if S[i]=="E":
        A[i]=1
E=sum(A)
e=E

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

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

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

for i in range(n):
    if i == 0:
        if s[i] == 'W':
            w[i] = 1
            e[i] = 0
        else:
            w[i] = 0
            e[i] = 1
    else:
        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 = 10 ** 10
for i in range(n):
    if i == 0:
        ans = min(ans, e[-1] - e[0])
    elif i == n:
        ans = min(ans, w[-1])
    else:
        right = e[-1] - e[i]
        left = w[i - 1]
        ans = min(ans, left + right)

print(ans)

Solution (Python):
N = int(input())
S = list(input())
W = 0
E = S[1:].count('E')
Turn = []
Turn.append(E)

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

print(min(Turn))

Solution (Python):





def main():
    N = int(input())
    S = input()
    
    ans = len([1 for s in S[1:] if s == "E"])
    now = ans
    for i in range(1, N):
        now += 1 if S[i-1] == "W" else 0
        now -= 1 if S[i] == "E" else 0

        if now < ans:
            ans = now
    
    return ans


print(main())

Solution (Python):
n=int(input())
s=input()
rw=s.count("W")
re=s.count("E")
lw=0
le=0
now=""
m=10000000000000000000
for i in range(n):
    if now=="W":lw+=1
    if now=="E":le+=1
    if s[i]=="W":rw-=1
    if s[i]=="E":re-=1
    m=min(m,lw+re)
    now=s[i]
print(m)

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

E = 0
W = S.count("W")
a, ans = 0, len(S)

for i in range(len(S)):
  if S[i] == "W":
    W -= 1
  a = len(S) - 1 - W - E
  if a < ans:
    ans = a
  if S[i] == "E":
    E += 1

print(ans)

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


s = input()
countE = 0
countW = s.count("W")
ans = len(s)
min_idx = 0

for i in range(len(s)):
    if s[i] == "W":
        countW -= 1
    total = int(len(s))-1-i-countW + i-countE
    if total < ans:
        ans = total
    if s[i] == "E":
        countE += 1

print(ans)

Solution (Python):
n = int(input())
s = input()
cum_R = [0]*(n+1)
for i in range(n):
    cum_R[i+1] = cum_R[i]+(s[i] == 'E')

INF = 10**18
ans = INF
for i in range(n):
    cnt_L = i-cum_R[i]
    cnt_R = cum_R[n]-cum_R[i+1]
    ans = min(ans, cnt_L+cnt_R)
print(ans)

Solution (Python):
n = int(input())
s = input()
cnt_R = s.count('E')
cnt_L = 0
INF = 10**18
ans = INF
for x in s:
    if x == 'W':
        ans = min(ans, cnt_L+cnt_R)
        cnt_L += 1
    else:
        cnt_R -= 1
        ans = min(ans, cnt_L+cnt_R)
print(ans)

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

for i in range(n):
    dp_L[i+1] = dp_L[i]+[0, 1][s[i] == 'W']

for i in reversed(range(n)):
    dp_R[i] = dp_R[i+1]+[0, 1][s[i] == 'E']
    
print(min(L+R for L, R in zip(dp_L, dp_R)))

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

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

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

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

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

INF = 10**18
ans = INF
for i in range(1, n+1):
    ans = min(ans, (((i-1)-cum_sum[i-1])+(cum_sum[n]-cum_sum[i])))

print(ans)

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

S_W = [0]*(N+1)
S_E = [0]*(N+1)

A_W = []
A_E = []

for i in range(N):
    if S[i]=="W":
        A_W.append(1)
        A_E.append(0)
    else:
        A_W.append(0)
        A_E.append(1)
for i in range(len(S)):
    S_W[i+1] = S_W[i] + A_W[i]
    S_E[i+1] = S_E[i] + A_E[i]

ans = float('inf')
for i in range(len(S)):
    e_count = S_E[len(S_E)-1]-S_E[i+1]
    w_count = S_W[i]
    ans = min(ans, e_count+w_count)
   
print(ans)

Solution (Python):

n=int(input())
s=input()
lens=len(s)

ec=[0]*lens
wc=[0]*lens
if s[0]=="W":
    wc[0]=1
else:
    ec[0]=1
for i in range(1,len(s)):
    if s[i]=="W":
        wc[i]=wc[i-1]+1
        ec[i]=ec[i-1]
    else:
        wc[i]=wc[i-1]
        ec[i]=ec[i-1]+1
        
cmin=min(ec[lens-1]-ec[0],wc[lens-2])
for i in range(1,n-1):
    c=ec[lens-1]-ec[i]+wc[i-1]    

    cmin=min(cmin,c)
print(cmin)

Solution (Python):
N = int(input())
S = input()
east = [0]*N
west = [0]*N
for i in range(N):
    s = S[i]
    if i == 0:
        if s == "E":
            east[i] = 1
        else:
            west[i] = 1
    else:

        if s == "E":
            east[i] = east[i-1]+1
            west[i] = west[i-1]
        else:
            east[i] = east[i-1]
            west[i] = west[i-1]+1
Min = 10000000000
for i in range(N):
    if i == 0:
        num = east[N-1]-east[0]
    elif i == N-1:
        num = west[N-2]
    else:
        num = east[N-1]-east[i]+west[i-1]
    Min = min(Min,num)
print(Min)

Solution (Python):
n = int(input())
s = input()
INF = 10**12
li = [INF]*n
c = 0
for i in range(len(s)):
    if i == 0:
        continue
    else:
        if s[i] == 'E':
            c+=1
li[0] = c
for i in range(n):
    if i == 0:
        continue
    else:
        li[i] = li[i-1]
        if s[i] == 'E':
            li[i] -= 1
        if s[i-1] == 'W':
            li[i] += 1

print(min(li))

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

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

N = int(input())
S = input()[:-1]
acc = [0]*(N+1)

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

ans = 10**18

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

print(ans)

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

oder = N
l = [0 for i in range(N)]
r = [0 for i in range(N)]

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

for i in range(N):
  oder = min(oder, l[i]+r[i])

print(oder)

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

oder = N
l = [0 for _ in range(N)]
r = [0 for i in range(N)]

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

for i in range(N):
  oder = min(oder, l[i]+r[i])

print(oder)

Solution (Python):
n = int(input())
s = str(input())
e_e = 0
w_w = 0
ans = len(s)
for i in range(len(s)):
  if s[i] ==  'E':
    e_e += 1
for i in range(len(s)):
  if s[i] == 'E':
    e_e -= 1
  ans = min(ans, e_e + w_w)
  if s[i] == 'W':
    w_w += 1
print(ans)

Solution (Python):

import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N=int(input())
    S=list(input())
    S_W=[0]*(N+1)
    S_E=[0]*(N+1)
    for i in range(N):
        S_W[i+1]=S_W[i]+(S[i]=='W')
        S_E[i+1]=S_E[i]+(S[i]=='E')
    ans=INF
    for i in range(N):
        ans=min(ans,S_W[i]+(S_E[N]-S_E[i+1]))
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
s = input()
w_c=0
e_c=0
ans=n
for i in s:
  if i == 'E':
    e_c+=1
for i in s:
  if i == 'E':
    e_c-=1
  k=w_c+e_c
  if ans > k:
    ans = k
  if i == 'W':
    w_c+=1
print(ans)

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


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

Solution (Python):

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

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

    print(min_count)

if __name__ == '__main__':
    main()

Solution (Python):
import sys

from itertools import accumulate

def main():
    N = int(input())
    S = input()
    
    east = accumulate([0] + list(S), lambda total, c: total + (1 if c == "E" else 0))
    next(east)
    east = list(east)

    west = accumulate([0] + list(S), lambda total, c: total + (1 if c == "W" else 0))
    next(west)
    west = list(west)

    east_max = east[-1]
    for i in range(N):
        e = east_max - east[i]
        w = 0 if i == 0 else west[i-1]
        yield e + w



print(min(main()))

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

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

ans = left[0] + right[0]
for i in range(1, N):
    count = left[i] + right[i]
    if ans > count:
        ans = count
print(ans)

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

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

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

list = [0]*N
for i in range(N):
    list[i] = listw[i] + liste[-1] - liste[i+1 if i != N-1 else -1]

list.sort()

print(list[0])

Solution (Python):
n = int(input())
s = input()
lstW = [0]
c = 0

for i in range(1,len(s)):
    if s[i-1] == "W":
        c += 1
    lstW.append(c)
    
lstE = [0]
c = 0
for i in range(len(s)-2,-1,-1):
    if s[i+1] == "E":
        c += 1 
    lstE.append(c)
lst = []
for i in range(len(lstW)):
    lst.append(lstW[i] + lstE[n-i-1])
lst.sort()
print(lst[0])

Solution (Python):
n = int(input())
s_list = list(input())
e_sum = [0]
su = 0
for s in s_list[:-1]:
    if s == "W":
        su += 1
    e_sum.append(su)
w_sum = [0]
su = 0
for s in s_list[::-1][:-1]:
    if s == "E":
        su += 1
    w_sum.append(su)
w_sum = w_sum[::-1]
ans = 10 ** 10
for i in range(n):
    ans = min(ans, e_sum[i] + w_sum[i])
print(ans)

Solution (Python):

import sys


def solve(N: int, S: str):
    nww = [0] * N
    nee = [0] * N

    acc = 0
    for i in range(N - 1):
        if S[i] == "W":
            acc += 1
        nww[i + 1] = acc

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

    print(min(nwwi + neei for nwwi, neei in zip(nww, nee)))


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):
from itertools import accumulate
n = int(input())
s = input()
list_e = [0]*n
list_w = [0]*n
for i in range(n):
  if s[i]=="E":
    list_e[i]=1
for i in range(n):
  if s[i]=="W":
    list_w[i]=1
    
list_e = list(accumulate([0]+list_e))
list_w = list(accumulate([0]+list_w))
m = 10**7
for i in range(n):
  a = list_w[i]
  b = list_e[-1]-list_e[i+1]
  m = min(m,a+b)
print(m)

Solution (Python):
n = int(input())
s = input()
e = [0 for i in range(n)]
w = [0 for i in range(n)]
ee = 0
ww = 0
for i in range(n):
	w[i] = ww
	if s[i] == "E":
		ee += 1
	else:
		ww += 1
	e[i] = ee
ec = s.count("E")
wc = s.count("W")
ans = 10**6
c = 0
for i in range(n):
	c = ec - e[i] + w[i]
	ans = min(c,ans)
print (ans)

Solution (Python):


data=int(input())

ini=input()

array=[]

l=0

for i in range(data):
  if ini[i]=="W":
    l+=1
  array.append(l)

m=min(array[data-1],data-array[data-1])

for i in range(1,data):
  m=min(m,array[i-1]+data-1-i-array[data-1]+array[i])
print(m)

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

def f(S):
    cnt = S.count('E')
    for s in S:
        if s == 'E':
            cnt -= 1
        yield cnt
        if s == 'W':
            cnt += 1

print(min(list(f(S))))

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

w = 0
wlist = []
elist = [0]*n
e = 0
for i in range(n):
    if s[i] == 'W':
        w += 1
    wlist.append(w)

for i in range(n-1,-1,-1):
    if s[i] == 'E':
        e += 1
    elist[i] = e
mn = 10**9
for i in range(n):
    mn = min(mn, elist[i]+wlist[i])

print(mn-1)

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

def f(S):
  count = S.count('E')
  for s in S:
    if s == 'E':
      count += -1
    yield count
    if s == 'W':
      count += 1

print(min(list(f(S))))

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

def f(S):
  count = {'W': 0, 'E': 0}
  yield count['W'], count['E']
  for c in S:
    count[c] += 1
    yield count['W'], count['E']

count = list(f(S))
print(min([count[i-1][0] + count[-1][1] - count[i][1] for i in range(1, N+1)]))

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

w_cnt = [0] * n
e_cnt = [0] * n
for i, ss in enumerate(s):
    if i != 0:
        w_cnt[i] = w_cnt[i-1]
        e_cnt[i] = e_cnt[i-1]
    if ss == 'W':
        w_cnt[i] += 1
    else:
        e_cnt[i] += 1

ans = n
for i in range(n):
    left = i - 1
    right = i + 1
    left_w = 0
    right_e = 0
    if left >= 0:
        left_w = w_cnt[i-1]
    if right_e <= n - 1:
        right_e = e_cnt[-1] - e_cnt[i]
    ans = min(ans, left_w + right_e)
print(ans)

Solution (Python):
N = int(input())
S = input()
int_s = [0] * N
cum_sum_w = [0] * (N + 1)
cum_sum_e = [0] * (N + 1)
answers = [0] * N
for i in range(N):
    if (S[i] == 'W'):
        int_s[i] = 1
    else:
        int_s[i] = 0
    cum_sum_w[i + 1] = cum_sum_w[i] + int_s[i]
for i in range(N):
    if (S[i] == 'E'):
        int_s[i] = 1
    else:
        int_s[i] = 0
    cum_sum_e[i + 1] = cum_sum_e[i] + int_s[i]
for i in range(N):
    answers[i] = cum_sum_w[i] + (cum_sum_e[N] - cum_sum_e[i + 1])
print(min(answers))

Solution (Python):
n = int(input())
s = input()
c = [0 for i in range(n)]
for i in range(0+1, n):
    if s[i] == 'E':
        c[0] += 1

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

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

ans=s[1:].count("E")
c=ans

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

print(ans)

Solution (Python):
n=int(input())
s=input()
 
ans=s[1:].count("E")
c=ans

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

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

Solution (Python):
import math, sys
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from copy import deepcopy
from functools import lru_cache
from heapq import heapify, heappop, heappush
from itertools import accumulate, combinations, permutations
input = sys.stdin.readline
mod = 10**9 + 7
ns = lambda: input().strip()
ni = lambda: int(input().strip())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n = ni()
s = ns()

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

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

print(min(ans))

Solution (Python):
import math, sys
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from copy import deepcopy
from functools import lru_cache
from heapq import heapify, heappop, heappush
from itertools import accumulate, combinations, permutations
input = sys.stdin.readline
mod = 10**9 + 7
ns = lambda: input().strip()
ni = lambda: int(input().strip())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

def main():
    n = ni()
    s = ns()

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

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

    print(min(ans))

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
s = input()
wdp = [0]*n
edp = [0]*n
if s[0] == "E":
    edp[0] = s.count("E")-1
else:
    edp[0] = s.count("E")

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

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

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

print(min(ans))

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

ans = 10 ** 8

for i in range(N):
    if i == 0:
        ans = min(ans, E[-1] - E[0])
    elif i == N-1:
        ans = min(ans, W[-2])
    else:
        ans = min(ans, W[i-1] + E[-1] - E[i])
print(ans)

Solution (Python):
import sys

n, s = sys.stdin.read().split()

def main():
    cnt = s.count('E')
    cur = 'E'
    res = [cnt]
    for c in s:
        if cur == 'W': cnt += 1
        if c == 'E': cnt -= 1
        res.append(cnt)
        cur = c
    print(min(res))
    
if __name__ ==  '__main__':
    main()

Solution (Python):




N = int(input())
S = input()
w = [0 for _ in range(N)]       
sw = [0 for _ in range(N+1)]    
e = [0 for _ in range(N)]       
se = [0 for _ in range(N+1)]    
l = []

for i in range(N):
    if S[i] == "W":
        w[i] = 1
    else:
        e[i] = 1


for i in range(N):
    sw[i+1] = sw[i] + w[i]
    se[i+1] = se[i] + e[i]


for i in range(N):      
    counter = sw[i] + (se[N] - se[i+1])
    l.append(counter)

print(min(l))

Solution (Python):
n = int(input())
s = input()
result = []
w_list = []
e_list = []
w_1 = 0
e_1 = 0
for i in range(n):
  if s[i] == "W":
    w_1 += 1
    w_list.append(w_1)
    e_list.append(e_1)
  if s[i] == "E":
    e_1 += 1
    w_list.append(w_1)
    e_list.append(e_1)
  
for i in range(n):
  if i == 0:
    ans = e_list[-1] - e_list[0]
    result.append(ans)
  if i == n-1:
    ans = w_list[n-2]
    result.append(ans)
  else:
    ans_e = w_list[i-1]
    ans_w = e_list[-1] - e_list[i]
    result.append(ans_e + ans_w)
    
print(min(result))