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()
countr = 0
countl = 0
r = [0 for i in range(n-1)]
l = [0 for i in range(n-1)]
for i in range(n-1):
    if s[i] == 'W':
        countr += 1
    if s[n-i-1] == 'E':
        countl += 1
    r[n-i-2] = countr
    l[i] = countl
mi = min(countl,countr)
for i in range(n-1):
    mi = min(mi,r[i]+l[i])
print(mi)

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

mn = 10**6
for i in range(n):
    if i==0:
        left_W = 0
        right_E = (n-1)-(ruiseki[n-1]-ruiseki[0])
    elif i==n-1:
        left_W = ruiseki[n-2]
        right_E = 0
    else:
        left_W = ruiseki[i-1]
        right_E = (n-(i+1))-(ruiseki[n-1]-ruiseki[i])
    
    ans = left_W + right_E
    mn = min(mn,ans)
print(mn)

Solution (Python):
import sys

n, s = sys.stdin.read().split()

def main():
    cnt = s.count('E')
    cur = 'E'
    res = cnt
    for c in s:
        if cur == 'W': cnt += 1
        if c == 'E': cnt -= 1
        res = min(res, cnt)
        cur = c
    print(res)
    
if __name__ ==  '__main__':
    main()

Solution (Python):
import sys

n, s = sys.stdin.read().split()

def main():
    cnt = s.count('E')
    cur = 'E'
    res = [cnt]
    for c in s:
        if cur == 'W': cnt += 1
        if c == 'E': cnt -= 1
        res.append(cnt)
        cur = c
    print(min(res))
    
if __name__ ==  '__main__':
    main()

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

t=[0]*n
if s[0]=="W":
  t[0]=-1
else:
  t[0]=1
for i in range(1,n):
  if s[i]=="W":
    t[i]=-1+t[i-1]
  else:
    t[i]=1+t[i-1]
    
max1=max(t)
a=t.index(max1)

ans=0
ans+=s[:a].count("W")

ans+=s[a+1:].count("E")
print(ans)

Solution (Python):




mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    n = int(input())
    s = input()
    min_ = float('inf')
    lw = 0
    le = 0
    rw = s.count('W')
    re = s.count('E')

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

if __name__ == '__main__':
  main()

Solution (Python):
n = int(input())
s = list(input())
rignt = 0
left = 0


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

if s[0] == "E":
  rignt -= 1

ans = rignt

for i in range(1,n):
  if s[i-1] == "E":
    left += 1
    
  if s[i] == "E":
    rignt -= 1
    
  temp = rignt + (i-left) 
  
  if temp < ans :
    ans = temp
    
print(ans)

Solution (Python):
def cal(n,s):
	cum = np.cumsum(s == 'W')
	val = [-1]*n
	for i in range(n):
		W = cum[i-1]  if i-1 >= 0 else 0 
		H = (n-i-1) - (cum[-1] - cum[i])	 
		val[i] = W+H
	return min(val)

import numpy as np

n = int(input())
s = np.array(list(input()))
print(cal(n,s))

Solution (Python):
def main():
  n=int(input())
  s=input()
  E=s.count('E')
  e=[0]*n
  e[0]=1 if s[0]=='E' else 0
  ans=E-e[0]
  for i in range(1,n):
    e[i]=e[i-1] + (1 if s[i]=='E' else 0)
    x=E-e[i]+i-e[i-1]
    ans=ans if ans<=x else x
  print(ans)
if __name__=='__main__':
  main()

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

wcount = [0 for i in range(N)]
ecount = [0 for i in range(N)]
sumcount = [0 for i in range(N)]

for i in range(1,N):
    if i == 0:
        continue
    elif i > 0 and S[i-1] == "W":
        wcount[i] = wcount[i-1] + 1
    elif i > 0:
        wcount[i] = wcount[i-1]
 
for i in reversed(range(N)):
    if i == N:
        continue
    elif i < N-1 and S[i+1] == "E":
        ecount[i] = ecount[i+1] + 1
    elif i < N-1:
        ecount[i] = ecount[i+1]
    

for i in range(N):
    sumcount[i] = wcount[i] + ecount[i]

print(min(sumcount))

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

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

l = [0]
r = [0]
l_cnt = 0
r_cnt = 0
for i in range(n-1):
    if s[i] == 'W':
        l_cnt += 1
    if s[n-i-1] == 'E':
        r_cnt += 1
    l.append(l_cnt)
    r.append(r_cnt)
print(min(l[i]+r[n-i-1] for i in range(n)))

Solution (Python):
N = int(input())
S = list(input())
INF = int(1e6)

nE = S.count("E")
nW = N - nE

lnW = 0
ans = INF

for i in S:
    if i == "W":
        nW -= 1
        ans = min(ans, nE + lnW)
        lnW += 1
    else:
        nE -= 1
        ans = min(ans, nE + lnW)

print(ans)

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

res = N
num = [0] * (N+1)
for i in range(0, N):
    num[i + 1] = num[i]
    if A[i] == 'W':
        num[i + 1] += 1
for i in range(1, N + 1):
    res = min(res, num[i - 1] + (N - i) - (num[N] - num[i]))

print(res)

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

Solution (Python):
n = int(input())
s = input()
 
W_csum = [0] * (n+1)
E_csum = [0] * (n+1)
for i in range(n):
    if s[i] == 'W':
        W_csum[i+1] = W_csum[i] + 1
        E_csum[i+1] = E_csum[i]
    else:
        W_csum[i+1] = W_csum[i]
        E_csum[i+1] = E_csum[i] + 1
E_csum.append(E_csum[-1])

ans = float('inf')
for i in range(1,n+1):
    count = 0
    count += W_csum[i-1]
    count += E_csum[-1] - E_csum[i]
    if count < ans:
        ans = count
print(ans)

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

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

W_csum_r = [0] * n
E_csum_r = [0] * n
if s[-1] == 'W':
    W_csum_r[0] = 1
else:
    E_csum_r[0] = 1
for i,j in enumerate(range(n-2,-1,-1),1):
    if s[j] == 'W':
        W_csum_r[i] = W_csum_r[i-1] + 1
        E_csum_r[i] = E_csum_r[i-1]
    else:
        W_csum_r[i] = W_csum_r[i-1]
        E_csum_r[i] = E_csum_r[i-1] + 1

W_csum_r.reverse()
E_csum_r.reverse()

ans = float('inf')
for i in range(1,n-1):
    num = 0
    if s[i] == 'W':
        num += E_csum_r[i+1]
        num += W_csum[i-1]
    else:
        num += E_csum_r[i+1]
        num += W_csum[i-1]
    if num < ans:
        ans = num

if ans > E_csum_r[1]:
    ans = E_csum_r[1]
if ans > W_csum[-2]:
    ans = W_csum[-2]

print(ans)

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

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

turn_east_list=[0]
for i in reversed(range(N-1)):
  if S[i+1]=="E":
    turn_east_list.append(turn_east_list[-1]+1)
  else:
    turn_east_list.append(turn_east_list[-1])
turn_east_list=list(reversed(turn_east_list))




answer=10**9
for i in range(N):
  answer=min(answer,turn_west_list[i]+turn_east_list[i])
  
print(answer)

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

l = [0]
r = [0]
l_cnt = 0
r_cnt = 0
for i in range(n-1):
    if s[i] == 'W':
        l_cnt += 1
    if s[n-i-1] == 'E':
        r_cnt += 1
    l.append(l_cnt)
    r.append(r_cnt)
print(min(l[i]+r[n-i-1] for i in range(n)))

Solution (Python):
import sys
sys.setrecursionlimit(10000)
def resolve():
    N = int(input())
    S = input()
    mem = [None for i in range(N)]
    mem[0] = S[1:].count("E")
    for i in range(1, N):
        if S[i-1] == "W" and S[i] == "W":
            mem[i] = mem[i-1] + 1
        elif S[i-1] == "E" and S[i] == "E":
            mem[i] = mem[i-1] - 1
        else:
            mem[i] = mem[i-1]
    
    print(min(mem))

if '__main__' == __name__:
    resolve()

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

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

Solution (Python):
N = int(input())
s = input()
sl = [0] * (N + 1)
sr = [0] * (N + 1)
for i in range(N):
    if s[i] == 'W':
        sl[i + 1] = sl[i] + 1
    else:
        sl[i + 1] = sl[i]
for i in range(N , 0, -1):
    if s[i - 1] == 'E':
        sr[i - 1] = sr[i] + 1
    else:
        sr[i - 1] = sr[i]
min_ = N
for i in range(N + 1):
    min_ = min(min_, sl[i] + sr[i])
print(min_)

Solution (Python):
n = int(input())
s = list(map(str,input()))
count = 0
for i in range(1,n):
  if s[i] == "E":
    count += 1
box = [0]*n
box[0] = count
for j in range(1,n):
  if s[j] == "E":
    count -= 1
  if s[j - 1] == "W":
    count += 1
  box[j] = count
print(min(box))

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



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


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

print(min(arr))

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


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


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

ans = N
for i in range(N):
  tmp = N- 1 - T[i] - U[i] 
  if tmp < ans:
    ans = tmp
print(ans)

Solution (Python):
n = int(input())
s = input()
work = s[1:].count("E")
temp = [work]
leader = s[0]
for c in s[1:]:
    if leader == "W":
        work += 1
    if c == "E":
        work -= 1
    temp.append(work)
    leader = c
print(min(temp))

Solution (Python):
N = int(input())
S = list(input())
INF = int(1e6)

nE = S.count("E")
nW = N - nE

lnW = 0
ans = INF

for i in S:
    if i == "W":
        nW -= 1
        ans = min(ans, nE + lnW)
        lnW += 1
    else:
        nE -= 1
        ans = min(ans, nE + lnW)

print(ans)

Solution (Python):


import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
rr = range


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols
    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])
    return ret






N = read_a_int()
S = read()[:-1]
W = 'W'
E = 'E'

l = 0  
r = S.count(W)  

ans = 10**9
for s in S:
    if s == W:  
        r -= 1

    
    ans = min(ans, N - 1 - (r + l))  

    if s == E:  
        l += 1

print(ans)

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

w_lst = [0] * n
e_lst = [0] * n

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

ans = 10**10
for i in range(n):
    ans = min(ans, w_lst[i] + e_lst[i])
print(ans)

Solution (Python):



n = int(input())
s = input()
lw, re = [0] * n, [0] * n
for i in range(1, n):
    if s[i - 1] == "W":
        lw[i] = lw[i - 1] + 1
    else:
        lw[i] = lw[i - 1]
for i in range(n - 2, -1, -1):
    if s[i + 1] == "E":
        re[i] = re[i + 1] + 1
    else:
        re[i] = re[i + 1]
ans = 10**10
for i in range(n):
    ans = min(ans, lw[i] + re[i])
print(ans)

Solution (Python):
n = int(input())
s = list(input())
cum_s = []
wum = 0
eum = 0
for i in range(n):
    if s[i] == "W":
        wum += 1
    else:
        eum += 1
    cum_s.append([wum,eum])
r = cum_s[-1]
ans = int(1e10)
for i in range(n):
    tar = cum_s[i]
    cnt = 0
    if s[i] == "W":
        cnt += tar[0]-1
        cnt += r[1] - tar[1]
    else:
        cnt += tar[0]
        cnt += r[1] - tar[1]
    ans = min(ans,cnt)
print(ans)

Solution (Python):
def main():
    n = int(input())
    s = input()
    w_ruisekiwa = [0] * (n+1)
    e_ruisekiwa = [0] * (n+1)

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

    mn = 10 ** 10

    for i in range(n):
        l_total = w_ruisekiwa[i] - w_ruisekiwa[0]
        r_total = e_ruisekiwa[n] - e_ruisekiwa[i+1]
        mn = min(l_total + r_total,mn)

    print(mn)






if __name__ == '__main__':
    main()

Solution (Python):

N = int(input())
S = input()
DP_w= []
DP_e = []
tmp = 0
tmpw = 0
tmpe = 0
ans = 10**9
for i in range(N):
    if S[i] == "W":
        tmpw += 1
        DP_w.append(tmpw)
    else:
        DP_w.append(tmpw)

for j in range(N-1,-1,-1):
    if S[j] == "E":
        tmpe += 1
        DP_e.append(tmpe)
    else:
        DP_e.append(tmpe)

DP_e.reverse()
for k in range(N):
    ans = min(ans,DP_w[k]+DP_e[k]-1)

print(ans)

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

Solution (Python):
n = int(input())
list_S = list(input())
left_W = 0
left_E = 0
right_W = list_S.count("W")
right_E = list_S.count("E")
ans = n

for i, s in enumerate(list_S):
    if s == "W":
        left_W += 1
        right_W -= 1
    elif s == "E":
        left_E += 1
        right_E -= 1

    if i == 0 and s == "W":
        ans = min(ans, right_E)
    elif i == n-1 and s == "E":
        ans = min(ans, left_W)
    else:
        ans = min(ans, left_W+right_E)

print(ans)

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

acc_e=[0]
acc_w=[0]
for i in S:
  acc_e.append(acc_e[-1])
  acc_w.append(acc_w[-1])
  if i=='E':
    acc_e[-1]+=1
  else:
    acc_w[-1]+=1

ans=N
for i in range(N):
  turn=acc_w[i]+acc_e[-1]-acc_e[i+1]
  ans=min(ans,turn)
print(ans)

Solution (Python):
n = int(input())
s = str(input())
acc_e = [0]
acc_w = [0]
for c in s:
  acc_e.append(acc_e[-1])
  acc_w.append(acc_w[-1])
  if c == 'E': acc_e[-1] += 1
  else: acc_w[-1] += 1
ans = n
for i in range(n):
  turn = acc_w[i] + acc_e[-1] - acc_e[i+1]
  ans = min(ans, turn)
print(ans)

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

f_inf = float("inf")
res = f_inf
for i in range(n):
    if s[i] == "W":
        t = w[i] - w[0] + e[len(e) - 1] - e[i]
    else:
        t = w[i] + e[len(e) - 1] - e[i]
    res = min(res, t)
print(res)

Solution (Python):
n = int(input())
s = input()
e_cnt = 0
for i in range(n):
    if s[i] == "E":
        e_cnt += 1
ans = e_cnt
w_cnt = 0
for i in range(n):
    if s[i] == "E":
        e_cnt -= 1
        
    ans = min(w_cnt + e_cnt, ans)
    if s[i] == "W":
        w_cnt += 1

print(ans)

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

ans = [0] * N

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


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


print(min(ans))

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

wc = [0]
ec = [0]

for i in range(N):
    if S[i] == "W":
        wc.append(wc[i]+1)
        ec.append(ec[i])
    else:
        wc.append(wc[i])
        ec.append(ec[i]+1)



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

print(ans)

Solution (Python):
n=int(input())
s=input()
wc=[0]
ec=[0]
for i in range(n):
    if s[i]=="W":
        wc.append(wc[i]+1)
        ec.append(ec[i])
    else:
        wc.append(wc[i])
        ec.append(ec[i]+1)
ans=10**6
for i in range(1,n+1):
    tmp=0
    
    tmp+=wc[i-1]
    tmp+=ec[n]-ec[i]
    ans=min(tmp,ans)
print(ans)

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


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

INF = float("inf")
ans = INF
for i in range(n):
    if s[i] == "E":
        sub = -1
    else:
        sub = 0
    change = i - (ruiseki[i] - ruiseki[0] - sub)
    change += ruiseki[-1] - ruiseki[i]
    ans = min(ans, change)

print(ans)

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

S = list(input())

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

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

print(min(ans))

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

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

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

E = [0] * (n + 10)
W = [0] * (n + 10)

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

ans = float("inf")
for i in range(1, n+1):
    x = 0
    x += W[i-1]
    x += E[n]-E[i]
    ans = min(ans, x)

print(ans)

Solution (Python):

N = int(input())
S = input()
e, w = 0, 0
e = S.count("E")
ans = e
for i in range(N):
    if S[i] == "E":
        e -= 1
        ans = min(ans, e+w)
    else:
        w += 1
print(ans)

Solution (Python):

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

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
        else:
            W[i]=W[i-1]+1
    else:
        if i!=0:
            W[i]=W[i-1]
    
    if S[-(i+1)]=="E":
        if i==0:
            E[-(i+1)]=1
        else:
            E[-(i+1)]=E[-i]+1
    else:
        if i!=0:
            E[-(i+1)]=E[-i]

cost=min(W[N-2],E[1])

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

print(cost)

Solution (Python):
import sys

import bisect





from operator import attrgetter, itemgetter





readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


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

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

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

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

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

    print(ans)


if __name__ == '__main__':
    main()

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

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


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


ew=[look_west[i]+look_east[i] for i in range(N)]

print(N-max(ew)-1)

Solution (Python):
n = int(input())
s = input()
left_w = 0
right_e = s.count('E')
ans = left_w + right_e
for c in s:
    if c == 'E':
        right_e -= 1
    ans = min(ans, left_w + right_e)
    if c == 'W':
        left_w += 1
print(ans)

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

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

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

Solution (Python):
import math
import collections
from operator import mul
from functools import reduce



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

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

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

Solution (Python):
N = int(input())
S = list(input())
a = [0]*(N+1)
r = [0]*(N+1)
rr = [0]*(N+1)
l = [0]*(N+1)
ll = [0]*(N+1)
a = [0]*(N+1)

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

for i in range(N+1):
    l[i] = i - r[i]
for i in range(N+1):
    ll[i] = i - rr[i] 

for i in range(N+1):
    a[i] = ll[N-i] + r[i]
    
"""
print(r)
print(rr)
print(l)
print(ll)
print(a)
"""

print(N-max(a))

Solution (Python):
n = int(input())
s = list(map(str, input()))
a = []
cnt = s.count("E")
s.insert(0, 0)
s.append(0)
for i in range(1, n + 1):
    if s[i - 1] == "W":
        cnt += 1
    if s[i] == "E" and cnt != 0:
        cnt -= 1
    a.append(cnt)
print(min(a))

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

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

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

print(min(lst))

Solution (Python):
N = int(input())
S = str(input())
sumL = [0]*N
sumL[0] = S[1:].count('E')
left = 0
right = 0
for i in range(1,N):
  if S[i-1] == 'W':
    left = 1
  else:
    left = 0
  if S[i] == 'E':
    right = 1
  else:
    right = 0
  sumL[i] = sumL[i-1] + left - right
sumL.sort()
print(sumL[0])

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






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

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

    array = [0,S[1:].count('E')]
    ans = sum(array)

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


if __name__ == '__main__':
    main()

Solution (Python):
def Attention(n , s):

    ans = 10**9
    west = [0] * (n + 1)
    east = [0] * (n + 1)

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

    for i in range(n):
        count = west[i] + (east[-1] - east[i + 1])
        ans = min(ans , count)

    return ans

def main():
    n = int(input())
    s = list(str(input()))
    print(Attention(n , s))

if __name__ == '__main__':
    main()

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

lie = [0] * (n+1)
liw = [0] * (n+1)
ans = 10**10

for i in range(n):
    if s[i] == 'E':
        lie[i+1] = lie[i] + 1
        liw[i+1] = liw[i]
    else:
        lie[i+1] = lie[i]
        liw[i+1] = liw[i] + 1

for i in range(n):
    cnte = lie[-1] - lie[i+1]
    cntw = liw[i]
    ans = min(ans, cnte + cntw)

print(ans)

Solution (Python):

from collections import Counter, deque






import sys
sys.setrecursionlimit(10 ** 5 + 10)


def resolve():
    N=int(input())
    S=list(input())
    from itertools import accumulate

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

    from itertools import accumulate

    WW = list(accumulate(WW))
    EE=list(accumulate(EE))


    
    ans=10**10
    for i in range(1, N + 1):
        ans = min(WW[i-1]+EE[N]-EE[i],ans)
    print(ans)



resolve()

Solution (Python):
n=int(input())
s=input()
t=s.count('E')
ans=10**9
e=0
w=0
for i in range(n):
  if s[i]=='E':
    e+=1
    ans=min(ans,t-e+w)
  elif s[i]=='W':
    w+=1
    ans=min(ans,t-e+w-1)
print(ans)

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

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

if s[0]=='E':
    e[0]+=1
else:
    w[0]+=1


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

mn = 10**10
for i in range(n):
    dire = s[i]
    if dire == "E":
        if i != 0 and i != n-1:
            ans = w[i-1] + (e[n-1] - e[i])
        elif i == 0:
            ans = e[n-1] - 1
        else:
            ans = w[n-1]
    else:
        if i != 0 and i != n-1:
            ans = w[i-1] + (e[n-1] - e[i])
        elif i == 0:
            ans = e[n-1]
        else:
            ans = w[i-1]
    if ans < mn:
        mn = ans

print(mn)

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


back = [0] * n
wester, easter = 0, 0
for i, p in enumerate(line):
    back[i] = wester

    if p == 'W':
        wester += 1
i = n
for p in line[::-1]:
    i -= 1
    back[i] += easter

    if p == 'E':
        easter += 1

print(min(back))

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

right_cost = s.count("E")
left_cost = 0

ans = 10 ** 9
for i in range(n):
    if s[i] == "E":
        right_cost -= 1

    ans = min(ans, left_cost + right_cost)

    if s[i] == "W":
        left_cost += 1


print(ans)

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

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

Solution (Python):
import collections
N=int(input())
S=list(input())
c=collections.Counter(S)
Eleft=0
Wleft=0
Eright=c["E"]
Wright=c["W"]
cnt=0
mi=float("inf")
l=[c['E'],c['W']]
for i in range(N):
    if S[i]=="E":
        Eright=Eright-1
    elif S[i]=="W":
        Wright=Wright-1
    
    cnt=Wleft+Eright
    if cnt<mi:
        mi=cnt
    if S[i]=="E":
        Eleft=Eleft+1
    elif S[i]=="W":
        Wleft=Wleft+1
print(mi)

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

e_acc, w_acc = 0, 0
e_acc_list = [0]
w_acc_list = [0]
acc = {'E': 0, 'W': 0}
acc_list = {'E': [0], 'W': [0]}
for s in S:
    if s == 'E':
        acc['E'] += 1
    else:
        acc['W'] += 1
    acc_list['E'].append(acc['E'])
    acc_list['W'].append(acc['W'])

def count(l_ind, r_ind, symbol):
    
    if r_ind < 0 or l_ind > N - 1:
        return 0
    else:
        if symbol == 'E':
            return acc_list['E'][r_ind] - acc_list['E'][l_ind]
        else:
            return acc_list['W'][r_ind] - acc_list['W'][l_ind]

cost = 1e10

for i in range(N):  
    tmp = count(0, i, 'W') + count(i + 1, N, 'E')
    if tmp < cost:
        cost = tmp
print(cost)

Solution (Python):
N = int(input())
S = input()
int_s = [0] * N
cum_sum_w = [0] * (N + 1)
cum_sum_e = [0] * (N + 1)
answers = [0] * N
for i in range(N):
    if (S[i] == 'W'):
        int_s[i] = 1
    else:
        int_s[i] = 0
    cum_sum_w[i + 1] = cum_sum_w[i] + int_s[i]
for i in range(N):
    if (S[i] == 'E'):
        int_s[i] = 1
    else:
        int_s[i] = 0
    cum_sum_e[i + 1] = cum_sum_e[i] + int_s[i]
for i in range(N):
    answers[i] = cum_sum_w[i] + (cum_sum_e[N] - cum_sum_e[i + 1])
print(min(answers))

Solution (Python):
import numpy as np

n = int(input())
s = input()
s_W = [0]
s_E = []

for i in range(n):
    if s[i] == "W":
        s_W.append(1)
        s_E.append(0)
    else:
        s_W.append(0)
        s_E.append(1)

W_l = np.cumsum(np.array(s_W))
E_l = np.cumsum(np.array(s_E))

ans = []
for j in range(n):
    num = W_l[j]-W_l[0]+E_l[-1]-E_l[j]
    ans.append(num)
print(min(ans))

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

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

i = 0
ee = []
ww = []

while i < n:
    j = i
    count = 1
    while j < n-1 and s[j] == s[j+1]:
        count += 1
        j += 1
    if s[j] == 'E':
        ee.append(count)
        ww.append(0)
    else:
        ww.append(count)
        ee.append(0)
    i = j+1
acume = [0 for _ in range(len(ee)+1)]
acumw = [0 for _ in range(len(ww)+1)]
for i, w in enumerate(ww):
    acumw[i+1] = acumw[i] + w
for i, e in enumerate(ee[::-1]):
    acume[i+1] = acume[i] + e
acume = acume[::-1]

minp = 10**6
for i in range(len(acume)):
    minp = min(minp, max(acumw[i]+acume[i], 0))
print(minp)

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

Solution (Python):




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

    turn = []
    east = 0
    total = S.count("E")

    for i in range(N):
        left = i - east
        s = S[i]
        if s == "E":
            east += 1
        right = total - east
        turn.append(left + right)

    print(min(turn))


if __name__ == "__main__":
    main()

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

W_accum = [0]
E_accum = [0]
for i in range(N):
    if S[i] == "W":
        W_accum += [W_accum[-1] +1]
    else:
        W_accum += [W_accum[-1]]
    E_accum += [i+1 - W_accum[-1]]

ans = N
for i in range(N):
    curr = W_accum[i] + (E_accum[-1] - E_accum[i+1])
    ans = min(curr, ans)
print(ans)