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

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

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

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

w.append(w[-1])
e.insert(0, e[0])

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

print(ans)

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

cntw = 0
cnte = 0
lstw = [0] * N
lste = [0] * N

for i in range(N):
    lstw[i] = cntw
    lste[N-i-1] = cnte
    if(S[i] == 'W'):
        cntw += 1
    if(S[N-i-1] == 'E'):
        cnte += 1 
    
ans = N
for i in range(N):
    ans = min(ans,lstw[i]+lste[i])

print(ans)

Solution (Python):
n = int(input())
s = input()
ans = n
lstw = [0]*n
lste = [0]*n
cntw = 0
cnte = 0

for i in range(n):
    lstw[i] = cntw
    if s[i] == "W": cntw += 1
    lste[n-1-i] = cnte
    if s[n-1-i] == "E": cnte += 1

for i in range(n):
    ans = min(lstw[i]+lste[i],ans)

print(ans)

Solution (Python):
N=int(input())
S=input()
E=[0]*N
W=[0]*N
if S[0]=="E":
    E[0]=1
else:
    W[0]=1
for i in range(N):
    if S[i]=="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=E[-1]-E[0]
for i in range(1,N):
    num=W[i-1]+E[-1]-E[i]
    ans=min(ans,num)
print(ans)

Solution (Python):
N = int(input())
S = list(input())
origin = S[1:]
summary = [origin.count("E")]
for i in range(N-1):
    s = summary[-1]
    if S[i] == "W":
        if S[i+1] == "E":
            delta = 0
        else:
            delta = 1
    else:
        if S[i+1] == "E":
            delta = -1
        else:
            delta = 0
    summary.append(delta+s)
print(min(summary))

Solution (Python):
def solve():
    N = int(input())
    S = list(input())
    rw = [0] * N
    re = [0] * N
    
    cntw = 0
    cnte = 0
    for i in range(N-1,-1,-1):
        if S[i] == 'W':
            cntw += 1
        else:
            cnte += 1
        
        rw[i] = cntw
        re[i] = cnte

    ans = N
    lw = 0
    le = 0
    for i in range(N):
        if i == N-1:
                ans = min(ans, lw)

        else:
            if S[i] == 'W':
                ans = min(ans, lw + re[i+1])
                lw += 1
            else:
                ans = min(ans, lw + re[i+1])
                le += 1
        
    print(ans)

if __name__ == '__main__':
    solve()

Solution (Python):

import sys


def solve(N: int, S: str):
    Right_w, Right_e = S.count("W"), S.count("E")
    Left_w, Left_e = 0, 0

    ans = N

    for si in S:
        
        if si == "W":
            Right_w -= 1
        else:
            Right_e -= 1
        
        ans = min(ans, Left_w + Right_e)
        
        if si == "W":
            Left_w += 1
        else:
            Left_e += 1
    print(ans)


def main():
    N = int(input())
    S = str(input())
    solve(N, S)

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
s = input()
west = [0 for _ in range(n+1)]
east = [0 for _ in range(n+1)]

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

print(ans)

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

cnt_w = [0] * (N+1) 
cnt_e = [0] * (N+1) 
for i in range(N):
    if S[i] == "W":
        cnt_w[i+1] = cnt_w[i] + 1 
    else:
        cnt_w[i+1] = cnt_w[i]
for i in range(N-1,-1,-1):
    if S[i] == "E":
        cnt_e[i] = cnt_e[i+1] + 1 
    else:
        cnt_e[i] = cnt_e[i+1]



ans = [w+e for w,e in zip(cnt_w,cnt_e)]
print(min(ans))

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


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

costl = [0]*n

costl[0] = esuml[-1] - esuml[0]
costl[-1] = wsuml[-2]

for i in range(1,n-1):
  costl[i] = wsuml[i-1] + esuml[-1]-esuml[i]
print(min(costl))

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

count = [0] * N
e_cnt = 0

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

w_cnt = 0

for i in reversed(range(N)):
    if S[i] == "E":
        w_cnt += 1
    count[i] += w_cnt

ans = min(count) - 1

print(ans)

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

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N = I()
    s = S()
    west = [0] * (N+1)
    east = [0] * (N+1)

    for i, c in enumerate(s):
        if c == 'W':
            west[i] = 1
        else:
            east[i] = 1

    for i in range(N):
        west[i+1] += west[i]

    for i in range(N)[::-1]:
        east[i] += east[i+1]

    min_c = inf
    ans_i = -1
    for i in range(N):
        if i == 0:
            tot = east[i+1]
        elif i == N-1:
            tot = west[i-1]
        else:
            tot = west[i-1] + east[i+1]

        min_c = min(min_c, tot)
    print(min_c)
main()

Solution (Python):
N=int(input())
S=list(input())
LE= [0] * (N+1)
RW= [0] * (N+1)
for i in range(1,N+1):
  if S[i-1] == 'E':
    LE[i] = LE[i-1] + 1
  else:
    LE[i] = LE[i-1]
for i in range(N-1,-1,-1):
  if S[i] == 'W':
    RW[i] = RW[i+1] + 1
  else:
    RW[i] = RW[i+1]



ANS = N + 1
for i in range(0,N):
  W = abs((N-1) - i - RW[i+1])
  if i == 0:
    E = 0
  elif i == N - 1:
    W = 0
  else:
  	E = abs(i - LE[i])
  
  ANS = min(ANS, W + E)
print(ANS)

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

a[0]=sum(s[1:])
for i in range(n-1):
  if s[i+1]==1 and s[i]==1:
    a[i+1]=a[i]-1
  elif s[i+1]==1 and s[i]==0:
    a[i+1]=a[i]
  elif s[i+1]==0 and s[i]==1:
    a[i+1]=a[i]
  elif s[i+1]==0 and s[i]==0:
    a[i+1]=a[i]+1
print(min(a))

Solution (Python):
n = int(input())
s = [1 if i == "E" else 0 for i in input()]

dp = [0] * n


dp[0] = sum(s[1:])



for i in range(1, n):
    if s[i - 1] == 0:
        dp[i] = dp[i - 1] + 1 - s[i]
    else:
        dp[i] = dp[i - 1] - s[i]

print(min(dp))

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

W = [0]*n
E = [0]*n

if s[0] == 'W':
	W[0] = 1
if s[-1] == 'E':
	E[-1] = 1

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

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

ans = [0]*n

for k in range(0, n):
	if k == 0:
		ans[0] = E[1]
	elif k == n-1:
		ans[n-1] = W[-2]
	else:
		ans[k] = W[k-1] + E[k+1]

print(min(ans))

Solution (Python):
n = int(input())
s = input()
e_counts = [0] * (n + 1)
w_counts = [0] * (n + 1)
e_counts[0] = 0
w_counts[0] = 0
ans = float("inf")
for i in range(n):
  if s[i] == "E":
    e_counts[i + 1] = e_counts[i] + 1
    w_counts[i + 1] = w_counts[i]
  else:
    e_counts[i + 1] = e_counts[i]
    w_counts[i + 1] = w_counts[i] + 1
for i in range(n):
  ans = min(ans,e_counts[n] - e_counts[i + 1] + w_counts[i])
print(ans)

Solution (Python):
import sys
from itertools import accumulate

def resolve():
        input = sys.stdin.readline
        N=int(input().rstrip())
        
        
        S=list(input().rstrip().replace("W","1").replace("E","0"))

        csum=["0"]+S
        csum=list(map(int,csum))
        csum=list(accumulate(csum))
        ans=N
        for i in range(1,N+1,1):
            numWleft=csum[i-1]-csum[0]
            numEright=(N-i)-(csum[N]-csum[i])
            ans=min(ans,numWleft+numEright)
        print(ans)

from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """5
WEEWW"""
        output = """1"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """12
WEWEWEEEWWWE"""
        output = """4"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """8
WWWWWEEE"""
        output = """3"""
        self.assertIO(input, output)

if __name__ == "__main__":
    
    resolve()

Solution (Python):
N = int(input())
S = input()
A = [0] * (N + 1)
mn = 1000000 
pos = 0 
for i in range(N):
        if S[i] == "W":
                A[i + 1] = A[i] + 1   
        else:
                A[i + 1] = A[i]
for i in range(1, N + 1): 
        mn = min(mn, A[i - 1] + N - i - (A[N] - A[i]))
print(mn)

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

Solution (Python):

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

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

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

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

w = 0
e = 0
for i in s:
  if i =="W":
    pass
  else:
    e +=1
n=e
for i in s:
  if i=="E":
    e-=1
    n = min(n,e+w)
  if i =="W":
    w+=1
print(n)

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

ans=float("inf")

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

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

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

e = [0]
ei = 0

for c in s:
    if c == "E":
        ei += 1
    e.append(ei)

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

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

e = [0]
w = [0]
ei = 0
wi = 0

for c in s:
    if c == "E":
        ei += 1
    else:
        wi += 1
    e.append(ei)
    w.append(wi)
e.append(ei)
w.append(wi)

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

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

tbl = [0] * n

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

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

print(min(tbl))

Solution (Python):
N = int(input())
s = input()
Sww = 0
Se = 0
for i in range(N) :
    if s[i] == 'E' :
        Se+=1
sum = N+10
for i in range(N) :
    if s[i] == 'E' :
        Se-=1
    sum1 = Sww + Se
    sum = min(sum1,sum)
    if s[i] == 'W' :
        Sww+=1
print(sum)

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

for i in range(n):
      if s[i]=="E":
            E[i]=E[i-1]+1
      else:
            E[i]=E[i-1]
for i in reversed(range(n)):
      if i==n-1:
            if s[i]=="W":
                  W[i]=1
            else:
                  W[i]=0
      else:
            if s[i]=="W":
                  W[i]=W[i+1]+1
            else:
                  W[i]=W[i+1]
ans=float("inf")
for i in range(n):
      ans=min(n-(E[i]+W[i]),ans)
print(ans)

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

L = [0] * N
R = [0] * N

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


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


LR = [0] * N
for i in range(N):
  LR[i] = L[i] + R[i]
print(N - max(LR) - 1)

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

Solution (Python):
import sys
input = sys.stdin.readline
N = int(input())
S = list(input())
dp = [[0,0] for _ in range(N)]
cnt = 0
ans = []
for i in range(1,N):
    if S[i] == "E":
        cnt += 1
dp[0][1] = cnt
ans.append(sum(dp[0]))
for j in range(1,N):
    if S[j-1] == "W":
        dp[j][0] = dp[j-1][0] + 1
    else:
        dp[j][0] = dp[j-1][0]
    if S[j] == "E":
        dp[j][1] = dp[j-1][1] - 1
    else:
        dp[j][1] = dp[j-1][1]
    ans.append(sum(dp[j]))
print(min(ans))

Solution (Python):
import sys
input = sys.stdin.readline
N = int(input())
S = list(input())
dp = [[0,0] for _ in range(N)]
cnt = 0
ans = []
for i in range(1,N):
    if S[i] == "E":
        cnt += 1
dp[0][1] = cnt
ans.append(sum(dp[0]))
for j in range(1,N):
    if S[j-1] == "W":
        dp[j][0] = dp[j-1][0] + 1
    else:
        dp[j][0] = dp[j-1][0]
    if S[j] == "E":
        dp[j][1] = dp[j-1][1] - 1
    else:
        dp[j][1] = dp[j-1][1]
    ans.append(sum(dp[j]))
print(min(ans))

Solution (Python):


N=int(input())
S=[i for i in input()]

pls_E=[0]*N
pls_W=[0]*N


if S[N-1]=='E':
    pls_E[N-1]=1
else:
    pls_E[N-1]=0
    
if S[0]=='W':
    pls_W[0]=1
else:
    pls_W[0]=0


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


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


ans=float('inf')
for i in range(1,N-1):
    ans=min(ans,pls_W[i-1]+pls_E[i+1])
    
print(min(ans,pls_E[1],pls_W[N-2]))

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

Solution (Python):
n = int(input())
s = input()
leftW = 0
leftE = 0
allW = s.count("W")
allE = s.count("E")
cost = 0
mincost = n
for si in s:
  if si == "W":
    rightW = allW - leftW - 1
    rightE = allE - leftE
    cost = leftW + rightE
    leftW += 1
  else:
    rightW = allW - leftW
    rightE = allE - leftE - 1
    cost = leftW + rightE
    leftE += 1
  mincost = min(mincost, cost)
print(mincost)

Solution (Python):
n=int(input()) 
s=list(input()) 
left,right=[0],[0] 
left_num,right_num=0,0
for i in range(1,n): 
    if s[i-1]=="W": 
        left_num+=1
    left.append(left_num) 
    if s[n-i]=="E": 
        right_num+=1
    right.append(right_num)


ans=1000000
for i in range(n): 
    num=left[i]+right[n-(i+1)]
    ans=min(ans,num)
print(ans)

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

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

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

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

ans = []
countE = n - countW
left = 0
right = 0

for i in range(n):
    if i == 0:
        if S[0] == 0:
            ans.append(countE - 1)
        else:
            ans.append(countE)
    elif 0 < i < n-1:
        left += S[i-1]
        right = n - i - 1 - countW + left + S[i]
        ans.append(left + right)
    elif i == n-1:
        ans.append(countW - S[-1])
print(min(ans))

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

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

ans = []
countE = n - countW
left = 0
right = 0

for i in range(n):
    if i == 0:
        if S[0] == 0:
            ans.append(countE - 1)
        else:
            ans.append(countE)
    elif 0 < i < n-1:
        left += S[i-1]
        
        right = n - i - 1 - countW + left + S[i]
        ans.append(left + right)
        
        
        
        
    elif i == n-1:
        ans.append(countW - S[-1])



print(min(ans))

Solution (Python):
from itertools import accumulate
N=int(input())
A=list(input())
E=[1 if A[i] =="E" else 0 for i in range(N) ]
W=[1 if A[i] =="W" else 0 for i in range(N) ]
E=list(accumulate(E[::-1]))[::-1]
W=list(accumulate(W))
min_=N
for i in range(N):
    min_=min(min_,E[i]+W[i]-1)
print(min_)

Solution (Python):
N=int(input())
S=input()
S='E'+S+'W'

Es=[0]*(N+2)
Ws=[0]*(N+2)

for i in range(1, N+1):
  if S[i-1]=='E':
    Es[i]=Es[i-1]
  else:
    Es[i]=Es[i-1]+1
    
for i in range(N, 0, -1):
  if S[i+1]=='W':
    Ws[i]=Ws[i+1]
  else:
    Ws[i]=Ws[i+1]+1
    
s=[Es[i]+Ws[i] for i in range(1, N+1)]
print(min(s))

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

cumsum = [0] * n


for i in range(n):
    dir = s[i]
    prev = cumsum[i - 1] if i > 0 else 0
    cur = 1 if dir == 'W' else 0
    cumsum[i] = prev + cur


num_min = 1000000


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

    num_min = min(num_min, w + e)

print(num_min)

Solution (Python):
n = int(input())
s = str(input())
l_w = [0]
l_e = [0]
cnt = 0
lists = []
for i in range(n):
    if s[i] == "W":
        cnt += 1
        l_w.append(cnt)
    else:
        l_w.append(cnt)
cnt = 0
for i in range(n):
    if s[i] == "E":
        cnt += 1
        l_e.append(cnt)
    else:
        l_e.append(cnt)
for i in range(len(l_e)-1):
    ans = l_w[i] + (l_e[-1] - l_e[i+1])
    lists.append(ans)
print(min(lists))

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

w_cnt = 0
e_cnt = 0
E = s.count("E")
ans = 10 ** 9

for i in s:
    if i == "E":
        e_cnt += 1
    ans = min(ans, w_cnt + E - e_cnt)
    if i == "W":
        w_cnt += 1

print(ans)

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

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

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

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

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

ans = float('inf')
for i in range(1, n):
    tmp = west[i - 1] + east[-1] - east[i]
    ans = min(ans, tmp)

ans = min(ans, west[n - 1])
print(ans)

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

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

ans = float('inf')

ans = min(ans, east[-1] - east[2])

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

ans = min(ans, west[n - 1])
print(ans)

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

Solution (Python):


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


num_W = [0]*N 
num_E = [0]*N 

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

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

ans = N
for i in range(N):
    ans = min(ans, num_E[i] + num_W[i])

print(ans)

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


l = 0
li = 0
lmax = -(10**8)
for i in range(N):
    if S[i] == 'W':
        l -= 1
    else:
        l += 1
    if l > lmax:
      lmax = l
      li = i

lans = 0
for i in range(N):
    if i < li:
        if S[i] == 'W':
            lans += 1
    if li < i:
        if S[i] == 'E':
            lans += 1


r = 0
ri = 0
rmax = -(10**8)
for i in reversed(range(N)):
    if S[i] == 'E':
        r -= 1
    else:
        r += 1
    if r > rmax:
      rmax = r
      ri = i

rans = 0
for i in range(N):
    if i < ri:
        if S[i] == 'W':
            rans += 1
    if ri < i:
        if S[i] == 'E':
            rans += 1


print(min(lans, rans))

Solution (Python):

N = int(input())
S = "0" + str(input())


cumsum_E = [0]
cumsum_W = [0]

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

cumsum_E.pop(0)
cumsum_W.pop(0)
        



ans = N

for i in range(0,N):
    E_to_W = cumsum_E[N-1] - cumsum_E[i]
    if i!=0:
        W_to_E = cumsum_W[i-1]
    else:
        W_to_E = 0
    
    change = E_to_W + W_to_E
    
    if change < ans:
        ans = change
        
print(ans)

Solution (Python):
N = int(input())
S = input()
w = 0
e = 0
for s in S:
  if s == "E":
    e += 1
ans = e
for i in range(1,N):
  if S[i-1] == "W":
    w += 1
    if S[i] == "E":
      ans = min(ans, abs(w+e-1))
      continue
  else:
    e -= 1
    if S[i] == "E":
      ans = min(ans, abs(w+e-1))
      continue
  ans = min(ans, abs(w+e))
print(ans)

Solution (Python):
n=int(input())
s=input()
wr=s.count("W")
er=s.count("E")
wl=0
el=0
cur=None
ans=len(s)
for i in range(n):
  if cur=="W":
    wl+=1
  elif cur=="E":
    el+=1
  cur=s[i]
  if cur=="W":
    wr-=1
  else:
    er-=1
    
  ans=min(ans,wl+er)
print(ans)

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



E = []
W = []
e, w = 0, 0

for i in s:
    if i == 'E':
        e += 1
    elif i == 'W':
        w += 1
    E.append(e)
    W.append(w)


ans = 10 ** 10
W.insert(0,0)

for k in range(n):
    can = W[k] + (E[-1]-E[k])
    if can <= ans:
        ans = can

print(ans)

Solution (Python):


n=int(input())
s=list(input())
ans=10**10
wl=0
el=0
wr=s.count('W')
er=s.count('E')
for i in range(n):
    if wl+er<ans:
        ans=wl+er
    if s[i]=='E':
        el+=1
        er-=1
    else:
        wl+=1
        wr-=1
    if wl+er<ans:
        ans=wl+er
print(ans)

Solution (Python):
import collections
n = int(input())
s = input()
dic = dict(collections.Counter(s))

if len(dic)==1:
  print(0)
  exit()
left_w = 0
right_e = dic["E"]


ans = n
for i in range(n):
  if s[i] == "W":
    ans = min(ans,left_w+right_e)
    left_w += 1
  elif s[i] == "E":
    ans = min(ans,left_w+right_e)
    right_e -= 1


left_e = 0
right_w = dic["W"]

for i in range(n-1,-1,-1):
  if s[i] == "E":
    ans = min(ans,left_e+right_w)
    left_e += 1
  elif s[i] == "W":
    ans = min(ans,left_e + right_w)
    right_w -= 1

print(ans)

Solution (Python):
import collections
n = int(input())
s = input()
dic = dict(collections.Counter(s))

if len(dic)==1:
  print(0)
  exit()
left_w = 0
right_e = dic["E"]
cnt = [[] for _ in range(n)]



ans = n
for i in range(n):
  if s[i] == "W":
    cnt[i] = [left_w,right_e]
    ans = min(ans,left_w+right_e)
    left_w += 1
  elif s[i] == "E":
    cnt[i] = [left_w,right_e]
    ans = min(ans,left_w+right_e)
    right_e -= 1


left_e = 0
right_w = dic["W"]

for i in range(n-1,-1,-1):
  if s[i] == "E":
    ans = min(ans,left_e+right_w)
    left_e += 1
  elif s[i] == "W":
    ans = min(ans,left_e + right_w)
    right_w -= 1

print(ans)

Solution (Python):
n=int(input())
a=input()
import collections 
e=a.count("E")
w=0
mini=n
for i in range(n):
    if a[i]=="E":
        e-=1
    if i!=0 and a[i-1]=="W":
        w+=1
    mini=min(mini,e+w)
print(mini)

Solution (Python):
from collections import Counter
N = int(input())
S = list(input())
S_C = Counter(S)
left_num = S_C["E"]
if S[0] == "E":
    left_num -= 1
right_num = 0
ans = left_num + right_num
for i in range(1,N):
    
    if S[i] == "E":
        left_num -= 1
    if S[i-1] == "W":
        right_num += 1
    tmp_ans = left_num + right_num
    if ans > tmp_ans:
        ans = tmp_ans
print(ans)

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

E=[0]*N
W=[0]*N
c=0
for i in range(N):
  W[i] =c
  if S[i]=='W':
    c +=1
c=0
for j in range(N):
  E[-1-j]=c
  if S[-1-j]=='E':
    c +=1
E=[E[0]]+E+[0]
W=[0]+W+[W[-1]]
answer=float('inf')
for i in range(N+2):
  answer=min(answer,E[i]+W[i])
print(answer)

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

Solution (Python):
N = int(input())
S = list(input())
ws = [0]*N
es = [0]*N

for i in range(N-1):
    if S[i] == 'W':
        ws[i+1] += ws[i]+1
    else:
        ws[i+1] = ws[i]
for i in range(N-1, 0, -1):
    if S[i] == 'E':
        es[i-1] += es[i]+1
    else:
        es[i-1] = es[i]
ans = []
for w, e in zip(ws, es):
    ans.append(w+e)
print(min(ans))

Solution (Python):
n = int(input())
s = input()
wcount = 0
wecount = 0
prev = ""
for c in s:
    if c == "W":
        if prev == "E": wcount = 0
        wcount += 1
    if wcount and c == "E":
        wcount -= 1
        wecount += 1
print(wecount)

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

count = [float('inf')] * n


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


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

print(min(count))

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

w_to_e = [0]*n
e_to_w = [0]*n

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

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

change = [w_to_e[i] + e_to_w[i] for i in range(n)]

print(min(change))

Solution (Python):

def examC():
    N = I()
    S = SI()
    cumE = [0]*(N+1)
    for i in range(N):
        cur = 0
        if S[i]=="E":
            cur = 1
        cumE[i+1] = cumE[i] + cur
    ans = N-1
    for i in range(1,N+1):
        cur = (i-1-cumE[i-1]) + (cumE[N]-cumE[i])
        ans = min(ans,cur)
    print(ans)



import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examC()

Solution (Python):

N = int(input())
S = list(str(input()))
look_east = [0]
E = 0
W = 0
for s in S:
    if s == "E":
        look_east.append(look_east[-1]+1)
        E+=1
    else:
        look_east.append(look_east[-1])
        W+=1

ans = N
for i in range(N):
    we = look_east[i]
    if S[i] == "E":
        change = i-we + E-(we+1)
    else:
        change = i-we + E-(we)
        
    ans = min(ans,change)
        
print(ans)

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

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

w, e = 0, S.count('E')
ans = n

for s in S:
    if s == 'W':
        ans = min(ans, (w+e))
        w += 1
    else:
        e -= 1
        ans = min(ans, (w+e))

print(ans)

Solution (Python):
N = int(input())
str_to_int = {"W":0, "E":1}
input_ = input()
row = [str_to_int[input_[n]] for n in range(N)]

min_change_count = N+2

west_cum_sum = 0
east_cum_sum = 0
west_cum_sums = []
east_cum_sums = []
for n in range(N):
    if row[n] == 0:
        west_cum_sum += 1
    if row[N-n-1] == 1:
        east_cum_sum += 1
    west_cum_sums.append(west_cum_sum)
    east_cum_sums.append(east_cum_sum)
    
east_cum_sums = east_cum_sums[::-1]
    
min_count = min(east_cum_sums[1], west_cum_sums[N-2])
for n in range(1, N-1):
    count = west_cum_sums[n-1] + east_cum_sums[n+1]
    if count < min_count:
        min_count = count
        
print(min_count)

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

e=[1 if c=='E' else 0 for c in s]
w=[1 if c=='W' else 0 for c in s]

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

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

print(m)

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

ans = float('inf')

W = {'W':0, 'E':0}
n = S.count('E')
E = {'W':N-n, 'E':n}

for s in S:
  E[s] -= 1

  ans = min(ans, E['E'] + W['W'])

  W[s] += 1

print(ans)

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

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

c = Counter(S)
if S[0] == "E":
    change = c["E"] - 1
else:
    change = c["E"]
min_change = change
for i in range(1, N):
    mae = S[i - 1]
    if S[i] == "E":
        change -= 1
    if mae == "W":
        change += 1
    min_change = min(min_change, change)

print(min_change)

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

w = [0]
e = []
w_tmp = 0
e_tmp = 0
for s in sl:
    if s == "W":
        w_tmp += 1
    else:
        e_tmp += 1
    w.append(w_tmp)
    e.append(e_tmp)

ans = float("inf")
for i in range(n):
    tmp = w[i] + e[-1] - e[i]
    if ans > tmp:
        ans = tmp

print(ans)

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

Wlist=[0]*(N+1)
Elist=[0]*(N+1)
for i in range(N):
    Wlist[i+1]=Wlist[i]
    if people[i]=='W':
        Wlist[i+1]+=1
    Elist[i+1]=Elist[i]
    if people[i]=='E':
        Elist[i+1]+=1




List=[]
for i in range(N):
    List.append(Wlist[i]+(Elist[N]-Elist[i+1]))

print(min(List))