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):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N =  INT()
S = input()

e = [0]*N
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]

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

ans = INF
for i in range(N):
	ans = min(ans, w[i]+e[i]) 
print(ans)

Solution (Python):

n=int(input())
s=input()
import collections
numberw=s.count("W")
numbere=n-numberw
start=0
if s[0]=="W":
    start=numbere
    
elif s[0]=="E":
    start=numbere-1
ans=n
for i in range(n-1):
    ans=min(ans,start)
    if s[i]=="W" and s[i+1]=="W":
        start+=1
    elif s[i]=="W" and s[i+1]=="E":
        start=start
    elif s[i]=="E" and s[i+1]=="W":
        start=start
    elif s[i]== "E"and s[i+1]=="E":
        start-=1  
    ans=min(ans,start)
print(ans)

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


wcum=[];wcount=0
ecum=[];ecount=0
for i in range(n):
  if s[i]=="W":
    wcount+=1
  else:
    ecount+=1
  ecum.append(ecount)
  wcum.append(wcount)
ans=ecum[n-1]-ecum[0]
for i in range(1,n):
  v=wcum[i-1]+(ecum[n-1]-ecum[i])
  if ans>v:
    ans=v
print(ans)

Solution (Python):
from sys import stdin
import bisect
from itertools import accumulate
import numpy as np

n = int(stdin.readline().rstrip())
s = stdin.readline().rstrip()

mi = 10**10

li = [0]*n
lin = [0]*n

for i in range(len(s)):
    if s[i] == "W":
        li[i] = 1
    if s[n-i-1] == "E":
        lin[i] = 1

li = np.asarray(list(accumulate(li)))
lin = np.asarray(list(accumulate(lin)))[::-1]
lis = li+lin

lis.sort()

print(lis[0]-1)

Solution (Python):
N = int(input())
S = input()
ne_r = S.count('E')
nw_l = 0
ans = N
for i,c in enumerate(S):
    if c=='E': ne_r-=1
    ans = min(ans, ne_r + nw_l)
    if c=='W': nw_l+=1
print(ans)

Solution (Python):
n=int(input())
s=input()
lst1=[0]*n
lst2=[0]*n
if s[0]=='W':
  lst1[0]=1
if s[0]=='E':
  lst2[0]=1
for i in range(1,n):
  if s[i]=='W':
    lst1[i]=lst1[i-1]+1
    lst2[i]=lst2[i-1]
  if s[i]=='E':
    lst2[i]=lst2[i-1]+1
    lst1[i]=lst1[i-1]
l=[]
l.append(lst2[n-1]-lst2[0])
for j in range(1,n-1):
  l.append(lst1[j-1]+lst2[n-1]-lst2[j])
l.append(lst1[n-2])

print(min(l))

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

leader = S[0]
right = 0
left = 0

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

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

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

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

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

Solution (Python):
N = int(input())
S = input()
 
num_E, num_W = 0, 0


min_sum_operation = N
argmin_i = 0
for i, c in enumerate(S):
    if i == 0:
        
        right = S[i + 1:]
        num_E = right.count('E')
        num_W += 1 if c == 'W' else 0
        
        
    elif c == 'W':
        num_W += 1
        
    elif c == 'E':
        num_E -= 1
        
    if min_sum_operation > num_E + num_W:
        min_sum_operation = min(min_sum_operation, num_E + num_W)
        argmin_i = i
 
    
 
if argmin_i == 0:
    print(min_sum_operation - 1)
else:
    print(min_sum_operation)

Solution (Python):
N = int(input())
S = input()
E = [0] * N
t = 0
for i, s in enumerate(S[len(S)-1:0:-1]):
  if s == 'E':
    t += 1
  E[-i-2] = t

t = 0
for i, s in enumerate(S[:len(S) - 1]):
  if s == 'W':
    t += 1
  E[i+1] += t

print(min(E))

Solution (Python):
n = int(input())
members = input()
m_e = members.count("E")
cost = m_e
costs = []
for i in range(n):
    if members[i]=="E":
        cost-=1
    if (i!=0 and members[i-1]=="W"):
        cost+=1
    costs.append(cost)
print(min(costs))

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

Solution (Python):
def main():
    N = int(input())
    S = input()
    from collections import Counter, defaultdict
    ri = defaultdict(int, dict(Counter(S)))
    le = defaultdict(int, {'W': 0, 'E': 0})
    pre = ''
    ans = 1 << 60
    for s in S:
        le[pre] += 1
        ri[s] -= 1
        ans = min(ans, le['W'] + ri['E'])
        pre = s
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):



import sys
import collections












def main():
    n = int(sys.stdin.readline().rstrip())
    s = sys.stdin.readline().rstrip()

    opposite = collections.Counter(s)
    opposite['W'] = 0
    minimum = n
    for i in range(n):
        if s[i] == 'E':
            opposite['E'] -= 1
        minimum = min(minimum, sum(opposite.values()))
        if s[i] == 'W':
            opposite['W'] += 1
    print(minimum)




if __name__ == "__main__":
    
    main()

Solution (Python):
N = int(input())
S = input()
cumsum_east = [0 for i in range(N+1)]
cumsum_west = [0 for i in range(N+1)]
for i in range(1,N+1):
    if S[i-1] == 'E':
        cumsum_east[i] = cumsum_east[i-1] + 1
    else:
        cumsum_east[i] = cumsum_east[i-1]
    if S[i-1] == 'W':
        cumsum_west[i] = cumsum_west[i-1] + 1
    else:
        cumsum_west[i] = cumsum_west[i-1]
cumsum_east = cumsum_east[1:]
cumsum_west = cumsum_west[1:]
ans = 1000000
for i in range(N):
    if i == 0:
        flip_num = cumsum_east[-1]-cumsum_east[0]
    elif i == N-1:
        flip_num = cumsum_west[-2]
    else:
        flip_num = cumsum_west[i-1] + cumsum_east[-1] - cumsum_east[i]
    if ans > flip_num:
        ans = flip_num
print(ans)

Solution (Python):
N=int(input())
S=input()
a=S[1:].count('E')
ans=a
t=1
while t<=N-1:
    if S[t-1]=='W':
        a+=1
    if S[t]=='E':
        a-=1
    ans=min(ans,a)
    t+=1
print(ans)

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

ans = n-1-sufw[1]

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

ans=min(ans,prew[n])

print(ans)

Solution (Python):
import collections
N = int(input())
S = list(input())
rightE = S.count('E')
rightW = S.count('W')
leftE = 0
leftW = 0
ans = N
for n in range(N):
    if S[n] == 'E':
        rightE -= 1
        ans = min(ans,leftW+rightE)
        leftE += 1
    else:
        rightW -= 1
        ans = min(ans,leftW+rightE)
        leftW += 1
print(ans)

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

turn_east = [0 for x in range(n)]
turn_west = [0 for x in range(n)]
total = [0 for x in range(n)]

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

    if s[n-i] == 'E':
      turn_west[n-1-i] = turn_west[n-i]+1
    else:
      turn_west[n-1-i] = turn_west[n-i]

for j in range(n):
  total[j] = turn_east[j] + turn_west[j]

print(min(total))

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

for i in range(n-1,0,-1):
    if s[i]=="E":
        right[i-1]=right[i]+1
    else:
        right[i-1]=right[i]
print(min([left[i]+right[i] for i in range(n)]))

Solution (Python):
n=int(input())
l=list(input())
lw=[0]
countw=0
counte=0
le=[0 for i in range(n)]

for i in range(1,n):
  if l[i-1]=="W":
    countw+=1
  lw.append(countw)
  if l[-i]=="E":
    counte+=1
  le[-i-1]=counte
m=n
for i in range(n):
  if m>lw[i]+le[i]:
    m=lw[i]+le[i]
print(m)

Solution (Python):
from itertools import accumulate

N=int(input())
s=str(input())


a = accumulate(c=='W' for c in 'X'+s[:-1])
b = reversed(list(accumulate(c=='E' for c in 'X'+s[-1:0:-1])))
print(min(x + y for x, y in zip(a, b)))

Solution (Python):
n = int(input())
s = str(input())
s = [str(i) for i in s]
d = int(n/2)
w, e = [], []
w.append(1) if s[0] == "W" else w.append(0)
e.append(1) if s[-1] == "E" else e.append(0)
for i in range(1, n):
    w.append(w[i-1] + 1) if s[i] == "W" else w.append(w[i-1])
    e.append(e[i-1] + 1) if s[n-1-i] == "E" else e.append(e[i-1])
e.reverse()
ans = [w[i-1] + e[i+1] for i in range(1, n-1)]
ans.insert(0, e[0])
ans.insert(-1, w[-1])
print(min(ans))

Solution (Python):
n = int(input())
string = input().rstrip("\n")

facing_west_in_west = 0
facing_east_in_east = string.count("E") - (string[0] == "E")
list_ = [facing_east_in_east]
for i in range(1, n):
    if string[i - 1] == "W":
        facing_west_in_west += 1
    if string[i] == "E":
        facing_east_in_east -= 1
    list_.append(facing_east_in_east + facing_west_in_west)
print(min(list_))

Solution (Python):
import sys


		
def main():
	a = int(input())
	s = input()
	
	sst=len([c for c in s[1:] if c == 'E'])
	ret=sst
	cc=sst
	for i, c in enumerate(s[:-1]):
		leader = s[i+1]
		if leader == 'E':
			cc -= 1
		
		if c == 'W':
			cc+=1
		
		ret = min(ret, cc)
	
	
	print(ret)
	
	

isTest=False

def pa(v):
	if isTest:
		print(v)

if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

Solution (Python):
inpl = lambda: list(map(int,input().split()))
N = int(input())
S = input()
NE = S.count('E')
ans = cur = NE
prev = S[0]
n = 0
for s in S + 'A':
    if s == prev:
        n += 1
    else:
        if prev == 'E':
            cur -= n
            if cur < ans:
                ans = cur
        else:
            cur += n
        n = 1
        prev = s
print(ans)

Solution (Python):
inpl = lambda: list(map(int,input().split()))
N = int(input())
S = input()
NE = S.count('E')
ans = cur = NE
for s in S:
    if s == 'E':
        cur -= 1
        if cur < ans:
            ans = cur
    else:
        cur += 1
print(ans)

Solution (Python):
inpl = lambda: list(map(int,input().split()))
def get_sequence(arr):
    if len(arr) == 0:
        return [], []
    elements = []
    nums = []
    n = 1
    prev = arr[0]
    for c in arr[1:]:
        if c == prev:
            n += 1
        else:
            elements.append(prev)
            nums.append(n)
            prev = c
            n = 1
    elements.append(prev)
    nums.append(n)
    return elements, nums

N = int(input())
S = input()
ele, num = get_sequence(S)
Estart = 1-int(ele[0]=='E')
NE = sum(num[Estart::2])
ans = cur = NE
for i in range(len(num)):
    if (i - Estart) % 2:
        cur += num[i]
    else:
        cur -= num[i]
        if cur < ans:
            ans = cur
print(ans)

Solution (Python):
n=int(input())
s=input()
mini=10**9
prew=[0]*n 
if s[0]=='W':
    prew[0]+=1 
pre=[0]*n 
if s[n-1]=='E':
    pre[n-1]+=1 
for i in range(1,n):
    
    prew[i]=prew[i-1]+(s[i]=='W')
for i in range(n-2,-1,-1):
    pre[i]=pre[i+1]+(s[i]=='E')
for i in range(1,n-1): 
    cost=prew[i-1]+pre[i+1]
    mini=min(mini,cost)
mini=min(mini,pre[0])
mini=min(mini,prew[n-1])
print(mini)

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

Solution (Python):


def input():
    import sys
    return sys.stdin.readline().rstrip()


def main():
    import math
    import collections
    import itertools

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

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

        ans = min(ans, w_count + e_count)
    print(ans)
    


if __name__ == '__main__':
    main()

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

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

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

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

C = S.count("E")
M = C

for i in S:
    if i == "E":
        C -= 1
    else:
        C += 1
    M = min(C,M)
print(M)

Solution (Python):
import numpy as np

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

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

print(min([x for x in np.array(W)+np.array(E)]))

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

C = S.count("E")
M = C

for i in S:
    if i == "E":
        C -= 1
    else:
        C += 1
    M = min(C,M)
print(M)

Solution (Python):
n = int(input())
s = input()
ans = 10**10
rightW = s.count('W')
rightE = len(s) - rightW
leftW = 0
leftE = 0

for i in range(len(s)):
    if s[i] == 'W':
        rightW -= 1
    else:
        rightE -= 1

    if i > 0:
        if s[i-1] == 'W':
            leftW += 1
        else:
            leftE += 1
    ans = min(ans, rightE + leftW)

print(ans)

Solution (Python):
n = int(input())
s = input()
ans = 10**10


rightW = s.count('W')
rightE = len(s) - rightW
leftW = 0
leftE = 0


for i in range(len(s)):
    
    

    if s[i] == 'W':
        rightW -= 1
    else:
        rightE -= 1

    if i > 0:
        if s[i-1] == 'W':
            leftW += 1
        else:
            leftE += 1
    temp = rightE + leftW
    ans = min(ans, temp)

print(ans)

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

W = []
w = 0
for si in S:
    if si == 'W':
        w += 1
    W.append(w)

e = 0
for i, si in enumerate(S[::-1]):
    if si == 'E':
        e += 1
    ans = min(ans, W[-1 * i - 1] + e - 1)

print(ans)

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

A = []
w = 0
for si in S:
    if si == 'W':
        w += 1
    A.append(w)

e = 0
for i, si in enumerate(S[::-1]):
    if si == 'E':
        e += 1
    ans = min(ans, A[-1 * i - 1] + e - 1)

print(ans)

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

A = []
w = 0
for si in S:
    if si == 'W':
        w += 1
    A.append([w])


e = 0
for i, si in enumerate(S[::-1]):
    if si == 'E':
        e += 1
    
    A[-1 * i - 1].append(e)





ans = N
for ai in A:
    ans = min(ans, ai[0] + ai[1] - 1)
print(ans)

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

A = []
w = 0
for si in S:
    if si == 'W':
        w += 1
    A.append([w, 100])

E = []
e = 0
for si in S[::-1]:
    if si == 'E':
        e += 1
    E.append(e)

E = E[::-1]
for i in range(N):
    A[i][1] = E[i]

ans = N
for ai in A:
    ans = min(ans, ai[0] + ai[1] - 1)
print(ans)

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

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

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

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

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

R_count = {}
R_count[N-1] = 0
for i in range(N-2,-1,-1):
    if S[i+1] == "E":
        R_count[i] = R_count[i+1] + 1
    else:
        R_count[i] = R_count[i+1]

leader_i = 0
turn_num = N+1

for i in range(N):
    
    if L_count[i] + R_count[i] < turn_num:
        leader_i = i
        turn_num = L_count[i] + R_count[i]


R_sort = sorted(R_count.items(), key=lambda x: x[0])



print(L_count[leader_i] + R_count[leader_i])

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

diff = []
e = 0
w = 0
for si in S:
    if si == 'E':
        e += 1
    else:
        w += 1
    diff.append(e - w)

i = diff.index(max(diff))


ans = S[:i].count('W') + S[i+1:].count('E')
print(ans)

Solution (Python):
N = int(input())
S = list(map(str, input()))
ret = float("inf")
west = [0 for _ in range(N + 2)]
east = [0 for _ in range(N + 2)]

for i in range(1, N + 1):
	if S[i - 1] == "W":
		west[i] = west[i - 1] + 1
	else:
		west[i] = west[i - 1]
for i in range(N, 0, -1):
	if S[i - 1] == "E":
		east[i] = east[i + 1] + 1
	else:
		east[i] = east[i + 1]
	ret = min(east[i] + west[i] - 1, ret)

print(ret)

Solution (Python):
n = int(input())
s = list(input())
allwest = s.count("W")
alleast = n-allwest
left_west = 0
right_east = alleast if s[0] == "W" else alleast-1
answer = left_west + right_east
for i in range(1,n):
    left_west += 1 if s[i-1]=="W" else 0
    right_east -= 1 if s[i] == "E" else 0
    answer = min(answer,left_west+right_east)
print(answer)

Solution (Python):
n = int(input())
s = list(input())
allwest = s.count("W")
alleast = n-allwest
left_west = 0
right_east = alleast if s[0] == "W" else alleast-1
answer = left_west + right_east
for i in range(1,n):
    left_west += 1 if s[i-1]=="W" else 0
    right_east -= 1 if s[i] == "E" else 0
    answer = min(answer,left_west+right_east)
print(answer)

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

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

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

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

print(ans)

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

numlist=[1 for i in range(N)]

if S=="E"*N or S=="W"*N:
    numlist[0]=0
else:
    num_0=0
    for i in range(1,N):
        if S[i]=="E":
            num_0+=1
    numlist[0]=num_0

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

print(max(min(numlist),0))

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

Solution (Python):


N=int(input())
S=input()
S=list(S)
East_number=0
East=[]
for i in S:
  if i=='E':
    East_number+=1
  East.append(East_number)
number=0
ans=[]
for i in range(N):
  if i >0:
    number+=i-East[i-1]
  number+=East[N-1]-East[i]
  ans.append(number)
  number=0
print(min(ans))

Solution (Python):
n = int(input())
s = input()
e = s.count("E")
w = s.count("W")
cnt1 = 0
cnt2 = 0
ans = 10e9
for i in s:
    if i=="E":
        cnt1 += 1
        ans = min(ans,cnt2+e-cnt1)
    else:
        ans = min(ans,cnt2+e-cnt1)
        cnt2 += 1
print(ans)

Solution (Python):
N = int(input())
S = input()
w_list = [0] * (N + 2) 
e_list = [0] * (N + 2) 
cnt = 0
for i in range(N):
  if S[i] == "W":
    cnt += 1
  w_list[i + 1] = cnt
cnt = 0
for i in range(N - 1, -1, -1):
  if S[i] == "E":
    cnt += 1
  e_list[i + 1] = cnt
ans = 10 ** 9
for i in range(1, N + 1):
  ans = min(ans, w_list[i - 1] + e_list[i + 1])
print(ans)

Solution (Python):
INF = 1e9
n = int(input())
s = list(input())
sum_1 = [0]*(n+1)
for i in range(n):
    if s[i]=="E":
        sum_1[i+1] = sum_1[i]+1
    else:
        sum_1[i+1] = sum_1[i]

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

Solution (Python):
n = int(input())
s = input()
L = [0 for j in range(n)] 
R = [0 for j in range(n)]
if s[0] == "W":
    L[0] = 1
if s[-1] == "W":
    R[-1] = 1
for i in range(1,n):
    L[i] = L[i - 1]
    R[-(i + 1)] = R[-i]
    if s[i] == "W":
        L[i] += 1
    if s[-(i + 1)] == "W":
        R[-(i + 1)] += + 1
m = min(n - 1 - R[1], L[-2])
for i in range(1,n - 1):
    k = L[i- 1] + (n - i - 1) - R[i + 1]
    if m > k:
        m = k
print(m)

Solution (Python):
n=int(input())
s=input()
cnt=s.count('E') 
m=cnt


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

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

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

Solution (Python):


n = int(input())
d = input()

pre = d[1:].count('E')
minimum = pre
for i in range(1,n):
    if d[i-1]=='W':
        pre = pre+1
    if d[i]=='E':
        pre = pre-1
    minimum = min(pre, minimum)

print(minimum)

Solution (Python):


def resolve():
    n=int(input())
    s=[i for i in input()]
    count=0
    l1=[0 for i in range(n)]
    for i in range(n):
        l1[i]=count
        if s[i]=='W':
            count+=1
    count=0
    l2=[0 for i in range(n)]
    for i in range(n-1,-1,-1):
        l2[i]=count
        if s[i]=='E':
            count+=1
    print(min([l1[i]+l2[i] for i in range(n)]))



if __name__ == "__main__":
    resolve()

Solution (Python):
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy, copy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd


def input(): return sys.stdin.readline().strip()


def INT(): return int(input())


def MAP(): return map(int, input().split())


def LIST(): return list(map(int, input().split()))


sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    n = INT()
    s = input()

    lw = [0]*n
    le = [0]*n
    rw = [0]*n
    re = [0]*n

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

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

    ans = INF

    for i in range(n): 
        

        ans = min(ans, lw[i]+re[i])

    print(ans)


if __name__ == '__main__':
    main()

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

Solution (Python):


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

all=len(S)
all_E=S.count("E")
all_W=S.count("W")
E=0
W=0

eva=10**12
ans=0
for index,item in enumerate(S):
    temp=W+all_E-E
    eva=min(eva,temp)
    if eva==temp:
        ans=index
    if item=="E":
        E+=1
    else:
        W+=1

temp=W+all_E-E
eva=min(eva,temp)


print(eva)

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

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

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

for i in range(n-1):
    if l[i]=='E':
        e[i+1]=e[i]+1
    else:
        e[i+1]=e[i]
    if l[n-1-i]=='W':
        w[n-2-i]=w[n-1-i]+1
    else:
        w[n-2-i]=w[n-1-i]
ew=[0]*n
for i in range(n):
    ew[i]=e[i]+w[i]
print(n-1-max(ew))

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

Solution (Python):
n = int(input())
s = input()
memoW = [0 for i in range(n)]
memoE = [0 for i in range(n)]
countW = 0
countE = 0
for i in range(1,n):
    if s[i-1]=='W':
        countW +=1
    memoW[i] = countW
for i in range(n-2,-1,-1):
    if s[i+1]=='E':
        countE +=1
    memoE[i] =countE
ans = n
for i in range(n):
   ans = min(memoW[i]+memoE[i],ans) 
print(ans)

Solution (Python):
n = int(input())
s = input()
way = [0]*(n+1)
for i in range(n):
    if s[i] == "E":
        way[i+1] = way[i]+1
    else:
        way[i+1] = way[i]
key = way[-1]
ans = 10000000
for j in range(n):
    sub = j - way[j]-way[j+1] + key
    if sub < ans:
        ans = sub
print(ans)

Solution (Python):
n=int(input())
s=input()
w_num=[0]*n
e_num=[0]*n
for i,c in enumerate(s[:-1]):
    w_num[i+1] = w_num[i] + (c=='W')
for i,c in enumerate(s[:0:-1]):
    e_num[n-2-i] = e_num[n-1-i] + (c=='E')
print(min(w + e for w,e in zip(w_num,e_num)))

Solution (Python):
import numpy as np

def test():
    n = int(input())
    s = input()
    w = [0]*n
    e = [0]*n
    ans = float("inf")
    for i in range(n):
        if s[i] == "W":
            w[i] = 1
        else:
            e[i] = 1
    w = np.cumsum(w)
    e = np.cumsum(e)

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


if __name__ == "__main__":
    test()

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

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

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

print(min(cws[i] + ces[i] for i in range(N)))

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

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


if __name__ == '__main__':
    main()

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

Solution (Python):
"""
    リーダーに向いてない人の最小
    (W in 0~i) + (E in i+2~n-1)
"""
from collections import Counter as count
n = int(input())
s = input()
turn = count(s[1:])['E']
least = turn

for i in range(n-1):
    if s[i] == 'W':
        turn += 1
    if s[i+1] == 'E':
        turn -= 1
    if turn < least:
        least = turn

print(least)

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

W = [0] *N
E = [0] *N
L = [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]
for i in range(1,N+1):
  if S[-i] == "E":
    E[-i] = E[-i+1] + 1
  else:
    E[-i] = E[-i+1]
L[0] = E[1]
L[-1] = W[-2]
for i in range(1,N-1):
  L[i] = W[i-1] + E[i+1]
print (min(L))

Solution (Python):
n = int(input())
S = input()
E = [0]*n
W = [0]*n
ans = n
for i in range(n):
  if S[i]=='E':
    E[i] = 1
  else:
    W[i] = 1

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

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

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

E_cnt = S.count('E')
S_cnt = 0

min_cnt = 10 ** 6
for i in range(N):
    cnt = 0
    if S[i] == 'E':
        E_cnt -= 1
        cnt = S_cnt + E_cnt
    else:
        cnt = S_cnt + E_cnt
        S_cnt += 1
    min_cnt = min(min_cnt, cnt)

print(min_cnt)

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

E_cnt = S.count('E')
S_cnt = 0
l_change_cnt = 0

min_cnt = 10 ** 6
for i in range(N):
    cnt = 0
    if S[i] == 'E':
        E_cnt -= 1
        cnt = S_cnt + E_cnt
    else:
        cnt = S_cnt + E_cnt
        S_cnt += 1
    min_cnt = min(min_cnt, cnt)

print(min_cnt)

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

pos = 0
minpos = 0
mini = 0

for i in range(N):
  if S[i] == "W":
    pos += 1
  else:
    pos -= 1
  if pos < minpos:
    minpos = pos
    mini = i

s1 = S[0:mini].count("W")
s2 = S[mini+1:N].count("E")
print(s1+s2)

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

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


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

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

ans = float("inf")  
for i in range(N):
  sm = 0
  if 0 < i:
    sm += W[i-1]
  sm += E[N-1] - E[i]
  ans = min(ans, sm)
    
print(ans)