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

countE = [0]*(N+1)
countW = [0]*(N+1)

for i in range(N):
    countW[i + 1] = countW[i]
    if S[i] == 'W':
        countW[i+1] += 1

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

minx = N+1
for i in range(N):
    newv = countW[i] + countE[i]
    if minx > newv:
        minx = newv

print(minx)

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

E = [0]*(N+1)
W = [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

min = N

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

Solution (Python):
n=int(input())
s=list(input())
e=s.count("E")
w=n-e
ans=e
nume=0
numw=0
for i in range(n):
    if s[i]=="E":
        nume+=1
    else:
        numw+=1
    ans=min(ans,numw+e-nume)
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

i=1
while i<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]
    i+=1

E=[0]+E
W=[0]+W

ans=N
j=1
while j<=N:
    count=W[j-1]+(E[N]-E[j])
    ans=min(ans,count)
    j+=1

print(ans)

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

Solution (Python):
n = int(input())
s = input()
l = [0]
ans = 10**6
for i,si in enumerate(s):
    l.append(l[i]+(si=="W"))
for i in range(n):
    ans = min(ans,l[i]+(n-1-i)-(l[-1]-l[i+1]))
    
print(ans)

Solution (Python):
def main():
    n = int(input())
    ss = list(input())

    ans = float("inf")
    ans = [0] * n
    cum_sum_w = [0] * (n+1)
    cum_sum_e = [0] * (n+1)
    for i in range(n):
        if ss[i] == 'W':
            add = 1
        else:
            add = 0
        cum_sum_w[i+1] = cum_sum_w[i] + add
    for i in range(n):
        if ss[i] == 'E':
            add = 1
        else:
            add = 0
        cum_sum_e[i+1] = cum_sum_e[i] + add
    for i in range(n):
        ans[i] = cum_sum_w[i] + (cum_sum_e[n] - cum_sum_e[i+1])
    print(min(ans))


if __name__ == "__main__":
    main()

Solution (Python):


import sys

DEBUG = False

def read(t):
    return t(sys.stdin.readline().rstrip())


def read_list(t, sep = " "):
    return [t(s) for s in sys.stdin.readline().rstrip().split(sep)]


def dprint(*args, **kwargs):
    if DEBUG:
        print(*args, **kwargs)
    return


def main():
    n = read(int)
    s = read(str)

    sum_east = [0] * (n + 1)
    sum_west = [0] * (n + 1)
    for i in range(1, n + 1):
        sum_east[i] = (1 if s[i - 1] == "E" else 0) + sum_east[i - 1]
        sum_west[i] = i - sum_east[i]
    dprint(repr(sum_east))
    dprint(repr(sum_west))
    
    ans = 1_000_000
    for i in range(n):
        turn_from_west = sum_west[i]
        turn_from_east = sum_east[n] - sum_east[i + 1]
        if ans > turn_from_east + turn_from_west:
            ans = turn_from_east + turn_from_west
    
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()


def main():
    N = int(input())
    S = input()
    east = [0] * (N + 1)
    west = [0] * (N + 1)
    for i in range(1, N + 1):
        if S[i - 1] == "W":
            west[i] = west[i - 1] + 1
        else:
            west[i] = west[i - 1]
    for i in range(N - 1, -1, -1):
        if S[i] == "E":
            east[i] = east[i + 1] + 1
        else:
            east[i] = east[i + 1]

    ans = 10**18
    for n in range(N):
        ans = min(ans, west[n] + east[n + 1])
    print(ans)



if __name__ == "__main__":
    main()

Solution (Python):

import numpy as np
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect


def zz():
    return list(map(int, input().split()))


def z():
    return int(input())


def S():
    return input()


def C(line):
    return [input() for _ in range(line)]


N = z()
S = S()
wester = [0] * N
west_sum = 0
easter = [0] * N
sum_ = [0]*N
east_sum = S.count('E')
for i, s in enumerate(S):
    wester[i] = west_sum
    if (s == 'W'):
        west_sum += 1
    else:
        east_sum -= 1
    easter[i] = east_sum
    sum_[i] = wester[i] + easter[i]


print(min(sum_))

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

Solution (Python):
n = int(input())
s = input()
wcnt = [0] * (n + 2)
ecnt = [0] * (n + 2)
for i in range(n):
  if s[i] == 'W':
    wcnt[i + 1] = wcnt[i] + 1
  else:
    wcnt[i + 1] = wcnt[i]
for i in range(n):
  if s[-(i + 1)] == 'E':
    ecnt[-(i + 2)] = ecnt[-(i + 1)] + 1
  else:
    ecnt[-(i + 2)] = ecnt[-(i + 1)]


lst = [wcnt[i - 1] + ecnt[i + 1] for i in range(1, n + 1)]

print(min(lst))

Solution (Python):
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(map(int,input().split()))
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
INF = 10**9
mod = 10**9+7

N = i()
S = list(s())
w = [0]*N
e = [0]*N
for i in range(N):
    if S[i] == 'W':
        w[i] = 1
    if S[i] == 'E':
        e[i] = 1
w = [0]+list(itertools.accumulate(w))
e = [0]+list(itertools.accumulate(e[::-1]))
e.reverse()
ans = 10**9
for i in range(N+1):
    ans = min(ans,w[i]+e[i])
print(ans)

Solution (Python):
def mi():
    return map(int, input().split())

def ii():
    return int(input())

def main():
    N = ii()
    S = input()
    dp = [0]*N
    dp[0] = S[1:].count('W')
    for i in range(1, N):
        dp[i] = dp[i-1]
        if S[i-1] == 'E':
            dp[i] += 1
        if S[i] == 'W':
            dp[i] -= 1
    print(N-1-max(dp)) 
    


if __name__ == '__main__':
    main()

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

l = [0]*(n+1)
r = [0]*(n+1)

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

for i in range(n-1, -1, -1):
    r[i] += r[i+1]
    if s[i] == "W":
        r[i] += 1
        
ans = 1001001001

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

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

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

Solution (Python):
N=int(input())
S=input()
ap=[0]*(N+1)
bp=[0]*(N+1)
a=0
b=0
for i in range(N):
  if S[i]=='W':
    a+=1
  else:
    b+=1
  ap[i+1]=a
  bp[i+1]=b
ans=10**9
for j in range(1,N+1):
  ans=min(ap[j-1]+bp[N]-bp[j],ans)
print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
s = readline().decode().rstrip()

cost = [0]*n
cost[0] = s[1::].count('E')
if s[0] == 'W':
    cost[1] += 1

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

print(min(cost))

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

ecount=s.count("E")
wcount=0
mn=n
for i in range(n):
    if s[i]=="E":
        ecount-=1
        count=ecount+wcount
        if mn>count:
            mn=count
    else:
        count=ecount+wcount
        if mn>count:
            mn=count
        wcount+=1

print(mn)

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


temp = 0
for j in range(1, N):
    temp += (S[j] == 'E')
    ans = temp

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

print(ans)

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

Solution (Python):

import sys


def solve(N: int, S: str):
    E = [0] * (N+1)
    W = [0] * (N+1)
    for i, s in enumerate(S):
        E[i+1] = E[i] + (1 if s == 'W' else 0)
    for i, s in enumerate(reversed(S)):
        W[N-i-1] = W[N-i] + (1 if s == 'E' else 0)
    return min(e+w for e, w in zip(E, W))



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    S = next(tokens)  
    print(solve(N, S))

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    
    main()

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

ppl = list(input())

buff = []
w = 0
e = ppl[1:].count('E')
for i in range(N):
    if i > 0:
        w += (ppl[i-1] == 'W' and 1 or 0)
    if i > 0:
        e -= (ppl[i] == 'E' and 1 or 0)
    n = w+e
    buff.append(n)

print(min(buff))

Solution (Python):
n = int(input())
S = list(input())
left = []
right = []
l = 0
r = 0
wa = []
for i in range(n):
  if i == 0:
    l += 0
    r += 0
    left.append(l)
    right.append(r)
  else:
    if S[i-1] == "W":
      l += 1
    left.append(l)
    if S[n-i] == "E":
      r += 1
    right.append(r)
for j in range(n):
  wa.append(left[j] + right[n-1-j])
print(min(wa))

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

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

ans = 10 ** 10
for i in range(N):
  if S[i] == "E":
    count -= 1
  if i != 0:
    if S[i - 1] == "W":
      count += 1
  ans = min(ans, count)
  
print(ans)

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

S = input()

min_num = N+1

W = []
E = []

w_cnt = 0
e_cnt = 0

for i in range(N):
    if S[i] == 'W':
        w_cnt += 1
    W.append(w_cnt)

for i in range(N-1,0,-1):
    min_num = min(min_num,W[i-1]+e_cnt)
    if S[i] == 'E':
        e_cnt += 1

print(min(min_num,e_cnt))

Solution (Python):
import numpy as np
N = int(input())
S = "d"+input()+"d"


leftwest = np.array([0]*(N+2))
righteast = np.array([0]*(N+2))

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





print(min(leftwest[1:N+1]+righteast[1:N+1]))

Solution (Python):
n=int(input())
s=input()
a=[]
now=0
for i in s:
    a.append(now)
    if i=="W":
        now+=1
    
now=0
for i in range(n):
    a[n-i-1]+=now
    if s[n-i-1]=="E":
        now+=1
    
a=sorted(a)
print(a[0])

Solution (Python):
N=int(input())
S=input()
nagasa=len(S)
D=[]
goukei=0
for i in range(nagasa-1):
    if S[i+1]=="E":
        goukei+=1
    else:
        goukei+=0

     
D.append(goukei)
for i in range(nagasa-1):
    if S[i]=="W":
        goukei+=1
    else:
        goukei+=0
    if S[i+1]== "W":
        goukei+=0
    else:
        goukei-=1
    
    D.append(goukei)

print(min(D))

Solution (Python):

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


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


out=hidari+migi

ld=1  

while ld<N:
    if S[ld-1]=='W' and S[ld]=='E':
        hidari+=1
        migi-=1
    elif S[ld-1]=='W' and S[ld]=='W':
        hidari+=1
    elif S[ld-1]=='E' and S[ld]=='E':
        
        migi-=1
    
    out = min(out,hidari+migi)
    ld+=1

print(out)

Solution (Python):
N = int(input())
S = list(input())
E = [0]*N 
W = [0]*N 

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

out = [0]*N
for i in range(N):
    out[i] = (N-1) - (W[i]+E[i])
print(min(out))

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):
n = int(input())
s = list(input())
left = [0] * n
right = [0] * n
for i in range(n):
    if s[i] == 'W':
        if i == 0:
            left[0] = 1
        else:
            left[i] = left[i-1] + 1
    elif i != 0:
        left[i] = left[i-1]
for i in range(n-1, -1, -1):
    if s[i] == 'E':
        if i == n - 1:
            right[n-1] = 1
        else:
            right[i] = right[i+1] + 1
    elif i != n - 1:
        right[i] = right[i+1]
ans = right[1]
for i in range(1, n-1):
    ans = min(ans, left[i-1] + right[i+1])
ans = min(ans, left[n-2])
print(ans)

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]
    if S[i] == 'E':
        e[i+1] = e[i] + 1
        w[i+1] = w[i]
ans = 10 ** 6
for i in range(N):
    ans = min(ans, w[i] + e[N] - e[i + 1])
print(ans)

Solution (Python):
def main():
    N = int(input())
    s = input()
    ans = 0
    temp = 0

    for i in range(1,N):
        if s[i] == 'E':
            ans += 1

    temp = ans
    

    for j in range(1,N):
        if s[j-1] == 'W':
            temp += 1

        if s[j] == 'E':
            temp -=1


        if temp < ans:
            ans = temp
        

    return ans

print(main())

Solution (Python):
def solve(S,N):
    E,W = [0 for _ in range(N)],[0 for _ in range(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]
        W[i] += W[i-1]

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

    return ans

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

    print(solve(S,N))

if __name__ == "__main__":
    main()

Solution (Python):
import sys, os, math, bisect, itertools, collections, heapq, queue
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict



sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)


MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    S = iss()
    NUM = [0] * (N+1)

    for n in range(N):
        NUM[n+1] += NUM[n]
        if S[n] == 'E':
            NUM[n+1] += 1

    ret = MAX
    for n in range(N+1):
        
        
        
        
        ret = min(ret, n - NUM[n] + (NUM[-1] - NUM[n]))
    print(ret)


if __name__ == '__main__':
    main()

Solution (Python):
import sys, os, math, bisect, itertools, collections, heapq, queue
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict



sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)


MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    S = iss()
    NUM = [0] * (N+1)

    for n in range(N):
        NUM[n+1] += NUM[n]
        if S[n] == 'W':
            NUM[n+1] += 1

    ret = MAX
    for n in range(N+1):
        ret = min(ret, NUM[n] + N - n - (NUM[-1] - NUM[n]))
    print(ret)


if __name__ == '__main__':
    main()

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

L = [0] * n
R = [0] * n


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


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

res = 1 << 29
for i in range(n):
    res = min(res, L[i] + R[i])

print(res)

Solution (Python):
n = int(input())
s = input()
e_l, e_sum = [], 0
w_l, w_sum = [], 0
for i in range(n):
    if s[i] == 'E':
        e_sum += 1
    else:
        w_sum += 1
    e_l.append(e_sum)
    w_l.append(w_sum)
for i in range(n):
    if i == 0:
        ans = e_l[n-1] - e_l[0]
    elif i == n:
        ans = max(ans, w_l[n-2])
    else:
        ans = min(ans, e_l[n-1] - e_l[i] + w_l[i-1])
print(ans)

Solution (Python):
n = int(input())
s = input()
e_l, e_sum = [], 0
w_l, w_sum = [], 0
for i in range(n):
    if s[i] == 'E':
        e_sum += 1
    else:
        w_sum += 1
    e_l.append(e_sum)
    w_l.append(w_sum)
for i in range(n):
    if i == 0:
        ans = e_l[n-1] - e_l[0]
    elif i == n:
        ans = max(ans, w_l[n-2])
    else:
        ans = min(ans, e_l[n-1] - e_l[i] + w_l[i-1])
print(ans)

Solution (Python):
import collections
n = int(input())
s = input()
l = s.count("W")
r = s.count("E")
ans = 0
keep = r
minn = keep
if s[0] == "E":
    ans = 1
for i in range(1, n):
    if s[i - 1:i + 1] == "WW":
        keep += 1
    elif s[i - 1:i + 1] == "WE":
        keep += 0
    elif s[i - 1:i + 1] == "EW":
        keep += 0
    elif s[i - 1:i + 1] == "EE":
        keep -= 1
    if minn > keep:
        minn = keep
print([minn, minn - 1][ans])

Solution (Python):



import os
import sys
from itertools import accumulate







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

    W = list()
    E = list()
    for i in range(N):
        flag = S[i] == "W"
        W.append(int(flag))
        E.append(int(not flag))

    
    W_l = accumulate([0] + W[:-1])
    E_r = accumulate([0] + list(reversed(E[1:N])))

    for w, e in zip(W_l, reversed(list(E_r))):
        ans = min(ans, w + e)

    print(ans)
    return


if __name__ == '__main__':
    main()

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

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

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

res = 0
for i in range(n):
    res = max(res, cnt_e[i] + cnt_w[i+1])

print(n - res - 1)

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

ans = s.count("E")

tmp = ans

for i in s:
    tmp += (-1 if i == "E" else 1)
    ans = min(ans, tmp)

print(ans)

Solution (Python):
n,s=open(0);a=t=s.count('E')
for i in s:t+=(-1 if i=='E' else 1);a=min(a,t)
print(a)

Solution (Python):

"""
Created on Fri Jun 12 21:52:40 2020

@author: naoki
"""

N = int(input()) 

S = input() 
    
change = [0 for i in range(N)] 
change[0] = S.count("E",1,N)
for i in range(1,N):
        if S[i-1] == "W" and S[i] == "E":
            change[i] = change[i-1]
        elif S[i-1] == "W" and S[i] != "E": 
            change[i] = change[i-1] + 1
        elif S[i-1] != "W" and S[i] == "E":
            change[i] = change[i-1] - 1
        else:
            change[i] = change[i-1]
change.sort()
print(change[0])

Solution (Python):
n = int(input())
s = input()
e = s.count("E")
w = s.count("W")
min_ = 10**9
l = [[0, 0] for i in range(n)]
if s[0] == "E":
  l[0] = [0, e-1]
else:
  l[0] = [0, e]
min_ = min(min_, sum(l[0]))
for i in range(1, n):
  if s[i-1] == "E":
    if s[i] == "E":
      l[i] = [l[i-1][0], l[i-1][1]-1]
    else:
      l[i] = [l[i-1][0], l[i-1][1]]
  else:
    if s[i] == "E":
      l[i] = [l[i-1][0]+1, l[i-1][1]-1]
    else:
      l[i] = [l[i-1][0]+1, l[i-1][1]]
  min_ = min(min_, sum(l[i]))
print(min_)

Solution (Python):
n = int(input())
s = input()
e = [0]
w = [0]
for i in range(len(s)):
    if s[i] == 'E':
        e.append(e[i]+1)
        w.append(w[i]+0)
    if s[i] == 'W':
        e.append(e[i]+0)
        w.append(w[i]+1)

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

Solution (Python):
n = int(input())
s = input()
w = [0]*(n + 2)
e = [0]*(n + 2)
ans = 100000000

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

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

print(ans)

Solution (Python):
def main():
    n = int(input())
    s = input()
    
    a = [0] * (n)
    b = [0] * (n)
    for i in range(1, n):
        if s[i - 1] == 'W':
            a[i] = a[i - 1] + 1
        else:
            a[i] = a[i - 1]
        if s[n- i] == 'E':
            b[n - i - 1] = b[n - i ] + 1
        else:
            b[n- i - 1] = b[n - i ]
    ans = 10**9
    for aa, bb in zip(a, b):
        ans = min(ans, aa++bb)
    print(ans) 
        
if __name__ == '__main__':
    main()

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
def main():
    N = int(readline())
    S = readline().rstrip().decode()
    
    
    csw = [0]
    cse = [0]
    for i in range(N):
        if S[i] == 'W':
            csw.append(csw[i]+1)
            cse.append(cse[i])
        else:
            csw.append(csw[i])
            cse.append(cse[i]+1)
    ans = int(1e6)
    for i in range(N):
        
        
        ans = min(cse[N]-cse[i+1] + csw[i] - csw[0], ans)
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n = ni()
s = [_ for _ in input()]

e = [0]
w = [0]

for si in s:
  if si == 'E':
    e.append(e[-1]+1)
    w.append(w[-1])
  if si == 'W':
    w.append(w[-1]+1)
    e.append(e[-1])




mn = 10**7

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

print(mn)

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


W = [[] for _ in range(N)]
E = [[] for _ in range(N)]

prev_w = 0
prev_e = 0
for i, s in enumerate(S):
    if s == 'W':
        W[i] = prev_w + 1
    else:
        W[i] = prev_w
    prev_w = W[i]

for i, s in enumerate(S[::-1]):
    if s == 'E':
        E[-i] = prev_e + 1
    else:
        E[-i] = prev_e
    prev_e = E[-i]
        
ans = len(S)
for i in range(N):
    if i == 0:
        cost = E[i+1]
    elif i == N-1:
        cost = W[i-1]
    else:
        cost = W[i-1] + E[i+1]
    ans = min([cost, ans])
print(ans)

Solution (Python):
N = int(input())
S = list(input())
e = S.count('E')
left = 0
right = e - (1 if S[0] == 'E' else 0)
ans = left + right

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

Solution (Python):
N=int(input())
S=input()
cnt=S.count("E")
m=cnt
for i in S:
    if i=="E":
        cnt-=1
    else:
        cnt+=1
    m=min(m,cnt)
print(m)

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

sum_W = [0] * (N+1)
sum_E = [0] * (N+1)

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

answers = []
for i in range(N):
    answer = sum_W[i] + (sum_E[N] - sum_E[i+1])
    answers.append(answer)

print(min(answers))

Solution (Python):
N=int(input())
S=input()
cnt=S.count("E")
m=cnt
for i in S:
    if i=="E":
        cnt-=1
    else:
        cnt+=1
    m=min(m,cnt)
print(m)

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




s=list(input())

wtotal=s.count("W")
etotal=s.count("E")
wl=[];el=[]
wcount=0
ecount=0
for i in range(n):
    if s[i]=="E":
        ecount+=1
    el.append(ecount)
for i in range(n-1,-1,-1):
    if s[i]=="W":
        wcount+=1
    wl.append(wcount)

wl.reverse()
mn=etotal-el[0]
for i in range(n):
    mn=min(mn,wtotal-wl[i]+etotal-el[i])

print(mn)

Solution (Python):

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

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

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

total_list = []
for i in range(N):
    to_E = count_W[i]
    to_W = count_E[i]
    total_list.append(to_E + to_W)

print(min(total_list))

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

total = 0
 
total_list = []


count_w_from_left = [0]
pre_count = 0
for i in range(1, N):
  if S[i-1:i] == 'W':
    pre_count += 1
  count_w_from_left.append(pre_count)

count_e_from_right = []
for i in range(N):
  count_e_from_right.append(0)
pre_count = 0
for i in range(N - 2, -1, -1):
  if S[i+1:i+2] == 'E':
    pre_count += 1
  count_e_from_right[i] = pre_count


for i in range(N):
    to_E = count_w_from_left[i]
    to_W = count_e_from_right[i]
    total_list.append(to_E + to_W)
 
print(min(total_list))

Solution (Python):
import bisect
n = int(input())
s = input()
elist = []
wlist = []
for i in range(n):
	if s[i] == "E":
		elist.append(i)
	elif s[i] == "W":
		wlist.append(i)
ans = 10**18
lenelist = len(elist)
"""
西にいるWを数える
東にいるEを数える
"""
for i in range(n):
	tmp = lenelist-bisect.bisect_right(elist, i)+bisect.bisect_left(wlist, i)
	ans = min(tmp, ans)
print(ans)

Solution (Python):
N = int(input())
S = input()
table_e = []
count_e = 0
table_w = []
count_w = 0
for s in S:
    if s == 'E':
        count_e += 1
    else:
        count_w += 1
    table_e.append(count_e)
    table_w.append(count_w)
ans = float('inf')
for i in range(N):
    tmp = table_w[i - 1] if i else 0
    tmp += count_e - table_e[i]
    ans = min(ans, tmp)
print(ans)

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

from collections import defaultdict
l = defaultdict(int)
r = defaultdict(int)
for si in s:
    r[si] += 1
r[s[0]] -= 1

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

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

Solution (Python):
N = int(input())
S = list(input())
e = [0]*(N+1)
w = [0]*(N+1)
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:
        e[i] += e[i-1]
        w[i] += w[i-1] + 1
        
ans = float('inf')
for i in range(N):
    tmp = w[i-1] + e[N-1] - e[i]
    ans = min(ans, tmp)
print(ans)

Solution (Python):
import sys


if __name__ == "__main__":
    n = int(input())
    s = input()
    sum_w = s.count("W")
    sum_e = s.count("E")
    ans = 10**9
    w,e = 0,0
    for i in range(n):
        if s[i] == "W":
            sum_w-=1
        else:
            sum_e-=1
        chenge = w + sum_e
        if s[i] == "W":
            w += 1
        else:
            e += 1
        ans = min(ans,chenge)
    print(ans)

Solution (Python):
n = int(input())
s = input()
countw = 0
counte = 0
ans = [0]*n
for i in range(1, n):
  if s[i-1] == "W":
    countw += 1
  if i != n:
    ans[i] += countw
  if s[-i] == "E":
    counte += 1
  ans[n-i-1] += counte

print(min(ans))

Solution (Python):
n = int(input())
S = input()
re = 0
for s in S:
    if s == 'E':
        re += 1
lw = 0
c = 3*10**5
for i in range(n):
    if S[i]  == 'E':
        re -= 1
    c = min(c, re+lw)

    if S[i] == 'W':
        lw += 1
print(c)

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

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

ans = 0

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

tmp = ans

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


print(ans)

Solution (Python):
n = int(input())
s = str(input())
s = s.replace("W","1")
s = s.replace("E","0")
fl = sum(list(map(int,s)))
k = 0
p = 0
for i in range(n):
    if i == 0:
        p = fl - int(s[0])
        k = p
    elif i == n-1:
        p = n-1-fl+int(s[n-1])
        if p > k:
            k = p
    else:
        if (s[i-1] == "1" and s[i] == "1"):
            p -= 1
        elif (s[i-1] == "1" and s[i] == "0"):
            continue
        elif (s[i-1] == "0" and s[i] == "1"):
            continue
        else:
            p += 1
            if p > k:
                k = p
print(n-k-1)

Solution (Python):
import sys

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

right = S[1:].count("E")
left = 0
ans = [0]*N
ans[0] = right + left
for i in range(1, N):
    if S[i] == "E":
        right = right - 1
    if S[i-1] == "W":
        left = left + 1
    ans[i] = right+left

print(min(ans))