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 = list(input())
count_E = S.count("E")
count_W = 0
ans = count_E
for i in range(N):
    if S[i] == "E":
        count_E -= 1
    if S[i-1] == "W" and i != 0:
        count_W += 1
    if count_E + count_W < ans :
        ans = count_E + count_W
print(ans)

Solution (Python):


import sys
from collections import Counter

input = sys.stdin.readline
INF = float('inf')


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


N = int(input())
S = input().strip()

num_E = Counter(S)['E']  
num_W = 0  

ans = N - 1
for i in range(N):
    if S[i] == 'E':
        num_E -= 1
        ans = min(ans, num_W + num_E)
    else:
        ans = min(ans, num_W + num_E)
        num_W += 1

print(ans)

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

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

s = 0
e = 0
for i in range(0, N):
    if S[i] == "W":
        s += 1
    else:
        e += 1
    W[i + 1] = s
    E[i + 1] = e

ret = 10**10
for leader in range(1, N+1):
    leftW = W[leader-1]
    rightE = E[N]-E[leader]
    
    sum = leftW + rightE
    ret = min(ret, sum)

print(ret)

Solution (Python):
def main():
    n = int(input())
    s = list(input())
    w = 0
    e = 0
    ans = 1<<32
    for ss in s:
        if ss=="W":
            w+=1
        else:
            e+=1
    ww = 0
    for ss in s:
        if ss=="W":
            w -= 1
            ans = min(ans,e+ww)
            ww += 1
        else:
            e -= 1
            ans = min(ans,e+ww)
    print(ans)


if __name__ == '__main__':
    main()

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

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

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

ret = 10 ** 6
for i in range(1, N+1):
    w = W[i - 1]
    e = E[N] - E[i]
    ret = min(ret, w + e)

print(ret)

Solution (Python):
N = int(input())
S = '0' + input()
west = [0 for i in range(N+1)]

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

change = N

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

print(change)

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

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

Solution (Python):
n = int(input())
s = list(str(input()))
list_check = []
num_east = 0
for i in range(0,n):
    if s[i] == "E":
        num_east += 1
    list_check.append(num_east)

calc_min = 10 ** 9
for i in range(0,n):
    if s[i] == "E":
        calc = ( (i+1) - list_check[i] ) + (list_check[-1] - list_check[i])
    else:
        calc = ( (i) - list_check[i] ) + (list_check[-1] - list_check[i])
    if calc_min > calc:
        calc_min = calc

print(calc_min)

Solution (Python):
N = int(input())
S = input()
w = [0]
s = [0]
for v in S:
    if v == 'W':
        w.append(w[-1] + 1)
        s.append(s[-1])
    else:
        w.append(w[-1])
        s.append(s[-1] + 1)
ans = 10 ** 9
for i in range(N):
    ans = min(ans, w[i] + s[-1] - s[i + 1])
print(ans)

Solution (Python):

n = int(input())
s = input()
ans = float('inf')
w, e = 0, s.count('E')
for c in s:
    e -= (c == 'E')
    ans = min(ans, w + e)
    w += (c == 'W')
print(ans)

Solution (Python):
debug = False

def main():
    N = int(input())
    S = list(input())

    tmpCnt=S.count('E')
    if S[0]=='E':
        tmpCnt-=1

    minCnt=tmpCnt

    for i in range(1,N):
        if S[i]=='E':
            tmpCnt-=1
        if S[i-1]=='W':
            tmpCnt+=1
        minCnt=min(minCnt,tmpCnt)
        if debug:
            print(i,S[i],tmpCnt,minCnt)
        
    print(minCnt)
    return

if __name__ == "__main__":
    main()

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

from itertools import accumulate as acc
w = [0] + list(acc([int(i == 'W') for i in s]))
e = [0] + list(acc([int(i == 'E') for i in s[::-1]]))

ans = float('inf')
for i in range(n):
    ans = min(ans, w[i] + e[-i-2])
print(ans)

Solution (Python):
N = int(input())
S = input()
L = [S[1:].count('E'),S[:N].count('W')]
k = 0
w = 0
e = S[1:].count('E')
while (k <= N-2):
  if (S[k] == 'W' and S[k+1] == 'W'):
    w += 1
  elif (S[k] == 'W' and S[k+1] == 'E'):
    w += 1
    e -= 1
  elif (S[k] == 'E' and S[k+1] == 'W'):
    w += 0
  else:
    e -= 1
  L.append(e+w)
  k += 1
  
print(min(L))

Solution (Python):
n = int(input())
s = input()
num = float("inf")
RW = [0] * n
LW = [0] * n
RE = [0] * n
LE = [0] * n
for i in range(1, n):
    w = 0
    if s[i - 1] == "W":
        w += 1

    RW[i] = RW[i - 1] + w

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

    LE[i] = LE[i + 1] + e

num = float("inf")
for i in range(n):
    num = min(num, RW[i] + LE[i])

print(num)

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

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353
dd = [(0,-1),(1,0),(0,1),(-1,0)]
ddn = [(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,-1),(-1,0),(-1,1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)

n = I()
s = S()
r = 0
for i in range(1,n):
    if s[i] == 'E':r+=1
res = r
temp = r
for i in range(1,n):
    if s[i-1] == 'W':temp += 1
    if s[i] == 'E':temp -= 1
    if temp<res:res = temp
print(res)

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

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

Solution (Python):
def main():
	N = int(input())
	a = input()
    
	W = [0]*(N+1)
	E = [0]*(N+1)
	for i in range(N):
		if a[i] == 'W':
			W[i+1] = W[i] + 1
			E[i+1] = E[i]
		else:
			W[i+1] = W[i]
			E[i+1] = E[i] + 1
	E_max = E[-1]
	n_min = N
	for i in range(N):
		t = W[i] + E_max - E[i+1]
		if t < n_min:
			n_min = t
	print (n_min)
main()

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

temp = 0
left = []
for s in S:
    left.append(temp)
    if s == 'W':
        temp += 1

temp = 0
right = []
for i, s in enumerate(S[::-1]):
    right.append(temp + left[-i-1])
    if s == 'E':
        temp += 1
print(min(right))

Solution (Python):
from bisect import *

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

N = read()
S = input()

accWfromL = [0] * (N+1)
accEfromR = [0] * (N+1)

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

print(min(accWfromL[i] + accEfromR[i+1] for i in range(N)))

Solution (Python):
n = int(input())
s = input()
w = [0]
e = [0]

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

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

Solution (Python):
_ = input()
aa = input()

west_count = [None for _ in aa]
east_count = [None for _ in aa]

west_count[0] = 0
for i in range(len(aa) - 1):
    if aa[i] == 'W':
        west_count[i + 1] = west_count[i] + 1
    else:
        west_count[i + 1] = west_count[i]

east_count[-1] = 0
for i in range(len(aa) - 1, 0, -1):
    if aa[i] == 'E':
        east_count[i - 1] = east_count[i] + 1
    else:
        east_count[i - 1] = east_count[i]

candidate_count = len(aa)
for i in range(len(aa)):
    candidate_count = min(west_count[i] + east_count[i], candidate_count)

print(candidate_count)

Solution (Python):
if __name__ == "__main__":
    n = int(input())
    s = input()

    west_list = []
    east_list = []
    west_list.append(0)
    east_list.append(0)

    for i in range(n - 1):
        prev = west_list.pop()
        now = prev + (1 if s[i] == "W" else 0)
        west_list.append(prev)
        west_list.append(now)
        prev = east_list.pop()
        now = prev + (1 if s[n - 1 - i] == "E" else 0)
        east_list.append(prev)
        east_list.append(now)
    east_list.reverse()

    print(min(a + b for a, b in zip(east_list, west_list)))

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

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

lE = [0] * N
lW = [0] * N
le = lw = 0
for i in range(N):
    if S[i] == "E":
        le += 1
    else:
        lw += 1
    lE[i] = le
    lW[i] = lw

rtn = lE[-1] - lE[0]

for i in range(1, N):
    tmp = lW[i-1] + lE[-1] - lE[i]
    if tmp <= rtn:
        rtn = tmp


print(rtn)

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

Solution (Python):
N = int(input())
S = input()
if len(S) != N: raise

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

print(min(p[0] + p[1] for p in zip(lcost, rcost)))

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

list = list(s)
count = 0
E_li = [0]

for i in range(0,N):
	if list[i] == 'E':
		count += 1
	E_li.append(count)

all_E = E_li[N]
n_W = 0
n_E = 0
ans = N

for i in range(0,N):
	n_W = i - E_li[i]
	n_E = all_E - E_li[i+1]
	ans = min(ans, n_W + n_E)

print ans

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

west = S.count("W")
east = S.count("E")

change = east
if S[0] == "E":
    change -= 1
least = change

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

print(least)

Solution (Python):
import sys
a = input()
strs = input()
temp = []
for char in strs:
    if char == 'E':
        temp.append(0)
    else:
        temp.append(1)

subsum1 = [0]
subsum2 = [0]
for num in temp[:-1]:
    subsum1.append(subsum1[-1]+num)
for num in temp[:0:-1]:
    subsum2.append(subsum2[-1]+num)
subsum2 = subsum2[::-1]

minturn = sys.maxsize
for i in range(len(temp)):
    minturn = min(minturn, subsum1[i]-subsum2[i]+len(temp)-1-i)

print(minturn)

Solution (Python):
from itertools import accumulate

n = int(input())
s = [c == 'E' for c in input()]

l = reversed(tuple(accumulate([0] + s[:0:-1])))
r = accumulate([0] + [not c for c in s[:-1]])

print(min(map(sum, zip(l, r))))

Solution (Python):
from itertools import accumulate

n = int(input())
s = [c == 'E' for c in input()]

l = list(accumulate([0] + s[:0:-1]))[::-1]
r = accumulate([0] + [not c for c in s[:-1]])

print(min(map(sum, zip(l, r))))

Solution (Python):
input()
S = input()
res = cnt = S.count('E')
for c in S:
    if c == 'E':
        cnt -= 1
        res = min(res, cnt)
    else:
        cnt += 1
print(res)

Solution (Python):
input()
S = input()
res = cnt = S.count('E')
for c in S:
    cnt -= c == 'E'
    res = min(res, cnt)
    cnt += c == 'W'
print(res)

Solution (Python):





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

e = [0 for i in range(N)]
w = [0 for i in range(N)]



for i in range(N):
    if i > 0:
        w[i] += w[i-1]
    if i < N-1:
        if S[i] == "W":
            w[i+1] += 1

for j in range(N):
    i = N-1-j
    if i < N-1:
        e[i] += e[i+1]
    if i > 0:
        if S[i] == "E":
            e[i-1] += 1

min = 10 ** 6
for i in range(N):
    total = e[i] + w[i]
    if total < min:
        min = total
print(min)

Solution (Python):





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

e = [0 for i in range(N)]
w = [0 for i in range(N)]



for i in range(N):
    if i > 0:
        w[i] += w[i-1]
    if i < N-1:
        if S[i] == "W":
            w[i+1] += 1

for j in range(N):
    i = N-1-j
    if i < N-1:
        e[i] += e[i+1]
    if i > 0:
        if S[i] == "E":
            e[i-1] += 1

min = 10 ** 6
for i in range(N):
    total = e[i] + w[i]
    if total < min:
        min = total
print(min)

Solution (Python):





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

e = [0 for i in range(N)]
w = [0 for i in range(N)]


for i in range(N):
    
    if i > 0:
        w[i] += w[i-1]
    if i < N-1:
        if S[i] == "W":
            w[i+1] += 1
    
    if i > 0:
        e[N-1-i] += e[N-1-(i-1)]
    if i < N-1:
        if S[N-1-i] == "E":
            e[N-1-(i+1)] += 1

min = 10 ** 6
for i in range(N):
    total = e[i] + w[i]
    if total < min:
        min = total
print(min)

Solution (Python):





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

e = [0 for i in range(N)]
w = [0 for i in range(N)]
d = [0 for i in range(N)]


for i in range(N):
    
    if i > 0:
        w[i] += w[i-1]
    if i < N-1:
        if S[i] == "W":
            w[i+1] += 1



for j in range(N):
    i = N-1-j
    if i < N-1:
        e[i] += e[i+1]
    if i > 0:
        if S[i] == "E":
            e[i-1] += 1


min = 10 ** 6
for i in range(N):
    total = e[i] + w[i]
    if total < min:
        min = total
print(min)

Solution (Python):





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

e = [0 for i in range(N)]
w = [0 for i in range(N)]
d = [0 for i in range(N)]


for i in range(N):
    
    if i > 0:
        w[i] += w[i-1]
    if i < N-1:
        if S[i] == "W":
            w[i+1] += 1
    
    if i > 0:
        e[N-1-i] += e[N-1-(i-1)]
    if i < N-1:
        if S[N-1-i] == "E":
            e[N-1-(i+1)] += 1

min = 10 ** 6
for i in range(N):
    total = e[i] + w[i]
    if total < min:
        min = total
print(min)

Solution (Python):





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

e = [0 for i in range(N)]
w = [0 for i in range(N)]
d = [0 for i in range(N)]


for i in range(N):
    
    
    
    
    
    
    if i > 0:
        w[i] += w[i-1]
    if i < N-1:
        if S[i] == "W":
            w[i+1] += 1
    if i > 0:
        e[N-1-i] += e[N-1-(i-1)]
    if i < N-1:
        if S[N-1-i] == "E":
            e[N-1-(i+1)] += 1

    
    
    
    






min = 10 ** 6
for i in range(N):
    total = e[i] + w[i]
    if total < min:
        min = total
print(min)

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

ans = line[1:N].count('E')
tmp = ans
for i in range(1,N):
 if line[i] == 'E':
  tmp -= 1
 if line[i-1] == 'W':
  tmp += 1
 if ans>tmp:
  ans = tmp

print(ans)

Solution (Python):


n=int(input())
s=input()
k=0
l=[]
for i in range(n):
    if s[i]=="W":
        k+=1
    l.append(k)



w=n
for i in range(n):
    if i==0:
        
        
        
        
        
        v=n-1-l[n-1]+l[0]
    else:
        v=l[i-1]+n-1-i-(l[n-1]-l[i])


    if v<w:
        w=v
    if v==0:
        w=0
        break    
print(w)

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


w=n
for i in range(n):
    if i==0:
        
        
        
        
        
        v=n-1-l[n-1]+l[0]
    else:
        v=l[i-1]+n-1-i-(l[n-1]-l[i])


    if v<w:
        w=v
print(w)

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

print(result)

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

Solution (Python):
input()
ss = input()
loss = ss[1:].count("E")
min_loss = loss
ss_before = ss[:-1]
ss_after = ss[1:]
for b, a in zip(ss_before, ss_after):
    loss += b == "W"
    loss -= a == "E"
    if loss < min_loss:
        min_loss = loss
print("{}".format(min_loss))

Solution (Python):
'''input
8
WWWWWEEE
'''
n = input()
s =raw_input().strip()
ee = [0]*(n+1)
ww = [0]*(n+1)
for i in xrange(n):
	if i == 0:
		if s[i] == 'E':
			ee[i] = 1
		continue
	if s[i] == 'E':
		ee[i] = ee[i-1]+1
	else:
		ee[i] = ee[i-1]
for i in xrange(n-1,-1,-1):
	if i == n-1:
		if s[i] == 'W':
			ww[i] = 1
		continue
	if s[i] == 'W':
		ww[i] = ww[i+1]+1
	else:
		ww[i] = ww[i+1]
ans = 10**10
for i in xrange(1,n-1):
	ans = min(ans,n-ee[i-1]-ww[i+1]-1)
print min(ans,n-ww[1]-1,n-ee[n-2]-1)

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

Solution (Python):
import sys

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

position_L = []
position_R = []
for s in S:
    if s == 'W':
        position_R.append(0)
    else:
        position_R.append(1)
        
flips = sum(position_R[1:])
flips_temp = flips

i = 1
while i < N:
    
    flips_temp = flips_temp - position_R[i] + ((-1) * position_R[i-1] + 1)
    if flips_temp < flips:
        flips = flips_temp
    i = i + 1
print(flips)

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

l = 0
r = s[1:].count('E')
x = l + r

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

  if s[i] == 'E':
    r -= 1
  x = min(x, l + r)

print(x)

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

w = 0
e = S[1:].count('E')

leader = S[0]
min_mem=e
cnt_mem=0

for i in range(1, N):
    if leader ==  'W':
        w += 1
    leader = S[i]

    if leader == 'E':
        e -= 1

    cnt_mem = w + e
    if cnt_mem < min_mem:
        min_mem = cnt_mem

print(min_mem)

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

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

Solution (Python):
N = int(input())
S = input()
 
left = 0
right = S[1:].count("E")
ans = left + right

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

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

left = 0
right = S[1:].count('E')
result = left + right

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

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

arr=[]
num=0
for i in range(N-1):
	if S[i+1]=='E':
		num=num+1

arr.append(num)

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

print(min(arr))

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

length = len(S)
min_num = 300000
left = 0
right = S.count("E")

for i in range(length):

  if i > 0:
    left += int(S[i - 1] == "W")

  if i < length:
    right -= int(S[i] == "E")

  if left + right < min_num:
    min_num = left + right

print(min_num)

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


order=0
for c in S:
    if c=='E':
        order+=1


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

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

result = 300000

before = 0
after = 0

for i in range(n):
    if s[i] == "E":
        before = before + 1
    if before > after:
        before = after
    if s[i] == "W":
        after = after + 1

print min(before, after)

Solution (Python):
import numpy as np

if __name__ == '__main__':
    N = int(input())
    S = input()
    str_l = np.array(list(S))
    direction_E = (str_l == 'E').astype(int)
    direction_W = (str_l == 'W').astype(int)
    direction = direction_W - direction_E
    min = sum = np.sum(direction_E)
    for i in range(N):
        sum += direction[i]
        if sum < min:
            min = sum
    print(min)

Solution (Python):


def west_count(s):
    a = []
    n = 0
    for c in s:
        a.append(n)
        if c == "W":
            n += 1
    return a


def east_count(s):
    a = []
    n = 0
    for c in reversed(s):
        a.append(n)
        if c == "E":
            n += 1
    return list(reversed(a))


n = int(input())
s = input()
w = west_count(s)
e = east_count(s)

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

print(ans)

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

d = {"W" : 0, "E" : 0}
for i in range(N):
    d[S[i]] += 1

t = {"W" : 0, "E" : 0}
ans = 300000
for i in range(N):
    if S[i] == "W":
        ans = min(ans,t["W"] + d["E"] - t["E"])
    else:
        ans = min(ans,t["W"] + d["E"] - t["E"] - 1)
    t[S[i]] += 1
print(ans)

Solution (Python):
length = int(raw_input())
directions = raw_input()

from_west = [None] * length
from_east = [None] * length


west = 0
east = 0
west_reverse = 0
east_reverse = 0

for n in range(length):
    if directions[n] == 'W':
        west += 1
    else:
        east += 1

    if directions[-(n + 1)] == 'W':
        west_reverse += 1
    else:
        east_reverse += 1

    from_west[n] = (west, east)
    from_east[-(n + 1)] = (west_reverse, east_reverse)


min_length = length + 1
for i in range(length):
    left = None
    right = None
    if i != 0:
        left = from_west[i - 1]
    if i != length - 1:
        right = from_east[i + 1]

    left_turns = 0
    if left:
        left_turns = left[0]
    right_turns = 0
    if right:
        right_turns = right[1]

    if left_turns + right_turns < min_length:
        min_length = left_turns + right_turns

print min_length

Solution (Python):

import sys


n = int(sys.stdin.readline().strip())
s = list(sys.stdin.readline().strip())


counts = []
counts.append([0] * n)
counts.append([0] * n)

length = len(s)

if (s[0] == 'W'):
     counts[0][0] = counts[0][0] + 1

if (s[length - 1] == 'E'):
    counts[1][length-1] = counts[1][length-1] + 1



for i in range(1, length):
    counts[0][i] = counts[0][i - 1]
    counts[1][length -1 - i] = counts[1][length - i]

    if (s[i] == 'W'):
        counts[0][i] = 1 + counts[0][i]

    if (s[length - i - 1] == 'E'):
        counts[1][length - i - 1] = 1 + counts[1][length - i - 1]

leaders = [0] * n
for i in range(length):
    if (i == 0):
        leaders[i] = counts[1][i + 1]
    elif(i == length - 1):
        leaders[i] = counts[0][i - 1]
    else:
        leaders[i] = counts[0][i - 1] + counts[1][i + 1]



print(min(leaders))

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

l = 0
r = S[1:].count('E')
result = l + r

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

print(result)

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

ans_list = [0 for _ in range(n)]
ans_list[0] = s[1:].count("E")
ans = ans_list[0]

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

	elif s[i] == "W" and s[i-1] == "W":
		ans_list[i] = ans_list[i-1] + 1

	else:
		ans_list[i] = ans_list[i-1]

	if ans_list[i] < ans:
		ans = ans_list[i]

print(ans)

Solution (Python):
from sys import stderr
from functools import reduce
from operator import add
def f(): return [int(i) for i in input().split()]
def debug(*x): stderr.write(reduce(add,repr(x)).strip("()\',")+'\n')

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

must = [0 for i in range(n)]

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

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

Solution (Python):
N = int(input())
S = list(input())
num_E = []
for i in range(N):
  if i == 0 and S[i] == 'E':
    num_E.append(1)
  elif i == 0 and S[i] == 'W':
    num_E.append(0)
  elif S[i] == 'E':
    num_E.append(num_E[i - 1] + 1)
  else:
    num_E.append(num_E[i - 1])
num_turn = [None for _ in range(N)]
ans = 100000000000000000000000
for i in range(N):
  me = 1 if (S[i] == 'E') else 0
  num_turn[i] = i - (num_E[i] - me) + (num_E[N - 1] - num_E[i])
  if ans > num_turn[i]:
    ans = num_turn[i]
print(ans)

Solution (Python):
def foo (s):
    ans = 0
    count = 0
    for i in s:
        if i == 'W':
            count += 1
        elif count :
            count -= 1
            ans += 1
        else:
            pass
    return ans

n = input()
s = input()
 
print(foo(s))

Solution (Python):
def foo (s):
	ans = 0
	count = 0
	for i in s:
		if   (not count) and (i == 'E'):pass
		elif not count: count += 1
		elif i == 'E': count -= 1; ans += 1
		else: count += 1
	return ans

n = input()
s = input()

print(foo(s))

Solution (Python):
n = int(raw_input())
S = raw_input().strip()

face_E = 0
face_W = 0
for i in S:
    if i=='E':
        face_E+=1
    else:
        face_W+=1

change = n
left_E = 0
left_W = 0
right_E = face_E
right_W = face_W
for people in S:
    if people == 'W':
        right_W -= 1
        change = min(change, left_W + right_E)
        left_W += 1
    else:
        right_E -= 1
        change = min(change, left_W + right_E)
        left_E += 1

print change

Solution (Python):
from itertools import accumulate

n = int(input())
s = [c == 'W' for c in input()]
l = accumulate([0] + s[:-1])
r = accumulate([0] + [not c for c in reversed(s[1:])])
print(min(e + w for e, w in zip(l, reversed(list(r)))))

Solution (Python):
import sys
 
n = int(sys.stdin.readline())
s = list(sys.stdin.readline())

it = s[0]
w = 0
e = s[1:].count('E')

m = e
j = 0

for i in range(1, len(s)):
	if it == 'W':
		w = w + 1

	it = s[i]

	if it == 'E':
		e = e - 1

	m2 = w + e
	if m2 < m:
		m = m2
		j = i
print(m)

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

k = list(input())
p = [0]*da
for i in range(da):
  if k[i] == "E" and i < da -1:
    p[i+1] += 1
  elif k[i] == "W":
    p[0] += 1
    p[i] -= 1

for i in range(1,da):
  p[i] += p[i-1]

k = max(p)
if max(p) != p[0] or max(p) != p[da-1]:
  k += 1
print(da - k)

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

left = 0
right = S.count('E')

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

sum_ = left + right

for i in range(len(S) - 1):
    now = S[i]
    nxt = S[i+1]

    if now == 'W':
        left += 1
    if nxt == 'E':
        right -=1

    if left + right < sum_:
        sum_ = left + right


left = S.count('W')
right = 0

if S[-1] == 'W':
    left -= 1
if left + right < sum_:
    sum_ = left + right

print(sum_)

Solution (Python):
N = int(input())
S = input()
w_count = 0
e_count = 0
w_counts = [0 for _ in range(N)]
e_counts = [0 for _ in range(N)]
for i in range(N):
    if S[i] == 'W':
        w_count += 1
    w_counts[i] = w_count
for i in range(N-1, -1, -1):
    if S[i] == 'E':
        e_count += 1
    e_counts[i] = e_count
_min = -1
for i in range(N):
    total = w_counts[i] + e_counts[i]
    if _min == -1:
        _min = total
    if _min > total:
        _min = total
print(_min-1)

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

leftW = [0]*N
rightE = [0]*N
for i in range(N-1):
    if S[i] == "W":
        leftW[i+1] = leftW[i] + 1
    else:
        leftW[i+1] = leftW[i]
for i in range(N-1,0,-1):
    if S[i] == "E":
        rightE[i-1] = rightE[i] + 1
    else:
        rightE[i-1] = rightE[i]
minlw = N
for i in range(N):
    if leftW[i] + rightE[i] < minlw:
        minlw = leftW[i] + rightE[i]
print(minlw)

Solution (Python):
n = int(input())
a = list(input())
for i in range(n):
    if a[i] == 'E':
        a[i] = 1
    else:
        a[i] = 0

ans = 99999999999999999999

l = 0
r = sum(a)
for i in range(n):

    if i == 0:
        if a[i] == 1:
            r -= a[i]
            a[i] = 0
        else:
            a[i] = 1
        l = 0
    else:
        if a[i] == 1:
            a[i] = 0
            r -= 1
        else:
            a[i] = 1
        l += a[i-1]

    ans = min(l+r,ans)
    
print(ans)

Solution (Python):
n=int(input())
s=input()
a=[]
for i in s:
    if i=='W': a.append(1)
    else: a.append(-1)
a2=[-1*i for i in a]
suma=[sum(a)-a[0]]
for i in range(len(a)-1):
    suma.append(-a[i+1]+suma[i])
suma2=[0]
for i in range(len(a2)-1):
    suma2.append(suma2[i]+a2[i])

count=0
for i in range(len(a)):
    d=suma[i]+suma2[i]
    if count<d:count=d
    
print((n-1-count)//2)

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

left = 0
right = N - 1

DP = [0] * N

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

for i in range(1, N):
    if S[i - 1] == "W":
        a = 1
    else:
        a = 0
    if S[i] == "E":
        b = -1
    else:
        b = 0
    DP[i] = DP[i - 1] + a + b

print(min(DP))

Solution (Python):



def solve():
    n = int(input())
    s = input()
    el, wl = [0]*n, [0]*n
    ec, wc = 0, 0
    for i in range(n):
        if s[n - 1 - i] == 'E':
            ec += 1
        if s[i] == 'W':
            wc += 1
        el[n - 1 - i] = ec
        wl[i] = wc

    ans = n - 1
    for i in range(n):
        if i == 0:
            ans = min(ans, el[i + 1])
        elif i == n - 1:
            ans = min(ans, wl[i - 1])
        else:
            ans = min(ans, wl[i - 1] + el[i + 1])
    print(max(0, ans))

if __name__=="__main__":
    solve()

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

from collections import defaultdict
wc = defaultdict(int)
ec = defaultdict(int)

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

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

min_ = N
for i in range(1, N-1):
    v = wc[i-1] + ec[i+1]
    if v < min_:
        min_ = v
v0 = ec[1]
if v0 < min_:
    min_ = v0
vn = wc[N-1]
if vn < min_:
    min_ = vn
print(min_)