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

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

print(result)

Solution (Python):
n=int(input())
s=input()
ss=s[::-1]
ans=10**7
front_bucket=[0,0]
back_bucket=[0,0]

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

for i in range(n):
  if ss[i]=="E":
    front_bucket[0]-=1
    ans=min(ans, front_bucket[1]+back_bucket[0])
    back_bucket[0]+=1

  else:
    front_bucket[1]-=1    
    ans=min(ans, front_bucket[1]+back_bucket[0])
    back_bucket[1]+=1

print(ans)

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


w = [0] * n
bf = 0

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

ans = n - 1

for i in range(n):
    
    myE = (n - w[n - 1]) - (i + 1 - w[i])
    if i == 0:
        myW = 0
    else:
        myW = w[i - 1]
    ans = min(ans, myE + myW)

print(ans)

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

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

Solution (Python):
N = int(raw_input())
S = raw_input()
l = [0] * N
r = [0] * N
w = 0
e = 0
for i in range(N-1):
  w += (1 if S[i]=="W" else 0)
  l[i+1] = w
  e += (1 if S[N-1-i]=="E" else 0)
  r[N-1-i-1] = e
ret = N
for i in range(N):
  ret = min(ret, l[i]+r[i])
print ret

Solution (Python):
import numpy
n = int(input())
s = input()
a = [0]*n
ans = 10**6
for i in range(n):
    if s[i] == 'W':
        a[i] = 1
res = numpy.cumsum(a)
for i in range(n):
    x = res[i]-str(a[i]).count('1')
    y = n-i-1-(res[n-1]-res[i])
    ans = min(ans,x+y)
print(ans)

Solution (Python):
s = ["", ""]

s[0] = input()
s[1] = input()

WtoE = []
wtoec = 0
EtoW = []
etowc = 0

for c in s[1]:
    if c == "W":
        wtoec = wtoec + 1
    WtoE.append(wtoec)

for c in s[1][::-1]:
    if c == "E":
        etowc = etowc + 1
    EtoW.append(etowc)
EtoW.reverse()

a = []
for i in range(0, int(s[0])):
    ans = WtoE[i] + EtoW[i]
    if WtoE[i] != 0 and s[1][i] == "W" or EtoW[i] != 0 and s[1][i] == "E":
        ans = ans - 1

    a.append(ans)

print(min(a))

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

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

Solution (Python):
def c_attention(N, S):
    count_east = [0] * N  
    count_west = [0] * N  

    for k in range(N - 2, -1, -1):
        count_east[k] = count_east[k + 1] + 1 \
            if S[k + 1] == 'E' else count_east[k + 1]
    for k in range(1, N):
        count_west[k] = count_west[k - 1] + 1 \
            if S[k - 1] == 'W' else count_west[k - 1]

    count = [0] * N  
    for k in range(N):
        count[k] = count_east[k] + count_west[k]
    ans = min(count)
    return ans

N = int(input())
S = input().strip()
print(c_attention(N, S))

Solution (Python):





if __name__ == '__main__':
    n = int(input())
    s = input()
    count = 0
    ws = [0] * n
    es = [0] * n

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

    for i in range(len(s) - 1):
        if i == 0:
            count = es[-1] - es[1]
        else:
            count = min(count, (ws[i]) + (es[-1] - es[i + 1]))

    print(count)

Solution (Python):
n=int(input())
s=list(input())
e_cnt=s[1:].count("E")
cnt=e_cnt
for i in range(1,n):
    if s[i-1]=="W":
        e_cnt+=1
    if s[i]=="E":
        e_cnt-=1
    if e_cnt<cnt:
        cnt=e_cnt
print(cnt)

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

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

change = S[1:].count('E')
min_ = change

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

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

w = [0] * (n + 1)
e = [0] * (n + 1)
a = [0] * n
for i, x in enumerate(s):
    if x == 'W':
        w[i+1] = w[i] + 1
        e[i+1] = e[i]
    else:
        w[i+1] = w[i]
        e[i+1] = e[i] - 1
    a[i] = w[i] + e[i+1]

print(n - w[-1] + min(a))

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

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

def we_counter(pos):
  front = s[:pos]
  back = s[pos+1:]
  people = front.count('W') + back.count('E')
  return people


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

max_point = max(point)
pos = point.index(max_point)

print(we_counter(pos))

Solution (Python):
n = int(input())
s = input()
t = []
x = 0
y = s.count('E')
for ch in s:
    if ch == 'E':
        y -= 1
    t.append(x+y)
    if ch == 'W':
        x += 1
print(min(t))

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

Solution (Python):
from numpy import cumsum
n = int(input())
s = input()
def conv(i):
    if i == 'E': return 1
    return 0
s2 = [conv(i) for i in s]
cum = cumsum(s2)
ans = n - 1
b = cum[-1]
for i in range(n):
    a = cum[i]
    s1 = b - a
    s3 = (i+1) - cum[i]
    if s2[i] == 0:
        s3 -= 1
    ans = min(ans, s1+s3)
print(ans)

Solution (Python):
N = int(raw_input())
S = list(raw_input())
S = ['X']+S+['X']
D = [[0, 0]]
W = [0] * (N+2)
E = [0] * (N+2)
for i in range(1,N+2):
 W[i] = W[i-1]+(1 if S[i-1]== 'W' else 0)
for i in range(N+1)[::-1]:
 E[i] = E[i+1]+(1 if S[i+1]== 'E' else 0)
ans = N
for i in range(N):
  if W[i+1]+E[i+1] < ans:
   ans = W[i+1]+E[i+1]
print ans

Solution (Python):
n = int(input())
S = input()
W = 0
E = 0
T = [0 for i in range(n)]  
for i in range(n-1):
    if S[i] == "W": W+=1
    if i > 0:
        T[i+1] += W 

for i in range(n-1,0,-1):
    if S[i] == "E": E+=1
    if i < n-1:
        T[i-1] += E
print(min(T))

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

ms = n
a = 0
b = s.count('E')
for i in range(n):
	if(i != 0 and s[i-1] == 'W'):
		a += 1
	if(s[i] == 'E'):
		b -= 1
	
	
	
	c = a + b
	if(ms > c):
		ms = c

print(ms)

Solution (Python):
import math
import numpy as np

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

nW = [0 for i in range(N)] 
nE = [0 for i in range(N)] 

if S[0] == 'W':
    nW[0] = 1
else:
    nE[0] = 1

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

if nE[N-1] - nE[0] < nW[N-1]:
    count = nE[N-1] - nE[0]
else:
    count = nW[N-1]

for i in range(1,N-1):
    if nW[i-1] + nE[N-1] - nE[i] < count:
        count = nW[i-1] + nE[N-1] - nE[i]

print(count)

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

e = [0 for _ in range(n)]
w = [0 for _ in range(n)]

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

res = [0 for _ in range(n)]

for i in range(n):
    res[i] = w[i] + e[i]

print(min(res))

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):
from collections import Counter
n = int(input())
st = input()
l = []
stc = Counter(st)
ma = [Counter(st[:0])["W"] + Counter(st[1:])["E"]]
previous = st[0]
for i in range(1,n):
    temp = ma[-1]
    if(previous == "W"):
        temp+=1
    previous = st[i]
    if(st[i] == "E"):
        temp-=1
    ma.append(temp)
print(sorted(ma)[0])

Solution (Python):
N = input()
S = input()
times = []
N = int(N)

wt = S.count("W")
et = S.count("E")
w = 0
e = 0
    
for i in S:
    if i == "W":
        w += 1
        times.append(w-1+et-e)
    else:
        e += 1
        times.append(w+et-e)
print(min(times))

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

count_l = [0]
count_r = [0]


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

count_r.reverse()

Ans = count_l

for i in range(N+1):
    Ans[i] = count_r[i] + count_l[i]

print(min(Ans))

Solution (Python):
if __name__ == '__main__':
	N = int(input())
	S = input()
	w = [0 for _ in range(N)]
	e = [0 for _ in range(N)]

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

	res = [0 for _ in range(N)]
	for i in range(N):
		res[i] = w[i] + e[i]

	print(min(res))

Solution (Python):
if __name__ == '__main__':
	N = int(input())
	S = input()
	w = [0 for _ in range(N)]
	e = [0 for _ in range(N)]




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


	res = [0 for _ in range(N)]
	for i in range(N):
		res[i] = w[i] + e[i]

	print(min(res))

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

Solution (Python):
lis=[]
n=int(input())
lis += input()
count=0
for i in range(1,n):
    if lis[i]=="E":
        count += 1
saidai = count
for i in range(n-1):
    if lis[i] == "W":
        count += 1
    if lis[i+1] == "E":
        count -= 1
        if saidai > count:
            saidai = count
print(saidai)

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(0, n - 1):
    et = e[i - 1] if i != 0 else 0
    et += 1 if s[i] != "E" else 0
    e[i] = et
    wt = w[(n - 1 - i) + 1] if i != 0 else 0
    wt += 1 if s[n - 1 - i] != "W" else 0
    w[n - 1 - i] = wt

tmin = 0
for i in range(0, n):
    t = 0
    t += e[i - 1] if i != 0 else 0
    t += w[i + 1] if i != (n - 1) else 0
    if t < tmin or i == 0:
        tmin = t 
print(tmin)

Solution (Python):
N=int(input())
S=input()
flag=1
result=S[1:].count("E")
res=result
for i in range(len(S)-1):
    if(S[i] == S[i+1]):
        if(S[i]=="E"):
            res-=1

            result=min(res,result)
        else:
            res+=1
            result=min(res,result)
print(result)

Solution (Python):
n = int(raw_input())
q = raw_input()

etow = []
wtoe = []

nowe = 0
noew = 0

for a in range(n):
  etow.append(noew)
  wtoe.append(nowe)
  if q[a] == "W":
    nowe += 1
  if q[n - 1 - a] == "E":
    noew += 1

wtoe = wtoe[::-1]
mi = 999999999




for a in range(n):
  
  if etow[a] + wtoe[a] < mi:
    mi = etow[a] + wtoe[a]
print mi

Solution (Python):

def main():
    n = int(input().strip())
    s = input().strip()

    right_e = len([x for x in s if x == 'E'])
    left_w = 0

    m = right_e - 1 if s[0] == 'E' else right_e
    
    for i in range(1, len(s)):
        if s[i-1] == 'E':
            right_e -= 1
        else:
            left_w += 1

        if s[i] == 'E':
            m = min(m, right_e - 1 + left_w)
        else:
            m = min(m, right_e + left_w)


    print(m)


if __name__ == '__main__':
    main()

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

w = [0] * (n + 1)
e = [0] * (n + 1)
bfW = 0
bfE = 0
for i in range(n+1):
    w[i] = bfW
    e[i] = bfE
    if s[i] == 'W':
        bfW += 1
    elif s[i] == 'E':
        bfE += 1

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

Solution (Python):
def main():
    """
    2 <= N <= 3 * 10^5
    |S| = N
    Si = {E, W}
    """
    N = int(input())
    S = input()

    f(N, S)


def f(N, S):
    """
    leaderにする人の東西について
    どちらを向いているか分かればよい
    """
    ans = N
    
    w_cumsum = [0] * N
    w_cumsum[0] = int(S[0] == "W")
    for i in range(1, N):
        w = int(S[i] == "W")
        w_cumsum[i] = w_cumsum[i-1] + w

    
    for i in range(N):
        
        
        diff = w_cumsum[i] - int(S[i] == "W")
        diff += (N - (i+1)) - (w_cumsum[-1] - w_cumsum[i])
        ans = min(ans, diff)
        
        

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from sys import stdin
import collections

N = int(stdin.readline().rstrip())
S = stdin.readline().rstrip()

people = []

for i in range(N):
    people.append(S[i])

tmp = collections.Counter(people[1:])
num_return = tmp['E']
minv = num_return

for i in range(1, N):
    if people[i] == 'E':
        num_return -= 1
    if people[i-1] == 'W':
        num_return += 1
    if num_return < minv:
        minv = num_return

print(minv)

Solution (Python):
n = int(input())
s = input()
s = list(map(lambda x: 0 if x == 'W' else 1, s))
ans = 300000
beright = [0 for i in range(n)]
beleft = [0 for i in range(n)]
for i in range(1, n):
    beright[i] = beright[i-1] + abs(s[i-1] - 1)
    beleft[-i-1] = beleft[-i] + s[-i]

for br, bl in zip(beright, beleft):
    ans = min(ans, br + bl)

print(ans)

Solution (Python):
n = int(input())
s = list(input())
ans = s.count('E')
r = 10**6
if s[0]=='E':
    ans -= 1

r = min(ans,r)

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

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

D = Counter(s[1:])["E"]
nums = [D]
for i in range (1,N):
    temp = nums[-1]
    if s[i-1] == "W":
        temp += 1
    if s[i] == "E":
        temp -= 1
    nums.append(temp)

print(min(nums))

Solution (Python):


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

for (i, x) in enumerate(s):
    if x == "E":
        s[i] = 1
    elif x == "W":
        s[i] = 0

left = 0
right = sum(s[1:n])
center = s[0]
min_changed_num = right

for i in range(1, n):
    left = left + (1 - center)
    center = s[i]
    right = right - center
    changed_num = left + right
    if min_changed_num > changed_num:
        min_changed_num = changed_num

print(min_changed_num)

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




list_a_cnt = 0
list_b_cnt = 0
list_a = list(s[0:0])

list_b = list(s[0:])

list_b_cnt = list_b.count('E')
min = list_a_cnt + list_b_cnt
for i in range(N):
    if s[i] == "W":
        list_a_cnt += 1
    elif s[i] == "E":
        list_b_cnt -= 1
    
    
    if list_a_cnt + list_b_cnt < min:
        min = list_a_cnt + list_b_cnt
print(min)

Solution (Python):
def main(num, string):
	e = string.count("E")
	w = num-e
	handol = e 
	minval = e 
	for s in string:
		if s =="E":
			handol -= 1
		else:
			handol += 1
		minval = min(minval, handol)
	return minval
	
	

num = int(input())
string = input()

print(main(num, string))

Solution (Python):
import sys

N = int(input())
S = input()
cw = [] 
cw.append(0)
for i, s in enumerate(S):
    i += 1
    if s == "W":
        cw.append(cw[i-1]+1)
    else:
        cw.append(cw[i-1])


ans = 100000*3
for i in range(N):
    left = cw[i] 
    right = (N-i-1)-(cw[N]-cw[i+1]) 
    
    tmp = left+right
    if ans>tmp:
        ans = tmp
print(ans)

Solution (Python):
N=int(input())
S=input()
x=len([s for s in S[1:] if s=='E'])
x_min=x

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

print(x_min)

Solution (Python):
from collections import Counter
n = int(input())
s = input()
r = dict(Counter(s))
if 'W' not in r:
    r['W'] = 0
if 'E' not in r:
    r['E'] = 0
l = {'W': 0, 'E': 0}
a = []
for c in s:
    l[c] += 1
    r[c] -= 1
    l[c] -= 1
    a.append(l['W'] + r['E'])
    l[c] += 1
print(min(a))

Solution (Python):
import bisect

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

changes = []

wests = [i for i in range(N) if S[i] == 'W']

easts = [i for i in range(N) if S[i] == 'E']


for leader in range(N):
    change = 0
    change += bisect.bisect_left(wests, leader)
    change += len(easts) - bisect.bisect_right(easts, leader)
    changes.append(change)

print(min(changes))

Solution (Python):
n = input()
n = input()
nl = list(n)
w = [0 for i in range(len(n))]
e = [0 for i in range(len(n))]
w[0] = 1 if nl[0] == "W" else 0
e[0] = 1 if nl[0] == "E" else 0
for i in range(len(n)-1):
    w[i+1] = w[i] + 1 if nl[i+1] == "W" else w[i]
    e[i+1] = e[i] + 1 if nl[i+1] == "E" else e[i]
wr = [0 for i in range(len(n))]
er = [0 for i in range(len(n))]
ans = [0 for i in range(len(n))]
for i in range(len(n)):
    wr[i] = w[len(n)-1] - w[i]
    er[i] = e[len(n)-1] - e[i]
for i in range(len(n)):
    temp = w[i] - 1 if nl[i] == "W" else w[i]
    ans[i] = temp + er[i]





print(min(ans))

Solution (Python):





if __name__ == '__main__':
    n = int(input())
    s = input()
    count = 0
    ws = [0] * n
    es = [0] * n

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

    for i in range(len(s) - 1):
        if i == 0:
            count = es[-1] - es[1]
        else:
            count = min(count, (ws[i]) + (es[-1] - es[i + 1]))

    print(count)

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

S = input()

result = 0
E_ = 0
W_ = 0
for i in range(1, N):
    if S[i] == "E":
        E_ += 1

result = E_ + W_

for i in range(1, N):
    if S[i] == "E":
        E_ -= 1

    if S[i - 1] == "W":
        W_ += 1

    result = min(result, E_ + W_)


print(result)

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

for i,s in enumerate(S):
    if i == 0:
        cnt_MIN = S[1:].count("E") 
        cnt = cnt_MIN
    else:
        if S[i-1] == "W":
            cnt += 1
        if S[i] == "E": 
            cnt -= 1
    if cnt < cnt_MIN:
        cnt_MIN = cnt
print(cnt_MIN)

Solution (Python):
from collections import Counter

N = int(input())
S = input()
c = Counter(S[1:])
E_count = c['E']
min_count = E_count

W_left = 0
E_right = E_count
for i in range(1, N):
    if S[i - 1] == 'W':
        W_left += 1
    if S[i] == 'E':
        E_right -= 1
    if W_left + E_right < min_count:
        min_count = W_left + E_right
print(min_count)

Solution (Python):
class dr_count(object):
    def __init__(self):
        self.w = 0
        self.e = 0


N = int(input())
S = input()
count = []
minn = 300000 
for x in range(0,N):  
    count.append(dr_count())
    if x == 0:
        if S[x] == 'E':
            count[x].e += 1
    else:
        if S[x] == 'E':
            count[x].e = count[x-1].e + 1
        else:
            count[x].e = count[x-1].e
a = N-1
while a>=0:
    if a == N-1:
        if S[a] == 'W':
            count[a].w += 1
    else:
        if S[a] == 'W':
            count[a].w = count[a+1].w + 1
        else:
            count[a].w = count[a+1].w
    a -= 1
for x in range(0,N):
    if x == 0:
        num = N - x - 1 - count[x+1].w
    elif x == (N-1):
        num = x - count[x-1].e
    else:
        num = N - x - 1 - count[x+1].w + x - count[x-1].e
    minn = min(num,minn)
print(minn)

Solution (Python):
if __name__ == '__main__':
    N = int(input())
    S = input()

    
    

    min_total_turns = N

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

    right_turns = [0 for _ in range(N)]
    for i in range(N - 1, -1, -1):
        if i != 0:
            right_turns[i - 1] = right_turns[i]
            if S[i] == "E":
                right_turns[i - 1] += 1

        min_total_turns = min(min_total_turns, left_turns[i] + right_turns[i])

    
    
    print(min_total_turns)

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

nE = 0
n = 0
minn = 0
for a in range(1, N):
	wE = (1 if S[a] == 'E' else 0)
	n = n + (1 if S[a-1] == 'W' else 0) - wE
	minn = min(minn, n)
	nE = nE + wE
print(minn + nE)

Solution (Python):
N = int(input())
S = input()
right = S[1:].count('E')
left = 0
answer = right
_reader = S[0]

for i in range(1, N):

    if _reader == "W":
        left += 1

    _reader = S[i]
    
    if _reader == "E":
        right -=1

    t = left + right
    answer = answer if answer <= t else t
        

print(answer)

Solution (Python):

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



list1=[]

ST=S[1:].count("E")
list1.append(ST)


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

print(min(list1))

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

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

Solution (Python):

"""
Created on Sat May 26 21:19:35 2018

@author: maezawa
"""

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

dp = [None for _ in range(n)]

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


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

ans = min(dp)
print(ans)

Solution (Python):
import collections

n = int(input())
s = input()
s_w = s.count("W")
s_e = s.count("E")

w_array = [0 for x in range(n)]
e_array = [0 for x in range(n)]
w_tmp = 0
e_tmp = 0

for i in range(n):
    if s[i] == "W":
        w_tmp += 1
    else:
        e_tmp += 1
    w_array[i] += w_tmp
    e_array[i] += s_e - e_tmp

res = min(s[1:].count("E"), s[:-1].count("W"))

for count in range(1, n-1):
    if w_array[count]+e_array[count+1] < res:
        res = w_array[count]+e_array[count]

print(res)

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

lst1 = [0]
c = 0
for i in range(1,len(s)):
    if s[i-1] == "W":
        c += 1
    lst1.append(c)
    
lst2 = [0]
c = 0
for i in range(len(s)-2,-1,-1):
    if s[i+1] == "E":
        c += 1 
    lst2.append(c)


lst = []
for i in range(len(lst1)):
    lst.append(lst1[i] + lst2[n-i-1])

lst.sort()

print(lst[0])

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

E_list = [0] * N
W_list = [0] * N



E_count = 0
for i in range(N):
    if i != 0:
        if S[i-1] == 'W':
            E_count = 1 + E_count
        E_list[i] = E_count

S.reverse()

W_count = 0
for i in range(N):
    if i != 0:
        if S[i-1] == "E":
            W_count += 1
        W_list[i] = W_count

W_list.reverse()




lll = [W_list[i] + E_list[i] for i in range(N)]
print(min(lll))

Solution (Python):
N = int(input())
P = list(input())
X = P[1:].count("E")
a = [X]
for i in range(N-1):
    if P[i] == "W":
        X += 1
    if P[i+1] == "E":
        X -= 1
    a.append(X)

print(min(a))

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

ans = 3 * 10**7
w = [0]
e = [0]

for i, a in enumerate(s):
    if a == 'W':
        w.append(w[-1]+1)
    else:
        w.append(w[-1])

for i, a in enumerate(s[::-1]):
    if a == 'E':
        e.append(e[-1]+1)
    else:
        e.append(e[-1])

e.reverse()

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

print(ans)

Solution (Python):
N = int(input())
S = list(input())
S1 = []
S2 = S[1:]
n1 = 0
n2 = list.count(S2, 'E')
m = n1 + n2
for i in range(N-1):
    elem1 = S[i]
    elem2 = S[i+1]
    if elem1 == 'W':
        n1 = n1+1
    if elem2 == 'E':
        n2 = n2-1
    m = min(m, n1 + n2)

print(m)

Solution (Python):


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

w = []
e = []
for i in range(len(s)):
    e.append(0)
    if i == 0:
        w.append(0)
        continue
    if s[i - 1] == 'W':
        w.append(w[i - 1] + 1)
    else:
        w.append(w[i - 1])

totalmin = float('inf')
for i in range(len(s) - 1, -1, -1):
    if i == len(s) - 1:
        e[i] = 0
    else:
        if s[i + 1] == 'E':
            e[i] = e[i + 1] + 1
        else:
            e[i] = e[i + 1]
    total = e[i] + w[i]
    totalmin = min(totalmin, total)

print(totalmin)

Solution (Python):
N = int(input())
S = list(input())
w_count = 0
e_count = 0
count_sum = [0 for k in range(N)]
for i in range(1,N):
    if S[i - 1] == "W":
        w_count += 1
    count_sum[i] += w_count
for q in range(N-2,-1,-1):
    if S[q + 1] == "E":
        e_count += 1
    count_sum[q] += e_count
print(min(count_sum))

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

max_num = S[1:].count('W')
pre_num = max_num

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

    if num > max_num:
        max_num = num
    pre_num = num

print(N - max_num - 1)

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


score = s[1:].count("E")
result = score

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

print(result)

Solution (Python):
def m():
    n = int(input())
    s = str(input())
    w = 0
    e = 0
    for i in range(n):
        if s[i] == 'W':
            w +=1
        elif s[i] == 'E':
            e +=1

    ans = 99999999999
    lw = 0
    for j in range(n):
        if s[j] == 'W':
            w -= 1
            ans = min(e+lw, ans)
            lw +=1
        if s[j] == 'E':
            e -= 1
            ans = min(e+lw, ans)
    return ans

print(m())

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

num = S[1:].count('E')
score_list = []
score_list.append(num)

for i in range(N-1):
	if S[i]=='W':
		num+=1
	if S[i+1]=='E':
		num-=1
	score_list.append(num)
	
print min(score_list)

Solution (Python):
n = int(input())
l = list(str(input()))
m = []
W_all = 0
for i in l:
    if i == 'W':
        W_all += 1
    else:
        continue

W_left = 0
ans = 300000

for i in range(len(l)):
    if l[i] == 'W':
        W_left += 1    
    E_count = n - (i + 1) - (W_all-W_left)


    if l[i] == 'W':
        ans = min(ans, W_left -1 + E_count)
    else:
        ans =min(ans, W_left + E_count)
print(ans)

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

sum = 0
for i in range(1,len(S)):
    if S[i] == 'E':
        sum += 1
min = sum

for i in range(1,len(S)):
    if S[i] == 'E':
        sum -= 1
    if S[i-1] == 'W':
        sum += 1
    if min > sum:
        min = sum
print(min)

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

w_l = []
e_l = []

t = 0
for si in s:
    if si == 'W':
        t += 1
        w_l.append(t)
    else:
        w_l.append(t)

t = 0
for si in s:
    if si == 'E':
        t += 1
        e_l.append(t)
    else:
        e_l.append(t)

mn = 10000000000
res = 0

for i in range(1, n-1):
    res = w_l[i-1] + e_l[-1] - e_l[i]
    if res < mn:
        mn = res

if mn > e_l[-1]:
    mn = e_l[-1]

if mn > w_l[n-2]:
    mn = w_l[n-2]

print(mn)

Solution (Python):

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

w2e = [0] * n
e2w = [0] * n

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

for i in reversed(range(n)):
    j = n - i - 1
    if j == 0:
        if s[i] == 'E':
            e2w[j] = 1
    else:
        if s[i] == 'E':
            e2w[j] = e2w[j-1] + 1
        else:
            e2w[j] = e2w[j-1]

out = n
for i in range(n):
    if i == 0:
        temp = e2w[n-1]
    elif i == n-1:
        temp = w2e[n-1]
    else:
        temp = w2e[i-1] + e2w[n-i-1]
    if temp < out:
            out = temp

print (out)

Solution (Python):
n = int(input())
s = input()
a, e = 0, s.count("E")
for c in s:
    if c == 'W':
        n = min(n, a+e)
        a += 1
    else:
        e -= 1
        n = min(n, a+e)
print(n)

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

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

print(ans)

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

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

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

ans = float('inf')
for i in range(n):
    ans = min(ans, cw[i] + ce[i + 1])
print(ans)

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