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

min = -1

for i in range(n):
    if i == 0:
        turn = s[i+1:n].count("E")
    else:
        if s[i-1] == "W" and s[i] == "W":
            turn += 1
        elif s[i-1] == "W" and s[i] == "E":
            next
        elif s[i-1] =="E" and s[i] =="W":
            next
        else:
            turn -=1
    if min == -1 or min >= turn:
        min = turn
        
    

print(min)

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

Wcnt = [0 for _ in range(N+2)]
for i in range(1, N+1):
    Wcnt[i] = Wcnt[i-1]
    if word[i-1] == 'W':
        Wcnt[i] += 1

Ecnt = [0 for _ in range(N+2)]
for i in range(N, 0, -1):
    Ecnt[i] = Ecnt[i+1]
    if word[i-1] == 'E':
        Ecnt[i] += 1


print(min([Wcnt[i-1]+Ecnt[i+1] for i in range(1,N+1)]))

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

s = list(raw_input())

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

cs.append(s[0])

for i in range(1,n):
	cs.append(cs[i-1]+s[i])

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

print ans

Solution (Python):
E = "E"  
W = "W"  

n = int(input().rstrip())

line = input().rstrip()

bef_change_count = line[1:].count(E)
min_change_count = bef_change_count

for leader_i in range(1, n):
    change_count = bef_change_count
    if line[leader_i] == E and line[leader_i - 1] == E:
        change_count -= 1
    elif line[leader_i] == W and line[leader_i - 1] == W:
        change_count += 1

    bef_change_count = change_count
    min_change_count = min(change_count, min_change_count)

print(min_change_count)

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

Solution (Python):
n=int(input())
s=list(input())
count_east=[0]*n
count_west=[0]*n
counts=[0]*n

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

print(min(counts))

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

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

print(ans)

Solution (Python):
import numpy as np
N=int(input())
s=input()
west=[]
east=[]
for i in range(N):
    west.append(0)
    east.append(0)
    if(s[i]=='W'):
        west[i]=1
    else:
        east[i]=1
eastsum=np.zeros(N)
westsum=np.zeros(N)
westsum[0]=west[0]
eastsum[N-1]=east[N-1]
for i in range(1,N):
    eastsum[N-1-i]=eastsum[N-i]+east[N-1-i]
    westsum[i]=westsum[i-1]+west[i]
print(int(np.min(eastsum+westsum))-1)

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

west = [0]*n
east = [0]*n

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

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

cntMin = n
for i in range(0,n):
    cntMin = min(cntMin, west[i]+east[i])
    
    
print(cntMin)

Solution (Python):
n = int(input())
s = list(input())
minChange = n
sumE = s.count('E')

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

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

for i in range(n):
    if s[i] == 'W':
        acW = w[i] - 1
    else:
        acW = w[i]
    racE = sumE - e[i]
    minChange = min(minChange, acW + racE)

print(minChange)

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

for k in s:
    P.append(P[-1] + (k=="W"))

ans = n

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

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

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

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

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

Solution (Python):
N=int(input().strip())
S=input().strip()
w_num=[0]*N
e_num=[0]*N
for i in range(1,N):
    j=N-i-1
    w_num[i] = w_num[i-1] + int(S[i-1]=="W")
    e_num[j] = e_num[j+1] + int(S[j+1]=='E')
total =[i+j for i, j in zip(w_num,e_num)]
print(min(total))

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

Solution (Python):

N = int(input())
S = input()
min = S[1:N].count("E") 
now = S[1:N].count("E") 

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

Solution (Python):
n = int(input())
s = list(input())
 
c = 0
for i in range(1,n):
  if s[i] == "E":
    c+=1
 
min = c
 
 
for i in range(n-1):
  if s[i] == s[i+1]:
    if s[i] == "W":
      c+=1
    else:
      c-=1
  if c < min:
    min = c
 
print(min)

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





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

min_turn = to_east + to_west
for i in range(1, N):
    if min_turn == 0:
        break
    if S[i - 1] == "W":
        to_east += 1
    if S[i] == "E":
        to_west -= 1
    min_turn = min(to_east + to_west, min_turn)

print(min_turn)

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

reS = S[::-1]

wSum = [0 for i in range(N)]
eSum = [0 for i in range(N)]

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

eSum = eSum[::-1]

cost = eSum[1]

for i in range(1,N):
    if i == N - 1:
        if cost > wSum[N-2]:
            cost = wSum[N-2]
    else:
        if cost > wSum[i-1] + eSum[i+1]:
            cost = wSum[i-1] + eSum[i+1]
print(cost)

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

reS = S[::-1]

wSum = [0 for i in range(N)]
eSum = [0 for i in range(N)]

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

eSum = eSum[::-1]

cost = eSum[1]

for i in range(1,N):
    if i == N - 1:
        if cost > wSum[N-2]:
            cost = wSum[N-2]
    else:
        if cost > wSum[i-1] + eSum[i+1]:
            cost = wSum[i-1] + eSum[i+1]
print(cost)

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

total_W = S.count('W')
total_E = N-total_W
temp_West_W = 0
temp_East_E = total_E - S[0].count('E')
temp = temp_West_W + temp_East_E
for i in range(N-1):
    temp_West_W = temp_West_W + S[i].count('W')
    temp_East_E = temp_East_E - S[i+1].count('E')
    if temp > temp_West_W + temp_East_E:
        temp=temp_West_W + temp_East_E
print(temp)

Solution (Python):
n=int(input())
s=input()
minv = 0
v = 0
minp = 0
i = 0
while i < n:
    if s[i] == 'W':
        v += 1
    else:
        v -= 1
    if v < minv:
        minv = v
        minp = i
    i += 1
ans = (minv - (-minp-1))/2 + ((n-minp-1) - (v - minv))/2
print(str(int(ans)))

Solution (Python):
n = input()
l = list(raw_input())
left = 0
right = l.count("E")
count = []

for i in range(0, n):
	if l[i] == "E":
		right -= 1

	count += [left + right]

	if l[i] == "W":
		left += 1

print min(count)

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

min = 3*10**5
cnt = 0

cnt = S.count("E")
min = cnt

for i in S:
  if i == "E":
    cnt -= 1
  elif i == "W":
    cnt += 1
  if min >= cnt:
    min = cnt

print(min)

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

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

cumW = [i - cumE[i] for i in range(N)]
cumE2 = [cumE[N] - cumE[i+1] for i in range(N)]
print(min([cumW[i] + cumE2[i] for i in range(N)]))

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

w=[0]*n
i=1
while i<n:
    if s[i-1]=='W':
        w[i-1]+=1
    w[i]+=w[i-1]
    i+=1
i=n-1
e=[0]*n
while i>0:
    if s[i]=='E':
        e[i]+=1
    e[i-1]+=e[i]
    i-=1
    
ans=10**9

i=0
while i<n:
    ans=min(ans,e[i]+w[i]-1)
    i+=1
print(ans)

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

Solution (Python):
if __name__ == '__main__':
    N = int(input())
    S = list(input())
    w, e = [0 for i in range(N)], [0 for i in range(N)]
    for i, s in enumerate(S):
        if s == 'W':
            if i > 0:
                w[i] = w[i-1] + 1
                e[i] = e[i-1]
            else:
                w[0] = 1
                e[i] = e[i-1]
        if s == 'E':
            if i > 0:
                w[i] = w[i-1]
                e[i] = e[i-1] + 1
            else:
                w[i] = w[i-1]
                e[0] = 1
    ans = 9999999
    for i in range(N):
        if i > 0:
            ans = min(ans, w[i-1]+(e[-1]-e[i]))
        else:
            ans = min(ans, e[-1]-e[i])
    print(ans)

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

countE = S.count('E')
countW = 0
min = countE + countW
for i in range(N):
    if S[i] == 'E':
        countE -= 1
    elif S[i] == 'W':
        countW += 1

    if countE + countW < min:
        min = countE + countW

print(min)

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

for i in range(N):
    if S[i] == "E":
        ans += 1
min_ans = ans

for reader in range(1, N):
    minus = 0
    if S[reader] == "E":
        minus = 1
    if S[reader - 1] == "W":
        ans += 1
    elif S[reader - 1] == "E":
        ans -= 1
    if ans - minus < min_ans:
        min_ans = ans - minus
        count = reader

print(min_ans)

Solution (Python):
def opt(N, S):
    if N == 1:
        return 0
    else:
        split = int(N/2)
        return min(opt(split,S[:split])+S[split:].count('E'),S[:split].count('W')+opt(N - split,S[split:]))

Num = int(raw_input())
Str = str(raw_input())
print(opt(Num, Str))

Solution (Python):
a=int(input())
b=input()
c=b.count('E')
d=0
e=c

for i in range(a):
 if b[i]=='E':
  c-=1
 else:
  d+=1
 e=min(e,c+d)


print(e)

Solution (Python):
from sys import stdin


def main():
    N = int(stdin.readline().rstrip())
    S = stdin.readline().rstrip()
    spam1 = [0]
    for x in S[:-1]:
        spam1 += [spam1[-1] + 1 if x == 'W' else spam1[-1]]
    spam2 = [0]
    for x in S[::-1][:-1]:
        spam2 += [spam2[-1] + 1 if x == 'E' else spam2[-1]]
    spam2 = spam2[::-1]
    print(min([spam1[i] + spam2[i] for i in range(N)]))


if __name__ == '__main__':
    main()

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

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

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

print(min_turns)

Solution (Python):
n = int(input())
s = input()
sample = s.replace("W","1").replace("E","0")
num = 0
num2 = sample.count("0")
sample2 = list(map(int, sample))
sample2.insert(0,0)
sample3 = s.replace("W","0").replace("E","1")
sample4 = list(map(int, sample3))
out = n
for i in range(n):
    num += sample2[i]
    num2 -= sample4[i]
    out = min(num+num2,out)
print(out)

Solution (Python):
def main():
    N = int(input())
    S = input()
    ec = S.count('E')
    wc = 0
    min = ec
    for k in S:
        if k == 'E':
            ec -= 1
            if min > ec + wc: min = ec + wc
        else:
            wc += 1
    print(min)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N = int(input())
    S = input()
    ec = S.count('E')
    wc = 0
    min = ec
    for k in S:
        if k == 'E':
            ec -= 1
        else:
            wc += 1
        if min > ec + wc: min = ec + wc
    print(min)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N = int(input())
    S = input()
    ec = S.count('E')
    tec = ec
    for k in S:
        if k == 'E':
            tec -= 1
        else:
            tec += 1
        ec = min(ec, tec)
    print(ec)

if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
m=list(input())
p=m.count("E")
s=p
for i in m:
    if i=="E":
        p-=1
    else:
        p+=1
    s=min(s,p)
print(s)

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

w_cusum_list = []
w = 0
for i in range(N):
  if S[i] == 'W':
    w += 1
  w_cusum_list.append(w)
    
e_cusum_list = []
e = 0
for i in range(1, N):
  if S[-i] == 'E':
    e += 1
  e_cusum_list.append(e)

minn = min(w_cusum_list[-1], e_cusum_list[-1])
for i in range(1, N):
  sumn = w_cusum_list[i-1] + e_cusum_list[N-i-1]
  if minn > sumn:
    minn = sumn

print(minn)

Solution (Python):
N = int(input())
str = input()
str_list=list(str)
num_list=[]
ans_list=[]
num=0
for i in range (N):
  if str_list[i]=="E":
    num=num+1
    num_list.append(num)
  else:
    num_list.append(num)
   
for i in range (N):
  if i==0:
    ans=num_list[N-1]-num_list[i]
    ans_list.append(ans)
  else:
    ans=num_list[N-1]-num_list[i]+i-num_list[i-1]
    ans_list.append(ans)

print(min(ans_list))

Solution (Python):
n=int(input())
s=input()
li=[]
w,e=0,0
wli,eli=[],[]
ans=[]

for i in range(n):
    if s[i]=="W":
        w+=1
    wli.append(w)
for i in reversed(range(n)):
    if s[i]=="E":
        e+=1
    eli.append(e)
for i in range(n):
    if i!=0 and i!=n-1:
        ans.append(wli[i-1]+eli[n-i-2])
    elif i==0:
        ans.append(eli[n-2])
    elif i==n-1:
        ans.append(wli[n-2])

print(min(ans))

Solution (Python):



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

right = []
left = []
bufl = 0
bufr = 0
for i in range(n):
    if 'W' == s[i]:
        bufl += 1
    left.append(bufl)
    if 'E' == s[(n-1)-i]:
        bufr += 1
    right.append(bufr)
right.reverse()

m = n
for i in range(n):
    if m > (right[i] + left[i]) - 1:
        m = (right[i] + left[i]) - 1
print(m)

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=int(input())
s=str(input())
arr=[]
k=0
for i in range(n):
    if(s[i]=='W'):
        k+=1
    arr.append(k)
arr1=[]
k=0
for i in range(n-1,-1,-1):
    if(s[i]=='E'):
        k+=1
    arr1.append(k)
sumx=300000
sumx=arr1[n-2]

if(arr[n-2]<sumx):
    sumx=arr[n-2]
for i in range(1,n-1):
    if(arr[i-1]+arr1[n-2-i]<sumx):
        sumx=arr[i-1]+arr1[n-2-i]
print(sumx)

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):
    ans = min(ans,t["W"] + d["E"] - t["E"] - (S[i] != "W"))
    t[S[i]] += 1
print(ans)

Solution (Python):
N = int(input())
S = input()
min_num = 10**7
west_num = [0]
east_num = [0]
w_max = 0
e_max = 0
for i in range(0, N): 
    if S[i] == 'W':
        w_max += 1
    else:
        e_max += 1
        
    west_num.append(w_max)
    east_num.append(e_max)

for i in range(0, N):
    num = west_num[i] + east_num[-1] - east_num[i+1]
    if num < min_num:
        min_num = num
        
print(min_num)

Solution (Python):

if __name__ == "__main__":
    n = int(input())
    s = list(input())
    ans = float("inf")
    a = [0]+[ 0 if tmp != 'W' else 1 for tmp in s ]
    b = list(reversed([ 0 if tmp != 'E' else 1 for tmp in s ]+[0]))
    for tmp in range(n-1):
        a[tmp+1] += a[tmp]
        b[tmp+1] += b[tmp]
    else:
        a.pop()
        b.pop()
        b = list(reversed(b))
    for tmp in range(n):
        ans = min(ans, a[tmp]+b[tmp])
    else:
        print(ans)

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

W = [0 for k in range(N)]
E = [0 for k in range(N)]

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

for k in range(1,N):
    if S[N-k] == "E":
        E[N-k-1] = E[N-k] + 1
    else:
        E[N-k-1] = E[N-k]

ans = 10**9
for k in range(N):
    ans = min(W[k]+E[k],ans)

print(ans)

Solution (Python):
N = int(input())
S = input()
lis =[0]
minn =N
for i in range(N):
  if S[i]=="E":
    lis.append(lis[i]+1)
  elif S[i]=="W":
    lis.append(lis[i])

minn = lis[N]

for i in range(2,N+1):


  minn = min(minn,(i-1)-lis[i-1]+(lis[N]-lis[i]))
print(minn)

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

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

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

e_sum= S.count('E')

w_c=e_c=0
ans = N

for i in range(N):

    if S[i]=='E':
        e_c += 1
    else:
        w_c += 1

    if S[i]=='E':
        k = w_c + e_sum -e_c
    else:
        k = w_c -1 + e_sum -e_c
    
    ans = min(ans,k)

print(ans)

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

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

Solution (Python):


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




min = 1000000
e = S.count('E')
w = 0

for i in range(0,N):
    flag = 0
    now = S[i]
    if now == 'E':
        e = e - 1
    else:
        flag = 1
    sum = w + e
    if min > sum:
        min = sum
    if flag:
        w = w + 1
print(min)

Solution (Python):
n=int(input())
s=input()
ans=[]
alle=list(s).count("E")
allw=n-alle
ecnt=wcnt=0
for i in range(n):
    if s[i] == "W":
        ans.append(wcnt+alle-ecnt)
        wcnt+=1
    else:
        ans.append(wcnt+alle-ecnt-1)
        ecnt+=1
print(min(ans))

Solution (Python):
def directions(n, s):
    left = []
    right = []

    change = 0
    for p in s:
        if p == 'W':
            change += 1
        left.append(change)

    change = 0
    for p in s[::-1]:
        if p == 'E':
            change += 1
        right.append(change)
    right.reverse()

    return min(map(lambda x, y: x + y - 1, left, right))



if __name__ == '__main__':
    n = int(input())
    s = input()
    print(directions(n, s))

Solution (Python):
n = int(input())
S = list(input())
dp = [0 for i in range(n)]
dp[0] = S[1:].count("E")

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

print(min(dp))

Solution (Python):


n=int(raw_input())
s=raw_input()
cnt=s[1:].count('E')
res=cnt
for i in xrange(1,n):
 if s[i-1]=='W':
  cnt+=1
 if s[i]=='E':
  cnt-=1
 res=min(cnt,res)
print res

Solution (Python):


"""
"""

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

    wn = [0]
    en = [0]

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

    m = N
    for i in range(len(S)):
        t = (wn[i] - wn[0]) + (en[N] - en[i+1])
        m = min(m, t)

    print(m)

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

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

W_cnt, E_cnt = [0], [0]
W_sum, E_sum = 0, 0

for S_i in S[:-1]:
    if S_i == 'W':
        W_sum += 1
    W_cnt.append(W_sum)
for S_i in list(reversed(S))[:-1]:
    if S_i == 'E':
        E_sum += 1
    E_cnt.append(E_sum)

ans = []
for W_i, E_i in zip(W_cnt, reversed(E_cnt)):
    ans.append(W_i+E_i)
print(min(ans))

Solution (Python):
n=int(input())
s=input()
tcnt=0
for i in range(1,n):
    tcnt+=s[i]=="E"

tmin=n
for i in range(n):
    if tmin>tcnt:
        tmin=tcnt
    if s[i]=="W":
        tcnt+=1
    try:
        if s[i+1]=="E":
            tcnt-=1
    except:
        pass
print(tmin)

Solution (Python):
import numpy as np

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


s = list(S)


s.reverse()
s = np.array([1 if i == 'E' else 0 for i in s])




cost = [0]*N
cost[0] = (N-1) - sum(s[1:N])
for i in range(1,N):
    now = s[i]
    bef = s[i-1]
    cost[i] = cost[i-1] + bef - (1^now)

print(min(cost))

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

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

s = input()

l = []

a = 0

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



ans = n

for i in range(0, n):
    e = w = 0
    if i > 0:
        e = i-l[i-1]
    w = l[n-1]-l[i]

    ans = min(ans, e+w)

print(ans)

Solution (Python):
n = int(raw_input())
s = raw_input()
l = [(0,0)]
for i,c in enumerate(s):
    if c == 'E':
        l.append((l[i][0]+1, l[i][1]))
    else:
        l.append((l[i][0], l[i][1]+1))
mn = n
for i in range(1,n+1):
    mn = min(mn, l[i-1][1]+l[-1][0]-l[i][0])
print mn

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

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

print(result)

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

total = s.count("E")

min_val = total

for char in s:
    if char == "E":
        total -= 1
    else:
        total += 1
    min_val = min(min_val, total)

print(min_val)

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

count = len([s for s in S[1:] if s == "E"])
min_count = count
for i in range(1, N):
    if S[i - 1] == "W":
        count += 1
    if S[i] == "E":
        count -= 1
    min_count = min(min_count, count)
print(min_count)

Solution (Python):
num=int(input())
word=str(input())
w=[0]
e=[0]
if word[0]=='W':
	w=[1]
	e=[0]
else:
	w=[0]
	e=[1]
for i in range(1,num):
	if word[i]=='W':
		w.append(w[i-1]+1)
		e.append(e[i-1])
	else:
		w.append(w[i-1])
		e.append(e[i-1]+1)
n_th=e[num-1]
for i in range(1,num):
	n=w[i-1]+e[num-1]-e[i]
	if n_th>n:
		n_th=n

print (n_th)

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

pre_count = 0
post_count = len([k for k in range(1, N) if S[k] == 'E'])
count = post_count
for i in range(1, N):
    if S[i-1] == 'W':
        pre_count += 1
    if S[i] == 'E':
        post_count -= 1
    if count > pre_count + post_count:
        count = pre_count + post_count

print(count)

Solution (Python):
n=int(input())
s=list(input())
cnt=s[1:].count("E")
e_cnt=[0]*(n+1)
w_cnt=[0]*(n+1)
e_cnt[0]=cnt
for i in range(n):
    w_cnt[i+1]=w_cnt[i]+(s[i]=="W")
for i in range(1,n):
    e_cnt[i]=e_cnt[i-1]-(s[i]=="E")
ans=float("inf")
for i in range(n):
    ans=min(ans,w_cnt[i]+e_cnt[i])
print(ans)

Solution (Python):
n=int(input())
s=list(input())
cnt=s[1:].count("E")
e_cnt=[0]*(n+1)
w_cnt=[0]*(n+1)
e_cnt[0]=cnt
for i in range(n):
    w_cnt[i+1]=w_cnt[i]+(s[i]=="W")
for i in range(1,n):
    e_cnt[i]=e_cnt[i-1]-(s[i]=="E")
ans=float("inf")
for i in range(n):
    ans=min(ans,w_cnt[i]+e_cnt[i])
print(ans)

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

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

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

Solution (Python):
def C(n, s):
    n = len(s)
    cr = 0
    cl = 0
    for i in range(1, n):
        if s[i] == "E":
            cl += 1
    
    min = cr + cl
    for i in range(1, n):
        if s[i - 1] == "W":
            cr += 1
        
        if s[i] == "E":
            cl -= 1
        
        if cr + cl < min:
            min = cr + cl
    
    return min

n = int(input())
s = input()
print(C(n, s))

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

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

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

print(n)

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

w=e=0
for s in S:
  if s=='W':
    w+=1
  else:
    e+=1

if S[0]=='W':
  v=e
else:
  v=e-1

r=v
for p in range(1, N):
  if S[p]=='W':
    t=v
    if S[p-1]=='W':
      t+=1
  else:
    t=v-1
    if S[p-1]=='W':
      t+=1
  if t<r:
    r=t
  v=t

print(r)

Solution (Python):
n=int(input())
a=list(input().strip())
w=0
wa=[]
for i in a:
  if i=="W":
    w+=1
  wa.append(w)

e=0
ea=[]
for i in a[::-1]:
  if i=="E":
    e+=1
  ea.append(e)
ea=ea[::-1]

ans=3 * 10**5
for i,j in zip(wa,ea):
  ans=min(ans,i+j-1)

print(ans)

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

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

    cnt = [s[1:n].count("E")]

    for i in range(1, n):
        cnt.append(cnt[i-1] - int(s[i] is "E") + int(s[i-1] is "W"))

    print(min(cnt))

if __name__=='__main__':
   solve()

Solution (Python):
n = input()
s = raw_input()
n_w = len(filter(lambda c: c=="W", s))
n_e = n - n_w
n_i_w = 0
n_i_e = 0
ans = n
for i in range(n):
    if s[i] == "W":
        n_i_w += 1
    else:
        n_i_e += 1
    cur = n_i_w + n_e - n_i_e
    if s[i] == "W":
        cur -= 1
    ans = min(ans, cur)
print ans

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

es = [0]
for c in S:
    es.append(es[-1] + int(c=='E'))

ans = N
for i in range(N):
    tmp = i - es[i] + es[N] - es[i+1]
    ans = min(ans, tmp)
print(ans)

Solution (Python):
def EW(x, char):
    re = 0
    if x == char:
        re = 1
    return re
        
def main(num, string):
    e = list(map(lambda x:EW(x, 'E'), string))
    w = list(map(lambda x:EW(x, 'W'), string))
    
    for i in range(1, num):
        w[i] = w[i-1] + w[i]
        e[num - i -1] = e[num - i  - 1] + e[num - i]
    
    w.insert(0, 0)
    w.insert(num+1,0)
    e.insert(0, 0)
    e.insert(num+1, 0)
    cnt = num
    for i in range(1, num + 1):
        tmp = w[i-1] + e[ i + 1]
        if tmp < cnt:
            cnt = tmp
    return cnt

num = int(input())
string = input()
 
print(main(num, string))

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

Solution (Python):
N = int(input())
S = input()
    
total = []
_sum1 = []
_sum2 = []
if S[0] == "W":
    _sum1.append(1)
else:
    _sum1.append(0)
if S[-1] == "E":
    _sum2.append(1)
else:
    _sum2.append(0)

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

j = 0
for k in _sum2[::-1]:
    total.append(_sum1[j] + k)
    j += 1

print(min(total)-1)

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

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

def cal(data, r, c):
    for i in range(1,N):
        if data[i-1] == "W" and data[i] == "W":
            c += 1
        elif data[i-1] == "E" and data[i] == "E":
            c -= 1
            if r > c:
                r = c
    return r

if S[0] == "E":
    count = S.count("E") -1
    result = count
    result = cal(S, result, count)
else:
    count = S.count("E")
    result = count
    result =  cal(S, result, count)


print(result)