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()
ans = n
w = [0 for i in range(n)]
e = [0 for i in range(n)]
m = [0 for i in range(n)]
for i in range(1,n):
    if s[i-1] == "W":
        w[i] = w[i-1] + 1
    else:
        w[i] = w[i-1]
for i in range(n-2,-1,-1):
    if s[i+1] == "E":
        e[i] = e[i+1] + 1
    else:
        e[i] = e[i+1]
for i in range(n):
    m[i] = e[i] + w[i]
print(min(m))

Solution (Python):
import numpy as np

N = int(input())
s = input()
w = np.array(list(map(lambda x: 1 if x == 'W' else 0, s)))
e = np.array(list(map(lambda x: 1 if x == 'E' else 0, s)))
w = np.cumsum(w)


e = np.cumsum(e[::-1])[::-1]


ans = []
for i in range(N):
    num = 0
    if 0 < i: num += w[i - 1]
    if i < N - 1: num += e[i + 1]
    ans.append(num)
print(min(ans))

Solution (Python):
import numpy as np

N = int(input())
s = input()
w = np.array(list(map(lambda x: 1 if x == 'W' else 0, s)))
e = np.array(list(map(lambda x: 1 if x == 'E' else 0, s)))
w = np.cumsum(w)

e = e[::-1]
e = np.cumsum(e)

e = e[::-1]
ans = []
for i in range(N):
    num = 0
    if 0 < i: num += w[i - 1]
    if i < N - 1: num += e[i + 1]
    ans.append(num)
print(min(ans))

Solution (Python):
import numpy as np

N = int(input())
s = input()
w = np.array(list(map(lambda x: 1 if x == 'W' else 0, s)))
e = np.array(list(map(lambda x: 1 if x == 'E' else 0, s)))
w = np.add.accumulate(w)
e = e[::-1]
e = np.add.accumulate(e)
e = e[::-1]
ans = []
for i in range(N):
    num = 0
    if 0 < i: num += w[i - 1]
    if i < N - 1: num += e[i + 1]
    ans.append(num)
print(min(ans))

Solution (Python):
from itertools import accumulate
N = int(input())
s = input()
w = map(lambda x: 1 if x == 'W' else 0, s)
e = list(map(lambda x: 1 if x == 'E' else 0, s))
e.reverse()
w = list(accumulate(w))
e = list(accumulate(e))
e.reverse()


ans = []
for i in range(N):
    num = 0
    if 0 < i: num += w[i - 1]
    if i < N - 1: num += e[i + 1]
    ans.append(num)

print(min(ans))

Solution (Python):
from itertools import accumulate
N = int(input())
s = input()
w = list(map(lambda x: 1 if x == 'W' else 0, s))
e = list(map(lambda x: 1 if x == 'E' else 0, s))
e.reverse()
w = list(accumulate(w))
e = list(accumulate(e))
e.reverse()


ans = []
for i in range(N):
    num = 0
    if 0 < i: num += w[i - 1]
    if i < N - 1: num += e[i + 1]
    ans.append(num)

print(min(ans))

Solution (Python):
from itertools import accumulate
N = int(input())
s = input()
w = list(map(lambda x: 1 if x == 'W' else 0, s))
e = list(map(lambda x: 1 if x == 'E' else 0, s))
e.reverse()
w = list(accumulate(w))
e = list(accumulate(e))
e.reverse()


ans = []
for i in range(N):
    num = 0
    if 0 < i: num += w[i - 1]
    if i < N - 1: num += e[i + 1]
    ans.append(num)

print(min(ans))

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


l=[0]
l_tmp=0
for i in range(n):
    if s[i]=="E":
        l_tmp+=1
        l.append(l_tmp)
    else:
        l.append(l_tmp)



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

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

LtoR=[0]
RtoL=[0]
tmpLtoR=0
tmpRtoL=0
for i in range(N):
    
    if S[i]=='W':
        tmpLtoR+=1
    LtoR.append(tmpLtoR)
    if S[N-1-i]=='E':
        tmpRtoL+=1
    RtoL.append(tmpRtoL)
LtoR.append(0)
RtoL.append(0)
RtoL.reverse()

total=N
for i in range(1,N+1):
    
    total=min(total,LtoR[i-1]+RtoL[i+1])
print(total)

Solution (Python):
N = int(input())
s = input()
lp = [0] * N
rp = [0] * N
for i in range(N):
    if i != 0:
        lp[i] = lp[i-1]
        if s[i-1] == 'W':lp[i]+=1
for j in range(N-1,-1,-1):
    if j != N-1:
        rp[j] = rp[j+1]
        if s[j+1] == 'E':rp[j]+=1
g = 9999999999999999
for i in range(N):g=min(g,lp[i]+rp[i])
print(g)

Solution (Python):
from functools import reduce
import math

def main():
    
    
    
    

    
    
    
    
    
    

    
    

    
    
    
    
    
    
    
    
    
    INF = float("inf")

    
    MOD = 10**9+7
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    
    
    
    
    N = int(input())
    S = list(_ for _ in input())
    l = S.count('E')
    r = 0
    ans = min(S.count('E'),S.count('W'))
    for i in range(N):
        if i != N-1:
            if S[i] == 'E':
                l = l - 1
        if i != 0:                        
            if prev == 'W':
                r = r + 1
        prev = S[i]
        ans = min(ans,l+r)
        
    print(ans)
    
if __name__ == '__main__':
    main()

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

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

Solution (Python):
N = int(input())
s = input().split()
s = list(s[0])
right = 0
left = 0
for i in range(1,N):
    if s[i] == "E":
        right += 1
ans = right
for i in range(1, N):
    if s[i] == "E":
        right -= 1
    if s[i-1] == "W":
        left += 1
    if ans > right + left:
        ans = right + left
print(ans)

Solution (Python):




from itertools import accumulate

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

x = [0]+[1 if s[i] == 'W' else 0 for i in range(n)]
y = [1 if s[i] == 'E' else 0 for i in range(n)] + [0]

mx = list(accumulate(x))
my = list(accumulate(y[::-1]))
my = my[::-1]
mi = float('inf')
for i in range(n+1):
    a = mx[i]
    b = my[i]
    mi = min(mi,a+b)
print(mi)

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

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

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

print(min(ans))

Solution (Python):
N = int(input())
S = input()
L = [0]
R = [0]
for i in range(1, N):
	if S[i-1] == 'W':
		L.append(L[-1]+1)
	else:
		L.append(L[-1])
	if S[-i] == 'E':
		R.append(R[-1]+1)
	else:
		R.append(R[-1])
R.reverse()
min = 5000000000000000
for i in range(0, N):
	if L[i]+R[i] < min:
		min = L[i]+R[i]
print(min)

Solution (Python):
import sys


fin = sys.stdin.readline
N = int(fin())
S = fin().rstrip("\n")
num_wests_from_left = [0] * N
cur_num_wests = 0
for i, d in enumerate(S):
    cur_num_wests += 1 if d == 'W' else 0
    num_wests_from_left[i] = cur_num_wests

num_easts_from_right = [0] * N
cur_num_easts = 0
for i in reversed(range(N)):
    d = S[i]
    cur_num_easts += 1 if d == 'E' else 0
    num_easts_from_right[i] = cur_num_easts

min_num_changing = N - 1
for i in range(N):
    num_changing = (num_wests_from_left[i - 1] if i > 0 else 0) + (num_easts_from_right[i + 1] if i < N - 1 else 0)
    min_num_changing = min(min_num_changing, num_changing)
print(min_num_changing)

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

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

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

print(ans)

Solution (Python):
n=int(input())
s=input()
a=b=s[1:].count('E')

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

Solution (Python):
n = int(input())
S = input()
NE = [0 for i in range(n)]
NE[0] = 1 if S[0] == "W" else 0
for i in range(1,n):
    NE[i] = NE[i-1] + (1 if S[i] == "W" else 0)

ans = 10**10
for i in range(n):
    nw = NE[i-1] if i>0 else 0
    ne = n-i-(NE[n-1]-nw) + (-1 if S[i]=="E" else 0)
    ans = min(ans, nw+ne)
print(ans)

Solution (Python):
n = int(input())
s = input()
C = [0]*(n)
C[0] = 1 if s[0]=="E" else 0
for i in range(1,n):
    C[i] = C[i-1] + (1 if s[i]=="E" else 0)
ans = n
for i in range(n):
    l = i - C[i-1] if i >0 else 0
    r = C[-1] - C[i] if i < n-1 else 0
    tmp =  l + r
    ans =min(ans,tmp)
print(ans)

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

Solution (Python):
n = int(input())
sList = list(input())
total = 0
for i in range(1, n):
    if sList[i] == "E":
        total+= 1
minNum = total
for i in range(1, n):
    if sList[i-1] == "W":
        total += 1
    if sList[i] == "E":
        total -= 1
    if total < minNum:
        minNum = total
print(minNum)

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

e = [0]


for i in range(n):
    if s[i]=="E":
        e.append(e[i]+1)
    else:
        e.append(e[i])

w = [0]

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



ans = 10**9
for i in range(n):
    ans = min(ans,(n-1)-e[i]-w[(n-1)-i])
print(ans)

Solution (Python):
N = int(input())
S = input()
acc_E = [0] * (N + 1)
for i in range(N):
    acc_E[i + 1] = acc_E[i] + (S[i] == 'E')
result = N
for leader in range(N):
    left_W = leader - acc_E[leader]
    right_E = acc_E[N] - acc_E[leader + 1]
    result = min(result, left_W + right_E)
print(result)

Solution (Python):
from collections import Counter

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

count_E = Counter(S)['E']
count_W = Counter(S)['W']

right = 0
left = count_E
tmp = N
for i in range(N):
    if S[i] == 'E':
        left -= 1
    if right + left < tmp:
        tmp = right + left
    if S[i] == 'W':
        right += 1
print(tmp)

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

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

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

    if count < min:
        min = count

print(min)

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

Solution (Python):


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


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


print(ans)

Solution (Python):
N = int(input())
S = input()
hdg = S[1:N].count('E')
hdg_chgd = hdg
hdg_chg_nrs =[]
for i in range(1,N):
    if S[i] == 'E':
        hdg_chgd -= 1
    if S[i-1] == 'W':
        hdg_chgd += 1
    hdg_chg_nrs.append(hdg_chgd)
hdg_chgd_f = min(hdg_chg_nrs)
print(min(hdg, hdg_chgd_f))

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

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

calc = ans

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

print(ans)

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

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

Solution (Python):
n = int(input());
s = input();
point = 0;
for i in range(1,n) :
	if s[i] == 'E' :
		point += 1;
ans = point;
for i in range(1,n) :
	if s[i-1] == 'W' :
		point += 1;
	if s[i] == 'E' :
		point -= 1;
	if point < ans :
		ans = point;
print(ans);

Solution (Python):

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


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
s = input()
d = []

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

d.append(num_first)

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

print(min(d))

Solution (Python):
N = int(input())
S = str(input()) + 'W'

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

print(ans)

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

ans=S[1:].count('E')
c=S[1:].count('E')
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()

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

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

print(min(ans))

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

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

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

L = []  
for i in range(1, N+1):
    num = w[i-1] + e[N] - e[i]
    L.append(num)

print(min(L))

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

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


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

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

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

e = [0]*N
for i in reversed(range(N)):
    
    
    if i==N-1:
        continue

    if S[i+1] == 'W':
        e[i] = e[i+1]+1
    else:
        e[i] = e[i+1]

ans = 1
for i in range(N):
    ans = max(ans, e[i] + w[i]+1) 

print(N - ans)

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

L = 0
R = s.count("E")
ans = L + R

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

print(ans)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n=I()
  s=S()

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

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

  
  

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

  return mn

print(main())

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

Solution (Python):

N = int(input())

S = input()

S_re = "".join(list(reversed(S)))

left = []
right = []

now = 0
for i in range(N):

    left.append(now)

    if S[i] == "W":
        now += 1

now = 0
for i in range(N):

    right.append(now)

    if S_re[i] == "E":
        now += 1

right.reverse()


ans = N
for i in range(N):

    ans = min(ans, left[i] + right[i] )
        

print (ans)

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

cnt_right = S[1:].count('E')
cnt_left = 0
ans = cnt_right + cnt_left

for ichi in range(len(S)-1):
    if S[ichi] == 'W':
        cnt_left += 1
    if S[ichi+1] == 'E':
        cnt_right -= 1
    ans = min(ans, cnt_right + cnt_left)
    
print(ans)

Solution (Python):

N = input()

A1 = [ n=='E' for n in input()]


minimum = sum(A1[1:])
count = minimum

for i in range(1,int(N)):
    if(not A1[i-1]):
        count += 1
    if(A1[i]):
        count -= 1
    if(count<minimum):
        minimum = count
print(minimum)

Solution (Python):
import numpy as np
import sys
buf = sys.stdin.buffer
N = int(buf.readline())
S = np.frombuffer(buf.read(N), dtype='S1')
is_E = (S == b'E')
is_W = ~is_E

left_E = is_E.cumsum() - is_E
left_W = np.arange(N) - left_E
right_E = is_E.sum() - left_E - is_E
right_W = is_W.sum() - left_W - is_W

answer = (left_W + right_E).min()
print(answer)

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

N = int(input())
S = list(input().strip())
t = [[0, 0] for _ in range(N)]

w = 0
e = 0
for i in range(0, N):
    if i != 0 and S[i] == "E":
        e += 1
        
    if i != N - 1 and S[i] == "W":
        w += 1
        
t[0][1] = e
t[-1][0] = w

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

ans = 10 ** 18
for v in t:
    ans = min(ans, v[0] + v[1])
    
print(ans)

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

higashi=[0]*(n+1)
nishi=[0]*(n+1)

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

ans=[0]*(n+1)    
for i in range(n+1):
    ans[i]=nishi[i]+higashi[i]

print(min(ans))

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

L = 0
R = s[1:N].count("E")
ans = L + R

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

print(ans)

Solution (Python):
N=int(input())
S=input()
move=S[1:].count("E")
ans=[move]

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

Solution (Python):
from collections import Counter
N = int(input())
S = input()

east = []
west = []
tmp_e = 0
tmp_w = 0
for i in range(N):
    if S[i] == 'W':
        tmp_e += 1
    east.append(tmp_e)
    if S[N-1-i] == 'E':
        tmp_w += 1
    west.append(tmp_w)

mn = N
for i in range(N):
    e = east[i-1] if i else 0
    w = west[N-1-i] if i != N-1 else 0
    if e + w < mn:
        mn = e + w
print(mn)

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

l = [0]*n
wl = [0]*n
el = [0]*n

for i in range(n-1):
  if i == 0:
    wl[i+1] = S[i].count("W")
  else:
    wl[i+1] = S[i].count("W")+wl[i]

for i in reversed(range(1,n)):
  if i == n:
    el[i-1] = S[i].count("E")
  else:
    el[i-1] = S[i].count("E")+el[i]
    
for i in range(n):
  l[i] = wl[i] + el[i]
  
print(min(l))

Solution (Python):
N = int(input())
S = input()
we = []
w = 0
e = 0

for i in range(N):
    if S[i] == "W":
        w += 1
    else:
        e += 1
    we.append([w,e])

e_count = we[-1][1]
ans = min(e_count,we[-1][0])
for i in range(N):
    ans = min(we[i][0] + abs(we[i][1] - e_count), ans)
print(ans)

Solution (Python):
N = int(input())
S = input()
we = []
w = 0
e = 0

for i in range(N):
    if S[i] == "W":
        w += 1
    else:
        e += 1
    we.append([w,e])

e_count = we[-1][1]
ans = min(e_count,we[-1][0])
for i in range(N):
    ans = min(we[i][0] + abs(we[i][1] - e_count), ans)
print(ans)

Solution (Python):


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

line = []
E = 0
W = 0
error = 0

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

counter = 0

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

    line.append(counter)

print(min(line))

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

ans = len([1 for d in S[1:] if d == 'E'])
cnt = ans
for i in range(1, N):
    if S[i] == 'E': cnt -= 1
    if S[i-1] == 'W': cnt += 1
    if ans > cnt: ans = cnt
print(ans)

Solution (Python):
N = int(input())
S = input()
E = S.count('E')
W = 0
res = E
for s in S:
    if s == 'E':
        E -= 1
    else:
        W += 1
    res = min(res,W+E)
print(res)

Solution (Python):
from collections import Counter
n = int(input())
s = input()
ans_list = [0]*n
e_num = Counter(s[1:])['E']
ans = e_num
for i in range(1, n):
    if s[i] == 'E':
        e_num -= 1
    if s[i-1] == 'W':
        e_num += 1
    ans = min(e_num, ans)
print(ans)

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

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

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n=I()
  l=list(S())

  l1=[0]*n
  for i in range(n):
    if l[i]=='E':
      l1[i]+=1

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

  l2=[]
  for i in range(n):
    l2.append(i+1-l1[i])

  mn=inf
  for i in range(n):
    if l[i]=='W':
      a=l2[i]-1
    else:
      a=l2[i]
    mn=min(mn,l1[-1]-l1[i]+a)

  return mn

print(main())

Solution (Python):
N = int(input())
S = input()
E = S.count('E')
res = E
W = 0
for s in S:
    if s == 'E':
        E -= 1
    else:
        W += 1
    res = min(res,W+E)
print(res)

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

left_w = [0 for i in range(n + 2)]
right_e = [0 for i in range(n + 2)]

for idx, ch in enumerate(s, 1):
    if ch == 'E':
        right_e[idx] = 1
    else:
        left_w[idx] = 1

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

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

min_turn = n
for i in range(1, n + 1):
    turn = left_w[i - 1] + right_e[i + 1]
    min_turn = min(min_turn, turn)

print(min_turn)

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

left_w = [0 for i in range(n + 2)]
right_e = [0 for i in range(n + 2)]

for idx, ch in enumerate(s, 1):
    if ch == 'E':
        right_e[idx] = 1
    else:
        left_w[idx] = 1

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

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

min_turn = n
for i in range(1, n + 1):
    turn = left_w[i - 1] + right_e[i + 1]
    min_turn = min(min_turn, turn)

print(min_turn)

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

tobi = lambda x: 1 if x=='E' else 0
S = list(map(tobi, list(S)))

ans = [sum(S[1:])]
for i in range(1, N):
    check1, check2 = 0, 0
    if S[i] == 1:
        check1 = 1
    if S[i-1] == 0:
        check2 = 1
    ans.append(ans[-1] - check1 + check2)
print(min(ans))

Solution (Python):
N = int(input())
S = input()
total = [0] * (N + 1)
for i in range(N):
    if S[i] == 'W':
        total[i + 1] = total[i] + 1
    else:
        total[i + 1] = total[i]

min_num = min(total[i] + (N - i - 1) - (total[N] - total[i + 1]) for i in range(N))

print(min_num)

Solution (Python):
import math
import numpy as np
def main(N, S):
    e_cum_list = []
    w_cum_list = []
    e_sum = 0
    w_sum = 0
    for char in S:
        if char == 'E':
            e_sum +=1
        else:
            w_sum +=1
        e_cum_list.append(e_sum)
        w_cum_list.append(w_sum)

    min_val = 10**6
    for i in range(N):
        
        if i - 1 < 0:
            w_cum = 0
        else:
            w_cum = w_cum_list[i - 1]
        
        e_cum = e_cum_list[-1] - e_cum_list[i]

        ew_sum = e_cum + w_cum
        if ew_sum < min_val:
            min_val = ew_sum

    print(min_val)




if __name__ == "__main__":
    N = int(input())
    S = input()
    
    main(N, S)

Solution (Python):
n = int(input())
s = input()
es = s.count('E')
e_cnt = 0
l = [0]*n
for i in range(n):
    left = i-e_cnt
    if s[i] == 'E':
        e_cnt +=1
    right = es - e_cnt
    l[i] = right+left

print(min(l))

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

cost = S.count('E')

ans = N
for c in S:
    if c == 'E':
        cost -= 1
    else:
        if ans > cost:
            ans = cost
        cost += 1
else:
    if ans > cost:
        ans = cost
print(ans)

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

W_cnt_array = [0] * N
E_cnt_array = [0] * N

W_cnt = 0
E_cnt = 0
for i in range(N):
    if S[i] == 'W':
        W_cnt += 1
    elif S[i] == 'E':
        E_cnt += 1
    W_cnt_array[i] = W_cnt
    E_cnt_array[i] = E_cnt

prev_dir = S[0]
num = min(max(E_cnt_array), max(W_cnt_array))
for i in range(1, N):
    if prev_dir == 'E' and S[i] == 'W':
        num = min(num, W_cnt_array[i - 1] + E_cnt_array[-1] - E_cnt_array[i])
    prev_dir = S[i]
print(num)

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

ans = 3*10**5

w_cnt = [0]*n 
e_cnt = [0]*n

for i in range(n):
    if i == 0:
        if l[i] == 'W':
            w_cnt[0] = 1
    if i >= 1 and i <= n-1:
        if l[i] == 'W':
            w_cnt[i] = w_cnt[i-1] + 1
        else:
            w_cnt[i] = w_cnt[i-1]
for i in range(n)[::-1]:
    if i == n-1:
        if l[i] == 'E':
            e_cnt[i] = 1
    if 0 <= i and i <= n-2:
        if l[i] == 'E':
            e_cnt[i] = e_cnt[i+1] + 1
        else:
            e_cnt[i] = e_cnt[i+1]
for i in range(1,n-2):
    ans = min(ans,w_cnt[i-1]+e_cnt[i+1])
print(min(ans,e_cnt[0],w_cnt[n-1]))

Solution (Python):
import re

def calc(people):
    Ws = 0
    Es = people[1:].count("E")
    ans = Ws + Es

    for i in range(1, len(people)):
        if people[i] == "E":
            Es -= 1
        
        if people[i - 1] == "W":
            Ws += 1

        ans = min(ans, Ws + Es)
    
    return ans


if __name__ == "__main__":
    _ = input()
    people = input()

    print(calc(people))

Solution (Python):
N = int(input())
S = list(input())
W_count = [0] * (N + 1)
E_count = [0] * (N + 1)

for i in range(len(S)):
  W_count[i + 1] = W_count[i] + (1 if S[i] == 'W' else 0)
  E_count[i + 1] = E_count[i] + (1 if S[i] == 'E' else 0)






answer = 4 * (10 ** 5)
for i in range(len(S)):
  if i == 0:
    answer = min(answer, E_count[-1] - E_count[1])
  elif i == len(S) - 1:
    answer = min(answer, W_count[len(S) - 1] - W_count[0])
  else:
    answer = min(answer, W_count[i] - W_count[0] + E_count[-1] - E_count[i + 1])

print(answer)

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

ans = float('inf')
sum_E = [0]
sum_W = [0]
cnt_e = 0
cnt_w = 0

for i in range(N):
	if S[i] == 'E':
		cnt_e += 1
	else:
		cnt_w += 1
	sum_E.append(cnt_e)
	sum_W.append(cnt_w)

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

print(ans)

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

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

print(ans)

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

res = float('inf')
W_sum = [0]
E_sum = [S.count('E')]
for i in range(0, N):
    W_sum.append(W_sum[i] + 1 if S[i] == 'W' else W_sum[i]) 
    E_sum.append(E_sum[i] - 1 if S[i] == 'E' else E_sum[i])
    res = min(res, W_sum[i] + E_sum[i])

res = min(res, W_sum[-1] + E_sum[-1])
print(res)

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

W_sum = [0]
E_sum = [S.count('E')]
for i in range(0, N):
    W_sum.append(W_sum[i] + 1 if S[i] == 'W' else W_sum[i]) 
    E_sum.append(E_sum[i] - 1 if S[i] == 'E' else E_sum[i])

res = float('inf')
for i in range(0, N+1):
    res = min(res, W_sum[i] + E_sum[i])
    
print(res)

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

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

east = [0] 
west = [0] 
for i in range(N-1):
  east.append(east[-1] + (S[N-i-1] == 'E'))
  west.append(west[-1] + (S[i] == 'W'))
  
ans = N
for i in range(N):
  cost = east[N-i-1] + west[i]
  ans = min(ans, cost)
  
print(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):
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):
n = int(input())
s = input()
ans = 10**10
s_E = [0]
s_W = [0]
e = 0
w = 0
for i in range(n):
    if s[i] == 'E':
        e += 1
    else:
        w += 1
    s_E.append(e)
    s_W.append(w)

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

print(ans)

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


E = S.count('E')
cost = 0
result = E
for c in S:
    if c == 'W':
        cost += 1
    else:
        E -= 1

    if cost+E < result:
        result = cost+E

print(result)

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

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

W[0] += S[0] == 'W'
E[-1] += S[-1] == 'E'

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

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

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

S = input()


w = 0
e = 0
W = [0]*N
E = [0]*N
for i, c in enumerate(S):
    if c == 'W':
        w+=1
    elif c == 'E':
        e+=1
    W[i] = w
    E[i] = e

m = N
D = [0]*N
result = 0
for i in range(N):
    
    
    
    if i == 0:
        D[i] =  E[N-1] - E[i]
    elif i == N-1:
        D[i] = W[i-1]
    else:
        D[i] = W[i-1] + E[N-1] - E[i]
    if m > D[i]:
        m = D[i]
        result = i+1






print(m)

Solution (Python):
n=int(input())
S=list(input())
R=[0]
for i in range(n):
    R.append(R[i]+(S[i]=='W'))
print(min([R[i]+n-i-1-(R[-1]-R[i+1]) for i in range(n)] ))

Solution (Python):
n,s=int(input()),input()
a=[0]
for i in s:a.append(a[-1]+(i=="W"))
m=999999
for i in range(1,n+1):m=min(m,a[i-1]+((n-i)-(a[-1]-a[i])))
print(m)

Solution (Python):

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


ans = n
if n < 10:
	for i in range(n):
		ans = min((s[:i].count("W")+s[i+1:].count("E")),ans)
else:
	change = (s[1:].count("E"))
	ans = change
	for i in range(1,n):

		t = 0
		if s[i-1] == "W":
			t += 1
		if s[i] == "E":
			t -= 1
		change += t
		ans = min(ans,change)

print(ans)