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


acme = list()
acmw = list()
acme.append(0)
acmw.append(0)
ans = n+1


for i in range(n):
    if s[i] == 'E':
        acme.append(acme[i] + 1)
        acmw.append(acmw[i])
    elif s[i] == 'W':
        acme.append(acme[i])
        acmw.append(acmw[i] + 1)

acme.append(acme[n])
acmw.append(acmw[n])


for i in range(1, n+1):
    ecnt = acme[n] - acme[i]
    wcnt = acmw[i-1]
    wcnt = ecnt + wcnt
    ans = min(ans, wcnt)


print(ans)

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

e_cnt = S.count('E')
w_cnt = 0
ans = float('inf')
for i in range(N):
    if S[i] == 'E':
        e_cnt -= 1
        w = w_cnt
    else:
        w_cnt += 1
        w = w_cnt - 1
    
    ans = min(ans, e_cnt+w)

print(ans)

Solution (Python):
N = int(input())
S = input().strip()
A = [0 for _ in range(N+1)]
B = [0 for _ in range(N+1)]
for i in range(1,N+1):
    if S[i-1]=="E":
        A[i] = A[i-1]+1
        B[i] = B[i-1]
    else:
        A[i] = A[i-1]
        B[i] = B[i-1]+1
cmin = 10**6
for i in range(N):
    cnt = B[i]+A[N]-A[i+1]
    cmin = min(cmin,cnt)
print(cmin)

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


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

ans = float("INF")
for i in range(1,n+1):
    a = ((i-1) - cnt[i-1]) + (cnt[-1]-cnt[i])
    if ans > a: ans = a
print(ans)

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

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

Solution (Python):

n = int(input())
S = input()

ans = S.count('E')

w = 0
tmp_w = 0
e = ans

for s in S:
    if s == 'E':
        e -= 1
    else:
        tmp_w += 1

    ans = min(ans, w+e)
    w = tmp_w

print(ans)

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

Solution (Python):

import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd





def solve():
    
    n = int(input())
    s = input()
    w = 0 
    e = 0 
    for i in range(n):
        if s[i] == 'W':
            w += 1
        else:
            e += 1
    ww = 0
    ee = 0
    ans = n
    for i in range(n):
        if s[i] == 'W':
            w -= 1
        else:
            e -= 1
        
        ans = min(ans, ww + e)

        if s[i] == 'W':
            ww += 1
        else:
            ee += 1

    print(ans)
    
t = 1

for case in range(1,t+1):
    ans = solve()


"""

R s
S p
P r

"""

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

cnt_W = [0]*N 
if S[0] == 'W':
    cnt_W[0] = 1

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

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

print(min(ans))

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

cnt_W = [0]*N 
if S[0] == 'W':
    cnt_W[0] = 1

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

most_E = cnt_W[-1] 

ans = float('inf')
for i in range(N):
    tmp = 0
    if i == 0:
        if S[i] == "W":
            tmp = (N-most_E)
        else:
            tmp = (N-most_E)-1
        
    elif i == N-1:
        if S[i] == "W":
            tmp = most_E-1
        else:
            tmp = most_E
        
    else:
        tmp = cnt_W[i-1] + (N-most_E) - (i+1-cnt_W[i])
        

    ans = min(ans, tmp)
print(ans)

Solution (Python):
mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

    N = int(input())
    S = input().rstrip('\n')
    ans = N
    w = 0
    e = S.count("E")
    for s in S:
        if s == "E":
            e -= 1
        ans = min(ans, w+e)
        if s == "W":
            w += 1
    print(ans)


if __name__ == '__main__':
    main()

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

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

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

print(ans)

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

def solve():
    right_E=0
    left_W=0
    ans=10**7

    for i in range(len(s)):
        if s[i]=="E":
            right_E+=1
     
    for i in range(len(s)):
        if s[i]=="E":right_E-=1
        cnt=left_W+right_E
        ans=min(ans,cnt)
        if s[i]=="W":left_W+=1
    
    print(ans)
    
if __name__ == "__main__":
    solve()

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

def solve():
    right_E=0
    right_W=0
    left_E=0
    left_W=0
    ans=10**7

    for i in range(0,len(s)):
        if s[i]=="E":
            right_E+=1
        else:
            right_W+=1
    
    for i in range(0,len(s)):
        if s[i]=="E":
            right_E-=1
            cnt=left_W+right_E
            ans=min(ans,cnt)
            left_E+=1
        else:
            right_W-=1
            cnt=left_W+right_E
            ans=min(ans,cnt)
            left_W+=1
    
    print(ans)
    
if __name__ == "__main__":
    solve()

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

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

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

numbers=[]
number=[0,0]


for s in S:
    if s=='W':
        number[0]+=1
        suuji=number[1]-number[0]
        numbers.append(suuji)
    else:
        number[1]+=1
        suuji=number[1]-number[0]
        numbers.append(suuji)
place=numbers.index(max(numbers))


if place==0:
    answer=S[1:].count('E')
    print(answer)
elif place==N-1:
    answer=S[:-1].count('W')
    print(answer)
else:
    first=S[:place]
    last=S[place+1:]
    answer=first.count('W')+last.count('E')
    print(answer)

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

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

sum_left_w = 0
sum_right_e = alle
result = n
for i in range(n):
  if s[i] == 'E':
    sum_right_e -= 1
  result = min(result, sum_left_w+sum_right_e)
  
  if s[i] == 'W':
    sum_left_w += 1
print(result)

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

from collections import Counter
we=Counter(list(S))
enum=we["E"]
wnum=0
ans=enum
for s in S:
  if s=="W":
    wnum+=1
  elif s=="E":
    enum-=1
  ans=min(ans, enum+wnum)
print(ans)

Solution (Python):
import bisect,copy,heapq,string
from collections import *
from math import *
from itertools import *
import sys
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
s = input()
a = [0]*n
b = [0]*n
c = [0]*n

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

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

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

for i in range(n-2,-1,-1):
        b[i] += b[i+1]

ans = min(a[i]+b[i] for i in range(n))
print(ans)

Solution (Python):
def Csum(a,x):
    b,c=[0],0
    for i in range(len(a)):
        if a[i]==x:
            c+=1
        b.append(c)
    return b

n=int(input())
s=input()
a,b,w,e=Csum(s,'W'),Csum(s,'E'),s.count('W'),s.count('E')
a.append(w)
b.append(e)
ans=n
for i in range(1,n+1):
    ans=min(ans,e+a[i-1]-b[i])
print(ans)

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


count = s.count('E')

ans = count

for ch in s:
    if ch == 'E':
        count -= 1
        ans = min(count,ans)
    else:
        count += 1
    
print(ans)

Solution (Python):
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


def calc_cumsum(l):
    cumsum = [l[0]]
    for i in l[1:]:
        cumsum.append(cumsum[-1] + i)
    return cumsum


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

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


count = s.count('E')

ans = count

for ch in s:
    if ch == 'E':
        count -= 1
        ans = min(count,ans)
    else:
        count += 1
    
print(ans)

Solution (Python):
n = int(input())
s = input()
left = [0]
right = [0]
for i in range(n):
    if s[i] == 'W':
        tmp = left[-1] + 1
        left.append(tmp)
    else:
        left.append(left[-1])
    if s[n-1-i] == 'E':
        tmp = right[-1] + 1
        right.append(tmp)
    else:
        right.append(right[-1])
mi = 10**18
right = right[::-1]
for i in range(n+1):
    if mi > left[i] + right[i]:
        mi = left[i] + right[i]
print(mi)

Solution (Python):
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))

n = ii()
s = input()
left = s[1:].count('W')

dp = [[0,left]]
ans = n-1-left
last = s[0]
for i in s[1:]:
    ri,le = dp[-1]
    if i == 'W':
        le -= 1
    if last == 'E':
        ri += 1
    dp.append([ri,le])
    last = i
    ans = min(ans,n-1-ri-le)
print(ans)

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

EAST = []
WEST = []
e = 0
w = 0
for s in S:
  if s == "E":
    e += 1
  else:
    w += 1
  EAST.append(e)
  WEST.append(w)
  
ans = N - 1
for i in range(N):
  c = 0
  if i > 0:
    c += WEST[i-1]
  if i < N - 1:
    c += EAST[N-1] - EAST[i]
  
  ans = min(c,ans)

print(ans)

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

ans = 100000000
east = 0
west = 0
for i in range(1, n):
    if l[i] == 'E':
        east+=1
ans = east

if l[0] == 'E':
    east+=1

for i in range(1, n):
    if l[i-1] == 'W':
        west+=1
    elif l[i-1] == 'E':
        east-=1
    if l[i] == 'E':
        ans = min(ans, west + east-1)
    else:
        ans = min(ans, west + east)
print(ans)

Solution (Python):

from collections import Counter, defaultdict
n = int(input())
s = input()

left = defaultdict(int)
right = Counter(s)

ans = float('inf')
for i in range(n):
    t = s[i]
    right[t] -= 1
    l = left['W']
    r = right['E']
    ans = min(ans, l + r)
    left[t] += 1
print(ans)

Solution (Python):
import numpy as np

n=int(input())
l=np.array(list(x for x in input()))
w_left=0
e_right=n-(len(l[l=='W']))
ans=n

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

print(ans)

Solution (Python):
n = int(input())
s = input()
from collections import Counter
c = Counter([i for i in s])
left_w = 0
right_e = c['E']
if s[0] == 'E':
    right_e -= 1

ans = left_w + right_e
for i in range(1, len(s)):
    if s[i-1] == 'W':
        left_w += 1
    if s[i] == 'E':
        right_e -= 1
    ans = min(ans, left_w+right_e)
print(ans)

Solution (Python):
import sys











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

def main():
    n = int(stdin.readline().rstrip())
    s = str(stdin.readline().rstrip())
    
    
    
    
    
    
    ans = n
    dp = [0 for j in range(n+1)]
    for i in range(n):
        if s[i] == "W":
            dp[i+1] = dp[i] + 1
        else:
            dp[i+1] = dp[i]
    for i in range(n):
        ans = min (ans, dp[i] + ( (n-i-1) - (dp[n] - dp[i+1]) ))
    print(ans)


main()

Solution (Python):
n = int(input())
s = list(input())
import collections
c = collections.Counter(s)
e = c["E"]
w = 0
ans = float("Inf")
for i in range(n):
    if s[i] == "E":
        ans = min(ans,e+w-1)
    else:
        ans = min(ans,e+w)
    if s[i] == "W":
        w += 1
    else:
        w -= 1
print(ans)

Solution (Python):
N = int(input())
S = input()
EE = S.count("E")
WE = S.count("W")
EW = 0
WW = 0
ans = N

for i in range(N):
  if S[i] == "W":
    ans = min(ans,EE+WW)
    WE -= 1
    WW += 1
  else:
    ans = min(ans,EE+WW-1)
    EE -= 1
    WE += 1
print(ans)

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

Solution (Python):
from itertools import accumulate
n = int(input())
s = input()
l = list(accumulate([1 if i == 'E' else 0 for i in s]))
ans = n
for i in range(n):
    a = l[-1] - l[max(i-1,0)]
    b = i - l[i]
    ans = min(ans, a+b)
print(ans)

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

count = S.count('E')

if S[0] == 'E':
	count-=1

countmin = count

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

print(countmin)

Solution (Python):
n = int(input())
s = input()
li = []
a = s[1:].count("E")
li.append(a)

for i in range(n-1):
    if s[i] == "E" and s[i+1] == "E":
        a -= 1
        li.append(a)
    elif s[i] == "W" and s[i+1] == "W":
        a += 1
        li.append(a)
print(min(li))

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

tmp = 0

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

rlt = tmp

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

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

ans = [0]*n
temp = 0
for i in range(1,n):
    if S[i-1] == "W":
        temp += 1
    ans[i] += temp
    
S.reverse()
ans.reverse()

temp = 0
for i in range(1,n):
    if S[i-1] == "E":
        temp += 1
    ans[i] += temp

ans.reverse()
print(min(ans))

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

s = input()
l = [1 if i=="W" else 0 for i in s]
ac = [0]*(N+1)

for i in range(1, N+1):
    ac[i] = ac[i-1]+l[i-1]
ans = N
for i in range(1, N+1):
    lw = ac[i-1]
    le = i-1-lw
    rw = ac[N]-ac[i]
    re = N-i-rw
    ans = min(ans, lw+re)

print(ans)

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

w_left = 0
e_right = S[1:].count("E")
ans = float('inf')
for i in range(N):
    if i != 0:
        if S[i] == "E":
            e_right -= 1
        if S[i-1] == "W":
            w_left += 1
    ans = min(ans, w_left + e_right)
print(ans)

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

from collections import deque
w=[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]
e=[0]*n

for j in range(n-2,-1,-1):
    if s[j+1]=='E':
        e[j]=e[j+1]+1
    else:
        e[j]=e[j+1]

ans=10**9

for k in range(n):
    ans =min(ans,e[k]+w[k])

print(ans)

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

m = S[1:].count("E")
a = [m]

for i in range(N- 1):
  if(S[i] == "E" and S[i+ 1] == "E"):
    m-= 1
    a.append(m)
  if(S[i] == "W" and S[i+ 1] == "W"):
    m+= 1
    a.append(m)
    
print(min(a))

Solution (Python):
def main():
  _, s = open(0).read().split()
  w, res = 0, 0
  for i in s:
    if i == 'W':
      w += 1
    elif w:
      w -= 1
      res += 1
    else:
      pass
  return res


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

Solution (Python):
_, s = open(0).read().split()
left, right = [0], []
lw, re = 0, 0
for i in s:
  if i == 'W':
    lw += 1
  left += [lw]
for i in s[::-1]:
  if i == 'E':
    re += 1
  right += [re]
print(min(i + j for i, j in zip(left[:-1], right[-2::-1] + [0])))

Solution (Python):
import numpy as np

n = int(input())
s = input()
e_cnt_list = [0 for _ in range(n)]
w_cnt_list = [0 for _ in range(n)]
e_cnt = 0
w_cnt = 0
for i in range(1, n):
    if s[i-1] == 'W':
        w_cnt += 1
    w_cnt_list[i] = w_cnt
    if s[-i] == 'E':
        e_cnt += 1
    e_cnt_list[-i-1] = e_cnt
print(np.min(np.array(w_cnt_list) + np.array(e_cnt_list)))

Solution (Python):
def main():
    n = int(input())
    s = input()
    r = []
    wCount = 0
    eCount = s.count("E")
    for v in s:
        if v == "W":
            wCount += 1
        r.append(wCount)
        r[-1] += eCount
        if v == "E":
            eCount -= 1
    
    print(min(r)-1)
    
if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
s = list(input())
w = []
e = []
x = 0
y = 0
ans = 1000000


for c in s:
    if c == "W":
        x += 1
    else:
        y += 1
    w.append(x)
    e.append(y)

for i in range(n):
    if i == 0:
        z = e[n-1] - e[i]
    elif i == n-1:
        z = w[i-1]
    else:
        z = w[i-1] + e[n-1] - e[i]
    if z < ans:
        ans = z

print(ans)

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

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

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

RE = S.count("E")
RW = N - RE
LE = 0
LW = 0

def f(x):
  global RE, RW
  if x == "E":
    RE -= 1
  else:
    RW -= 1

def g(x):
  global LE, LW
  if x == "E":
    LE += 1
  else:
    LW += 1

for i in S:
  f(i)
  ans = min(ans, LW+RE)
  g(i)

print(ans)

Solution (Python):
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction



mod = int(pow(10, 9) + 7)
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]







n=l()[0]
s=input()

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

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

E=[0]*n
W=[0]*n
if s[-1]=="E": E[-1]=1
if s[0]=="W" : W[0]=1


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

Solution (Python):
import sys

input = sys.stdin.readline


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

    n_E = S.count("E")

    ans = N
    n_W = 0
    for i in range(N):
        if S[i] == "E":
            n_E -= 1
        num = n_E + n_W
        if num < ans:
            ans = num
        if S[i] == "W":
            n_W += 1

    print(ans)


if __name__ == "__main__":
    main()

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

sumW=s[1:].count("W")
sumE=s[1:].count("E")

p=[]
e=sumE
w=0
p.append(e)
for i in range(n-1):
    
    if s[i]=="W":
        w+=1
    if s[i+1]=="E":
        e-=1
    p.append(w+e)
    
print(min(p))

Solution (Python):
N = int(input())
S = input()
E = [0]*N
W = [0]*N
if S[0] == 'E':
    E[0] = 1
else:
    W[0] = 1
for i in range(1,len(S)):
    if S[i] == 'E':
        E[i] = E[i-1]+1
        W[i] = W[i-1]
    else:
        W[i] = W[i-1]+1
        E[i] = E[i-1]
MIN = float('inf')
for i in range(N):
    if i == 0:
        MIN = min(MIN,E[N-1]-E[0])
    elif i == N-1:
        MIN = min(MIN,W[N-2])
    else:
        MIN = min(MIN,W[i-1]+E[N-1]-E[i])
print(MIN)

Solution (Python):
N = int(input())
S = input()
tmpMin = len(S.replace('W',''))
tmpIdx = -1
tmp = tmpMin
for i in range(N):
    if S[i] == 'E':
        tmp -= 1
    else:
        tmp += 1
    if tmp < tmpMin:
        tmpMin = tmp
        tmpIdx = i

print(tmpMin)

Solution (Python):
def answer(s):
    dic = {
        'left' : { 'W': 0, 'E': 0 },
        'right' : { 'W': 0, 'E': 0 } 
    }
    for i in range(1, len(s)):
        dic['right'][s[i]] += 1
    leader = s[0]
    _min = dic['right']['E']
    for i in range(1, len(s)):
        leader = s[i]
        dic['left'][s[i-1]] += 1
        dic['right'][leader] -= 1
        _min = min(_min, dic['left']['W']+dic['right']['E'])
    return _min

n = int(input())
s = input()
print(answer(s))

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


W = [[] for _ in range(N)]
E = [[] for _ in range(N)]

prev_w = 0
prev_e = 0
for i, s in enumerate(S):
    if s == 'W':
        W[i] = prev_w + 1
    else:
        W[i] = prev_w
    prev_w = W[i]

for i, s in enumerate(S[::-1]):
    if s == 'E':
        E[-i] = prev_e + 1
    else:
        E[-i] = prev_e
    prev_e = E[-i]
        
ans = len(S)
for i in range(N):
    if i == 0:
        cost = E[i+1]
    elif i == N-1:
        cost = W[i-1]
    else:
        cost = W[i-1] + E[i+1]
    ans = min([cost, ans])
print(ans)

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

    rE = S.count('E')  
    rW = N - rE

    lE = 0
    lW = 0

    c = N

    for s in S:
        if s == 'E':
            rE -= 1
            f = 1
        else:
            rW -= 1
            f = -1

        candidate = rE + lW
        

        if candidate < c:
            c = candidate

        if f == 1:
            lE += 1
        else:
            lW += 1

    print(c)


solver()

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

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

E=[0 for i in range(n)]
W=[0 for i in range(n)]

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

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

mini=10**8
for i in range(n):
    mini=min(mini,E[i]+W[i])

print(mini)

Solution (Python):

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

w_num = S.count('W')
e_num = S.count('E')

rev_num = S[1:].count('E')
rev_num_list = [rev_num]

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

print(min(rev_num_list))

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

Solution (Python):
N=int(input())
S=list(input())
nsums=[0]*N

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

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

  min_sum = min(min_sum,nsums[i])
print(min_sum)

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

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

sum_w = [0]
sum_e = [0]

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

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


sumu = [x+y for x, y in zip(sum_w, sum_e)]

print(min(sumu))

Solution (Python):
n = int(input())
s = str(input())
r = {'E': s.count('E'), 'W': s.count('W')}
l = {'E': 0, 'W': 0}
m = 10 ** 10
for i in range(n):
    r[s[i]] -= 1
    m = min(m, r['E'] + l['W'])
    l[s[i]] += 1
print(m)

Solution (Python):
import numpy as np

n = int(input())
s = list(map(str, input().rstrip()))

dp_e = np.array([0] + [int(x == "E") for x in s])
dp_e = (dp_e.sum() - dp_e.cumsum())[1:]
dp_s = np.array([0] + [int(x == "W") for x in s])
dp_s = dp_s.cumsum()[:-1]

ans = (dp_e + dp_s).min()

print(ans)

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

R = len( S.replace("W", "") ) 
L = 0
ret = L + R 
for i in range( N ):
  if S[ i ] == "E":
    R -= 1
  ret = min( ret, L + R )
  if S[ i ] == "W":
    L += 1

print( ret )

Solution (Python):
input();s=input();m=c=s.count("E")
for t in s:c-=t<"W";m=min(m,c);c+=t>"E"
print(m)

Solution (Python):
from itertools import*;input();s=input();print(min(w+e for w,e in zip(*[([0]+list(accumulate(map(int,s[::a].translate(str.maketrans("EW","01"[::a])))))[:-1])[::a]for a in[-1,1]])))

Solution (Python):
from itertools import*
input();s = input()
b, c = [([0]+list(accumulate(map(int, s[::a].translate(str.maketrans("EW","01"[::a])))))[:-1])[::a] for a in [-1,1]]
print(min(x+y for x, y in zip(b, c)))

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

left_E, left_W = 0, 0
right_E, right_W = list_S.count("E"), list_S.count("W")
ans = pow(10, 18)

for i in range(n):
    if list_S[i] == "E":
        right_E -= 1
        ans = min(ans, right_E + left_W)
        left_E += 1
    elif list_S[i] == "W":
        right_W -= 1
        ans = min(ans, right_E + left_W)
        left_W += 1

print(ans)

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

west_count = 0
east_count = 0
ans = 10**18
adj = 0

for i in s:
    if i == 'W':
        west_count += 1
    else:
        east_count += 1

for i in range(n):
    tmp = s[i]
    if tmp == 'W':
        west_count -= 1
    else:
        east_count -= 1

    if ans > east_count + adj:
        ans = east_count + adj

    if tmp == 'W':
        adj += 1

print(ans)

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

E, W = [], []
e, w = 0, 0
for s in S:
    if s == 'E':
        e += 1
    else:
        w += 1
    E.append(e)
    W.append(w)

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

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

ans = n

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

Solution (Python):
def myAnswer(N:int,S:str) -> int:
   rightE = S.count("E")
   leftW = 0
   ans = 10**10
   for s in S:
      if(s == "W"):
         ans = min(ans,rightE + leftW)
         leftW += 1
      else:
         rightE -= 1
         ans = min(ans,rightE + leftW)
   return ans

def modelAnswer():
   return
def main():
   N = int(input())
   S = (input())
   print(myAnswer(N,S))
if __name__ == '__main__':
   main()

Solution (Python):
def myAnswer(N:int,S:list) -> int:
   rightW = S.count("W")
   rightE = N - rightW
   leftW = 0
   leftE = 0
   ans = 10**10
   for s in S:
      if(s == "W"):
         rightW -= 1
         ans = min(ans,rightE + leftW)
         leftW += 1
      else:
         rightE -= 1
         ans = min(ans,rightE + leftW)
         leftE += 1
   return ans

def modelAnswer():
   return
def main():
   N = int(input())
   S = list(input())
   print(myAnswer(N,S))
if __name__ == '__main__':
   main()

Solution (Python):


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

P = [0 for i in range(N)]

w_c = S.count('W')
e_c = S.count('E')
if S[0] == 'W':
    P[0] = {'W_l':0, 'W_r':w_c-1, 'E_l':0, 'E_r':e_c}
else:
    P[0] = {'W_l':0, 'W_r':w_c, 'E_l':0, 'E_r':e_c-1}


for i in range(1, N):
    b = P[i-1]
    if S[i] == 'W':
        if S[i-1] == 'W':
            P[i] = {'W_l':b['W_l']+1, 'W_r':b['W_r']-1, 'E_l':b['E_l'], 'E_r':b['E_r']}
        else:
            P[i] = {'W_l':b['W_l'], 'W_r':b['W_r']-1, 'E_l':b['E_l']+1, 'E_r':b['E_r']}

    else: 
        if S[i-1] == 'W':
            P[i] = {'W_l':b['W_l']+1, 'W_r':b['W_r'], 'E_l':b['E_l'], 'E_r':b['E_r']-1}
        else:
            P[i] = {'W_l':b['W_l'], 'W_r':b['W_r'], 'E_l':b['E_l']+1, 'E_r':b['E_r']-1}


ans = 10**9
for i in range(N):
    ans = min(P[i]['W_l']+P[i]['E_r'], ans)
print(ans)

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

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

ans = 10000000000

for i in range(n):
    if i == 0:
        a = 0
    else:
        a = i - t[i-1]
    b = t[n-1] - t[i]
    c = a + b

    ans = min(ans, c)

print(ans)

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

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

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

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    N = int(rl())
    S = rl().rstrip()
    
    accw, acce = [0] * (N + 1), [0] * (N + 1)
    for i in range(N):
        accw[i + 1] = accw[i] + (S[i] == 'W')
        acce[i + 1] = acce[i] + (S[i] == 'E')
    
    ans = 10 ** 6
    for i in range(N):
        ans = min(ans, acce[-1] - acce[i + 1] + accw[i])
    print(ans)


if __name__ == '__main__':
    solve()