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()
ans = n
temp = 0

for i in range(1, n):
    if S[i] =='E':
        temp += 1
ans = min(ans, temp)

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

print(ans)

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

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

Solution (Python):
import sys






N = int(input())
S = str(input())
cusum = [0]*N
cusum[0] = 1 if S[0] == 'E' else 0

for i in range(1,N):
    cusum[i] = cusum[i-1]
    if S[i] == 'E':
        cusum[i]+=1
ans = 999999999999
for i in range(N):
    if i == 0:
        tmp = cusum[N-1] - cusum[0]
    elif i == N-1:
        tmp = N-1 - cusum[N-2]
    else:
        tmp = i - cusum[i-1]
        tmp += cusum[N-1] - cusum[i]
    ans = min(tmp, ans)
print(ans)

Solution (Python):

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

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

print(min)

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

output=S[1:].count("E")
a=output

for i in range(N-1):
    if(S[i]=="W"):
        a+=1
    if(S[i+1]=="E"):
        a-=1
    if(a<output):
        output=a
print(output)

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

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

ma = e[n-1]-e[0]
for i in range(n):
    if i != n-1:
        cos = e[n-1]-e[i] + w[i]
    else:
        cos = w[n-2]
    ma = min(cos,ma)

print(ma)

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

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

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

print(min(num))

Solution (Python):
def main():
    n = int(input())
    ss = [c == 'E' for c in input()]
    c_l = 0
    c_r = len([t for t in ss[1:] if t])
    res = c_r
    for i in range(1, n):
        c_l += int(not ss[i - 1])
        c_r -= int(ss[i])
        res = min(res, c_l + c_r)
    print(res)


if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
s=input()
e=s.count("E")
w=n-e
wc=0
ec=0
min1=300001
for i in range(len(s)):
    if s[i]=="W":
        wc+=1
        el=e-ec
        wl=wc-1
        ans=el+wl
        min1=min(min1,el+wl)    
    else:
        ec+=1
        el=e-ec
        wl=wc
        ans=el+wl
        min1=min(min1,el+wl)
print(min1)

Solution (Python):
n = int(input())
ls = [(0, 0) for _ in range(n)]
s = list(input())
sum_ls = {}

for i in range(n):
    if i == 0:
        w = 0
        e = s[i+1:].count('E')
        sum_ls[i] = (w, e)
    else:
        _w, _e = sum_ls[i - 1]
        if s[i] == 'W':
            if s[i-1] == 'W':
                sum_ls[i] = (_w+1, _e)
            else:
                sum_ls[i] = (_w+1, _e-1)
        else:
            if s[i-1] == 'W':
                sum_ls[i] = (_w, _e)
            else:
                sum_ls[i] = (_w, _e-1)

print(min([sum(v) for v in sum_ls.values()]))

Solution (Python):
N = int(input())
S = list(input())
E = S.count('E')
ans = []
prev = E
pS = 'E'
for i in range(N):
  if S[i] == 'W':
    current = prev
  elif S[i] == 'E':
    current = prev - 1
  if pS == 'W':
    current += 1
  ans.append(current)
  prev = current
  pS = S[i]
print(min(ans))

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

Solution (Python):
def sum_list(l1, l2):
  for i in range(len(l1)):
    l1[i] += l2[i]

  return l1


n = (int)(input())
s = input()
l_e = [0 for _ in range(n+1)]
l_w = [0 for _ in range(n+1)]

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

for i in reversed(range(n)):
  if s[i] == 'E':
    l_w[i] += 1
  l_w[i-1] = l_w[i]

l_e = l_e[:-1]
l_w = l_w[:n]





print(min(sum_list(l_e, l_w))-1)

Solution (Python):
a = int(input())
b = list(input())
e = b.count("E")
w = 0
z = e
for c in b:
    if c == "E":
        e -= 1
    else:
        w += 1
    if e + w < z:
        z = e + w
print(z)

Solution (Python):
N = int(input())
S = input()
e_count = S.count('E')
w_count = 0
ans = e_count

for s in S:
    if s == 'E':
        e_count -= 1
    else:
        w_count += 1
    if e_count + w_count < ans:
        ans = e_count+w_count

print(ans)

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

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

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

print(score)

Solution (Python):

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

tmp = 0
acm = [0] * (N + 1)

for i, Si in enumerate(S):
    if Si == "W": tmp += 1
    else: tmp -= 1
    acm[i + 1] = tmp

minscore = N
for i in range(N):
    L = acm[i]
    R = acm[N] - acm[i + 1]
    if (L - R) < minscore:
        minscore = (L - R)

print((minscore + N - 1) // 2)

Solution (Python):
N = int(input())
S = input()
e_count = S.count('E')
w_count = 0
ans = e_count
for s in S:
    if s == 'E':
        e_count -= 1
    else:
        w_count += 1
    if e_count + w_count < ans:
        ans = e_count + w_count
print(ans)

Solution (Python):
N = int(input())
S = input()
e_count = S.count('E')
w_count = 0
ans = e_count
for s in S:
    if s == 'E':
        e_count -= 1
    else:
        w_count += 1
    ans = min(ans, e_count + w_count)
print(ans)

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

E_count = S.count('E')
W_count = 0
mini_count = E_count

for i in S:
  if i == 'E':
    
    E_count -= 1
  else:
    
    W_count += 1
  if mini_count > E_count + W_count:
    mini_count = E_count + W_count
print(mini_count)

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

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

ans = 1e100
for i in range(1, N + 1):
    lt = lefts[i]
    rt = lefts[-1]

    lw = lt[0]
    le = lt[1]
    rw = rt[0] - lw
    re = rt[1] - le
    if S[i - 1] == "W":
        lw -= 1
    else:
        le -= 1
    ans = min(ans, lw + re)
print(ans)

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

dp = [0] * n

WsW = 0
WsE = 0
EsW = 0
EsE = 0
count = float('inf')

for i in range(n):
    if s[i] =="W":
        WsW += 1
    else:
        WsE += 1

for j in range(n):
    if s[j] == "W" :
        WsW -= 1
    else:
        WsE -= 1
    count = min(count, WsE + EsW)
    if s[j] == "W" :
        EsW += 1
    else:
        EsE += 1
print(count)

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

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

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

Solution (Python):
n = int(input())
s = input()
a = s[1:].count('E')
inf = float('inf')
ans = [inf]*n
ans[0] = a
for i in range(1,n):

    if s[i] == 'E':
        ans[i] = ans[i-1] - 1
    else:
        ans[i] = ans[i-1]

    if s[i-1] == 'W':
        ans[i] += 1
    else:
        continue

print(min(ans))

Solution (Python):
n = int(input())
s = input()
a = s[1:].count('E')
inf = float('inf')
ans = [inf]*n
ans[0] = a
for i in range(1,n):
    if s[i] == 'E':
        ans[i] = ans[i-1] - 1
        if s[i-1] == 'W':
            ans[i] += 1
        else:
            continue
    else:
        ans[i] = ans[i-1]
        if s[i-1] == 'W':
            ans[i] += 1
        else:
            continue
print(min(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):
n = int(input())
s = list(str(input()))
p = s.count("E")
ans = p
for i in s:
    if i == "W":
        p += 1
    else:
        p -= 1
    ans = min(p, ans)
print(ans)

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

Solution (Python):
n = int(input())
s = input()
s = list(s)
allli = {"w":s.count("W"),"e":s.count("E")}
leftli = {"w":0,"e":0}
rightli = allli
coli = []
for i in range(n):
    if i == 0:
        if s[i] == "W":
            rightli["w"] -= 1
        else:
            rightli["e"] -= 1
        coli.append(rightli["e"])
    else:
        if s[i - 1] == "W":
            leftli["w"] += 1
        else:
            leftli["e"] += 1
        if s[i] == "W":
            rightli["w"] -= 1
        else:
            rightli["e"] -= 1
        coli.append(rightli["e"] + leftli["w"])

print(min(coli))

Solution (Python):
N = int(input())
S = input()
ans = [0] * N
ans[0] = S[1:].count('E')
num = ans[0]

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

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

c = 0
for i in range(1, N):
    if S[i] == 'E':
        c += 1
dp = [c]

for i in range(1, N):
    c = dp[i-1]
    if S[i-1] == 'W':
        c += 1
    if S[i] == 'E':
        c -= 1
    dp.append(c)
dp.sort()
print(dp[0])

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

c = 0
for i in range(1, N):
    if S[i] == 'E':
        c += 1
dp = [{'i': 0, 'c': c}]

for i in range(1, N):
    c = dp[i-1]['c']
    if S[i-1] == 'W':
        c += 1
    if S[i] == 'E':
        c -= 1
    dp.append({'i': i, 'c': c})
dp.sort(key=lambda x:x['c'])
print(dp[0]['c'])

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

Wsum = [0]
for i in range(N):
    if S[i] == 'W':
        Wsum.append(Wsum[i] + 1)
    else:
        Wsum.append(Wsum[i])
del Wsum[0]

S = S[::-1]
Esum = [0]
for i in range(N):
    if S[i] == 'E':
        Esum.append(Esum[i] + 1)
    else:
        Esum.append(Esum[i])
del Esum[0]
Esum = Esum[::-1]

Sum = []
for i in range(0,N):
    Sum.append(Wsum[i] + Esum[i]-1)

print(min(Sum))

Solution (Python):
n=int(input())
s=list(input())
ls=[0 if i=='W' else 1 for i in s]
a=sum(ls)

l=[0]*n
for i in range(n):
    if ls[i]==1:
        a-=1
        l[i]=a
    elif ls[i]==0:
        a+=1
        l[i]=a-1
print(min(l))

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

es,ws = [],[]
if s[0] == 'E':
    es.append(1)
    ws.append(0)
else:
    es.append(0)
    ws.append(1)

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

ans = 9999999
for i in range(n):
    if i == 0:
        ans = min(ans,es[n-1] - es[0])
    else:
        ans = min(ans,ws[i-1] + es[n-1] - es[i])
print(ans)

Solution (Python):
def 解():
    iN = int(input())
    sS = input().rstrip()

    aE= [0]*iN 
    for i in range(1,iN):
        if sS[i-1] == "W":
            aE[i] = aE[i-1]+1
        else:
            aE[i] = aE[i-1]
    iMinCost = aE[iN-1]
    iW = 0 
    for i in range(iN-2,-1,-1):
        if sS[i+1]=="E":
            iW += 1
        iMinCost = min(iMinCost,iW+aE[i])
    print(iMinCost)

解()

Solution (Python):
def 解():
    iN = int(input())
    sS = input().rstrip()

    aE= [0]*iN 
    aW= [0]*iN 
    for i in range(1,iN):
        if sS[i-1] == "W":
            aE[i] = aE[i-1]+1
        else:
            aE[i] = aE[i-1]
    iMinCost = aE[iN-1]
    for i in range(iN-2,-1,-1):
        if sS[i+1]=="E":
            aW[i] = aW[i+1]+1
        else:
            aW[i] = aW[i+1]
        iMinCost = min(iMinCost,aW[i]+aE[i])
    print(iMinCost)

解()

Solution (Python):
def 解():
    iN = int(input())
    sS = input().rstrip()

    aE= [0]*iN 
    aW= [0]*iN 
    for i in range(1,iN):
        if sS[i-1] == "W":
            aE[i] = aE[i-1]+1
        else:
            aE[i] = aE[i-1]
    for i in range(iN-2,-1,-1):
        if sS[i+1]=="E":
            aW[i] = aW[i+1]+1
        else:
            aW[i] = aW[i+1]

    print(min(map(lambda x,y:x+y,aW,aE)))
解()

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

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


ans = float('inf')
for i in range(n):
    count = 0
    if i == 0:
        count = east[n-1] - east[0]
    elif i == n-1:
        count = west[n-2]
    else:
        count = west[i-1] + (east[n-1] - east[i])
    ans = min(ans, count)

print(ans)

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

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

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

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

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

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

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

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

SUM=[E[i]+W[i] for i in range(N)]
print(N-max(SUM))

Solution (Python):
import sys
stdin = sys.stdin

sys.setrecursionlimit(10**5)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n = ni()
s = lc()

le = 0
lw = 0
re = s[1:].count("E")
rw = s[1:].count("W")

ans = lw + re

for i in range(1,n):
    if s[i-1] == "E":
        le += 1
    else:
        lw += 1
        
    if s[i] == "E":
        re -= 1
    else:
        rw -= 1
        
    ans = min(ans, lw+re)

print(ans)

Solution (Python):
N = int(input())
L = list(input())
ANS = []
ans = L.count("E")
for i in range(len(L)) :
    if L[i] == "E" :
        ans -= 1
    if L[i-1] == "W" and i != 0 :
        ans += 1
    ANS.append(ans)
print(min(ANS))

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

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

nisi=[0]*n
for i in range(n):
    if(s[i]=="W"):
        nisi[i]=1

ruiseki=[0]
for i in range(n):
    ruiseki.append(ruiseki[i]+nisi[i])



ans=[]

for i in range(n):
    ans.append(ruiseki[i] + (n-i-1) - (ruiseki[-1] - ruiseki[i+1]) )

print(min(ans))

Solution (Python):
N = int(input())
S = input()
num_E = []
num_W = []
num_All = []
count_W = 0
count_E = 0

for i in range(N):
  if S[i] == 'W':
    count_W += 1
  if S[N-i-1] =='E':
    count_E += 1
  num_W.append(count_W)
  num_E.append(count_E)

num_E.reverse()
num_All = [x + y for (x, y) in zip(num_W, num_E)]

print(min(num_All)-1)

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

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

cost = S.count("E")

ans = 1000000
for si in S:
    if si == "E":
        cost -= 1
    else:
        if cost < ans:
            ans = cost
        cost += 1
else:
    if cost < ans:
        ans = cost
print(ans)

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

cost = list(S).count("E")

ans = 1000000
for si in S:
    if si == "E":
        cost -= 1
    else:
        if cost < ans:
            ans = cost
        cost += 1
else:
    if cost < ans:
        ans = cost
print(ans)

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

cost = list(S).count("E")

ans = 1000000
for si in S:
    if si == "E":
        cost -= 1
    elif si == "W":
        if cost < ans:
            ans = cost
        cost += 1
else:
    if cost < ans:
        ans = cost
print(ans)

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

cost = list(S).count("E")

ans = 10 ** 6
for si in S:
    if si == "E":
        cost -= 1
    
    if cost < ans:
        ans = cost

    if si == "W":
        cost += 1

print(ans)

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

cost = list(S).count("E")

ans = 10 ** 6
for si in S:
    if si == "E":
        cost -= 1
    
    ans = min(ans, cost)

    if si == "W":
        cost += 1

print(ans)

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

leftWest = 0
rightEast = list(S).count("E")

ans = 10 ** 6
for si in S:
    if si == "E":
        rightEast -= 1
    
    ans = min(ans, leftWest + rightEast)

    if si == "W":
        leftWest += 1

print(ans)

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

leftWest = 0
rightEast = S.count("E")

ans = 10 ** 6
for i in range(len(S)):
    if S[i] == "E":
        rightEast -= 1
    
    ans = min(ans, leftWest + rightEast)

    if S[i] == "W":
        leftWest += 1

print(ans)

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

leftWest = 0
rightEast = list(S).count("E")

ans = 10 ** 6
for i in range(len(S)):
    if S[i] == "E":
        rightEast -= 1
    
    ans = min(ans, leftWest + rightEast)

    if S[i] == "W":
        leftWest += 1

print(ans)

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

n_e = 0
for ss in s:
    if ss == 'E':
        n_e += 1
n_w = n - n_e

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

Solution (Python):

N=int(input())
S=list(str(input()))
W=[0]*N
Turn=[0]*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]=0
        else:
            W[i]=W[i-1]
for i in range(N):
    if i==0:
        Turn[i]=0+((N-1-i)-(W[N-1]-W[i]))
    else:
        Turn[i]=W[i-1]+((N-1-i)-(W[N-1]-W[i]))
print(min(Turn))

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

count = 0
for i in range(1, len(s)):
    if s[i] == 'E':
        count += 1
minc = count
for i in range(1, len(s)):
    if s[i - 1] == 'W':
        count += 1
    if s[i] == 'E':
        count -= 1
    minc = min(minc, count)
print(minc)

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

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

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

Solution (Python):
input()
ls = []
from numpy import cumsum
for i in input():
    if i == 'W':
        ls.append(1)
    else:
        ls.append(0)
a = cumsum(ls)
ans = (len(a)-1)-(a[-1]-a[0])
for i in range(1,len(a)):
    ans = min(ans,(len(a)-(i+1))-(a[-1]-a[i])+a[i-1])
print(ans)

Solution (Python):
N = int(input())
S = input()
ans = N
x = 0
y = S.count('E')
for i in range(N):
    if S[i-1] == 'W' and i > 0:
        x += 1
    if S[i] == 'E':
        y -= 1
    if x + y < ans:
        ans = x + y
print(ans)

Solution (Python):
n = int(input())
s = input()
e_l = [0] * (n+1)
w_l = [0] * (n+1)
for i in range(n):
    if s[i] == 'E':
        e_l[i+1] = e_l[i] + 1
    else:
        e_l[i+1] = e_l[i]
for j in range(n, 0, -1):
    if s[j-1] == 'W':
        w_l[j-1] = w_l[j] + 1
    else:
        w_l[j-1] = w_l[j]
ans = 0
for k in range(n):
    res = e_l[k+1] + w_l[k]
    ans = max(ans, res)
print(n-ans)

Solution (Python):
N=int(input())
s=list(input())
a=[0 for i in range(N)]
west=0
east=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):
def run(N, S):
    num_l = [0]
    num_r = [0]
    for i in range(1, len(S)):
        cnt = num_l[i-1]
        if S[i-1] == 'W':
            num_l.append(cnt+1)
        else:
            num_l.append(cnt)
        cnt = num_r[i-1]
        if S[N-i] == 'E':
            num_r.append(cnt+1)
        else:
            num_r.append(cnt)
    ret = max(num_l) + max(num_r)
    for i in range(len(num_l)):
        ret = min(ret, num_l[i]+num_r[N-1-i])
    return ret


def main():
    N = int(input())
    S = input()
    print(run(N, S))


if __name__ == '__main__':
    main()

Solution (Python):


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

LW_cnt = 0
RE_cnt = s[1:].count('E')


numlist = []
for i in range(n):
    if i > 0:
        if s[i-1] == 'W':
            LW_cnt += 1
        if s[i] == 'E':
            RE_cnt -= 1

    numlist.append(LW_cnt+RE_cnt)


print("{}".format(min(numlist)))

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

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

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

del w[0]
del e[0]



l = [0]*n
r = [0]*n

for i in range(n):
    if i != 0:
        l[i] = w[i - 1]
        r[i] = e[n - 1] - e[i]
    else:
        r[0] = e[n-1]


ans = [(l[i] + r[i]) for i in range(n)]

print(min(ans))

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

count = [S[1:].count('E')]
for i in range(1, N):
    check = count[i-1]
    if S[i-1] == 'W':
        check += 1
    if S[i] == 'E':
        check -= 1
    count.append(check)

print(min(count))

Solution (Python):
N = int(input())
S = input()
east = S.count("E")
west = 0
min = east

for i in S:
    if i == "E":
        east -= 1
    else:
        west += 1
    if min > east + west:
        min = east + west

print(min)

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

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

Solution (Python):
n=int(input())
s=input()
c=s[1:n].count('E')
l=[]
l.append(c)
for i in range(1,n):
  if s[i-1:i+1]=='WW':c+=1
  elif s[i-1:i+1]=='EE':c-=1
  l.append(c)
print(min(l))

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

nums = [s[1:].count('E')]
for i in range(1,n):
  tmp = nums[i-1]
  if s[i-1] == 'W':
    tmp  += 1
  if s[i] == 'E':
    tmp -= 1
  nums.append(tmp)

print(min(nums))

Solution (Python):
N=int(input())
arr=[0 for i in range(N+1)]
for i,x in enumerate(input()):
    if x == 'W':
        arr[i+1]+=arr[i]+1
    else:
        arr[i+1]=arr[i]

min_=float('inf')
for l in range(N):
    tmp = arr[l]+N-arr[N]-l-1+arr[l+1]
    if tmp<min_:
        min_=tmp
print(min_)

Solution (Python):
import numpy as np

n = int(input())
s = input()
reverse_list_1 = [0]
reverse_list_2 = [0]
ans = n

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

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

reverse_list_2 = list(reversed(reverse_list_2))

for i in range(n):
    if reverse_list_1[i]+reverse_list_2[i] < ans:
        ans = reverse_list_1[i]+reverse_list_2[i]
print(ans)

Solution (Python):


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

reverse = 3*10**5+1
west = []
east = []
west_num = 0
east_num = 0
for i in range(len(s)):
    if s[i] == "W":
        west_num += 1
    elif s[i] == "E":
        east_num += 1
    west.append(west_num)
    east.append(east_num)

for i in range(n):
    if i == 0:
        point = east[-1]+east[0]
    elif i == n-1:
        point = west[i-1]
    else:
        point = east[-1]-east[i]+west[i-1]
    
    reverse = min(point, reverse)

print(reverse)

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

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

Solution (Python):
N = int(input())
S = str(input())
cnt = []
count = 0

for i in range(1,len(S)):
    if S[i] == "E":
        count += 1
cnt = [count]*N

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

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

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

Solution (Python):
n = int(input())
s = str(input())
w = 0
e = s[1:n].count("E")
m = w + e
for i in range(n-1):
  if s[i] == "W":
    w = w + 1
  if s[i+1] == "E":
    e = e - 1
  if w + e < m:
    m = w + e
print(m)

Solution (Python):
N = int(input())
S = input()
l = 0
r = S[1:].count('E')
ans = l+r

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

print(ans)

Solution (Python):
N = int(input())
S = input()
ans = 3 * 10 ** 5
w = S.count('W')
w_r = 0

for i, a in enumerate(S):
	if a == 'W':
		w_r += 1
		ans = min(ans, 2*w_r+N-w-i-2)
	else:
		ans = min(ans, 2*w_r+N-w-i-1)
	

print(ans)

Solution (Python):


INF = float('inf')


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

num_E = S[:].count('E')
num_W = S[:].count('W')


e = num_E
w = num_W
ans = INF
for s in S:
    cost = 0
    if s == 'E':
        e -= 1
        cost = e + (num_W - w)
    else:
        w -= 1
        cost = e + (num_W - w - 1)
    ans = min(ans, cost)

print(ans)

Solution (Python):


INF = float('inf')


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

num_E = 0
num_W = 0
for s in S:
    if s == 'E':
        num_E += 1
    else:
        num_W += 1

e = num_E
w = num_W
ans = INF
for s in S:
    cost = 0
    if s == 'E':
        e -= 1
        cost = e + (num_W - w)
    else:
        w -= 1
        cost = e + (num_W - w - 1)
    ans = min(ans, cost)

print(ans)

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

def main():
    min_index = 0
    min_num = 0
    curr = 0
    for i in range(N):
        if S[i] == "E":
            curr -= 1
        else:
            curr += 1
        if min_num > curr:
            min_index = i
        min_num = min(min_num, curr)
    
    left = S[:min_index]
    right = S[min_index + 1:] 
    ans = left.count("W") + right.count("E")
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
s=list(input())
e_count=s.count("E")
w_count=n-e_count
w_left=0
e_right=e_count
c=[]
for i in range(n):
    if s[i]=='W':
        c+=[w_left+e_right]
        w_left+=1
    else:
        e_right-=1
        c+=[w_left+e_right]
print(min(c))

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


ans=[]
count=0

for i in range(1,N):
    
    if S[i]=="E":
        count+=1    
        
        
ans.append(count)


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

    ans.append(count)

print(min(ans))

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

le=0
ri=s[1:n].count("E")

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

for i in range(1,n-1):
    if s[i-1]=="W" :
        le+=1
    if s[i]=="E" :
        ri-=1
    minmuki=min(minmuki,le+ri)

print(minmuki)

Solution (Python):
N=int(input())
S=input()
p_sum=[0]*N
max_sum=0
leader=0
count = 0

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

	if max_sum < p_sum[i]:
		max_sum=p_sum[i]
		leader=i

for i in range(N):
	if i<leader:
		if S[i]=="W":
			count += 1
	elif i>leader:
		if S[i]=="E":
			count += 1

print(count)

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

SUM=[0]*(N+1)
SUM2=[0]*(N+1)
for i in range(N):
    if A[i]=="W":
        SUM[i+1] = SUM[i]+1
        SUM2[i+1] = SUM2[i]
    else:
        SUM2[i+1] = SUM2[i]+1
        SUM[i+1] = SUM[i]
    
SUM3 = [SUM[i]+(SUM2[-1]-SUM2[i+1]) for i in range(N)]
print(min(SUM3))

Solution (Python):


n = int(input())
S = input()

Wcnt = [0]
for i in range(n):
    if S[i] == "W":
        Wcnt.append(Wcnt[i]+1)
    else:
        Wcnt.append(Wcnt[i])



C = []

for i in range(n):
    cl = Wcnt[i]-Wcnt[0]
    cr = n-i-1-(Wcnt[n]-Wcnt[i+1])
    C.append(cl + cr)
    
print(min(C))

Solution (Python):
N = int(raw_input())
S = raw_input()
slist = []
for i in range(0, N):
	slist.append(S[i])
min = slist.count('E')
if slist[0] == 'E':
	min = min - 1
cnt = min
for i in range(1, N):
	if slist[i] == 'E':
		cnt = cnt - 1
	if slist[i-1] == 'W':
		cnt = cnt + 1
	if cnt < min:
		min = cnt

print min

Solution (Python):
n = int(input())
s = 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))