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









import os
import sys




def main():
    N = int(input())
    S = input()
    RW_cnt = S.count("W")
    RE_cnt = S.count("E")
    LW_cnt = 0
    LE_cnt = 0
    if S[0] == "W":
        RW_cnt -= 1
        LW_cnt = 1
    else:
        RE_cnt -= 1
        LE_cnt = 1
    ans = RE_cnt

    for i in range(1, N):
        if i == N-1:
            cnt = LW_cnt
        else:
            cnt = LW_cnt + RE_cnt
        if cnt < ans:
            ans = cnt
        if S[i] == "E":
            LE_cnt += 1
            RE_cnt -= 1
        else:
            LW_cnt += 1
            RW_cnt -= 1

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
S = input()
cnt = 0
for i in range(1, N):
	if S[i] == 'W':
		cnt += 1
max = cnt
for i in range(1, N):
	if S[i-1] == 'E':
		cnt += 1
	if S[i] == 'W':
		cnt -= 1
	if max < cnt:
		max = cnt
print(N - max - 1)

Solution (Python):





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

re = S.count("E")
rw = N - re
le = 0
lw = 0

ans = 10**9

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

print(ans)

Solution (Python):



import sys

from collections import Counter

N = int(input())

S = input()

e = [0]
w = [0]

for s in S:
    if s == "W":
        w += [w[-1] + 1]
        e += [e[-1]]
    elif s == "E":
        w += [w[-1]]
        e += [e[-1] + 1]

ans = 3 * (10 ** 5) + 1

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

print(ans)

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

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

w_sum,e_sum=[0],[0]

for x in S:
  if x=="W":
    w_sum.append(w_sum[-1]+1)
  else:
    w_sum.append(w_sum[-1])
w_sum.append(0)

RS=S[::-1]
for x in RS:
  if x=="E":
    e_sum.append(e_sum[-1]+1)
  else:
    e_sum.append(e_sum[-1])
e_sum.append(0)
e_sum.reverse()

ans=float("inf")
for i in range(1,N+1):
  ans=min(ans,w_sum[i-1]+e_sum[i+1])

print(ans)

Solution (Python):
import collections

def main():
    N = int(input())
    S = list(input())
    Scount = collections.Counter(S)
    Wcount = Scount['W']
    Ecount = Scount['E']

    if S[0] == "W":
        Wcount -= 1
    else:
        Ecount -= 1
    ans = Ecount
    L = [0, 0]; R = [Wcount, Ecount]
    for i in range(N-1):
        sl = S[i]
        s = S[i+1]
        if sl == 'W':
            L[0] += 1
        else:
            L[1] += 1
        if s == 'W':
            R[0] -= 1
        else:
            R[1] -= 1
        cnt = L[0] + R[1]
        ans = min(ans, cnt)
    print(ans)

if __name__ == "__main__":
    main()

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

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

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

print(ans)

Solution (Python):
from collections import Counter
N = int(input())
S = input()
conS = Counter(S)
NuE = conS['E']
NuW = conS['W']
def Change(x,lefW,rigE):
    if x == 'W':
        return lefW + rigE -1
    else:
        return lefW + rigE
lefW = 0
rigE = NuE
Ans = N
for i in S:
    if i == 'E':
        rigE -= 1
    else:
        lefW += 1
    Ans = min(Ans, Change(i,lefW,rigE))
print(Ans)

Solution (Python):
if __name__ == "__main__":
    N = int(input())
    E = [0]*(N+1)
    W = [0]*(N+1)
    for i, s in enumerate(input()):
        if s == "E":
            E[i+1] = 1
        elif s == "W":
            W[i+1] = 1
        E[i+1] += E[i]
        W[i+1] += W[i]
    ans = 3*10**5
    for i in range(N):
        if i == 0:
            ans = min(ans, E[N]-E[1])
            continue
        if i == N-1:
            ans = min(ans, W[i])
            continue
        ans = min(ans, E[N]-E[i+1] + W[i])
    print(ans)

Solution (Python):
def main():
    N = int(input())
    S = input()
    ans = tmp = S.count('E')
    prev = ''
    for c in S:
        if c == 'E':
            tmp -= 1
        if prev == 'W':
            tmp += 1
        ans = min(ans,tmp)
        prev = c
    print(ans)
main()

Solution (Python):
import sys

input = sys.stdin.readline
N = int(input())
peoples = list(input().strip())

E = 0
W = 0
for p in peoples:
    if p == "E":
        E += 1
    else:
        W += 1

ans = float("inf")
tmp = E
for i in range(N):
    if peoples[i] == "E":
        tmp -= 1
    if 0 < i and peoples[i-1] == "W":
        tmp += 1
    ans = min(ans, tmp)
print(ans)

Solution (Python):
N = int(input())
S = input()
e = S.count('E')
X = [0]*(N+1)
X[0] = e
Y = [0]*(N+1)
Y[N] = N-e
for i in range(1, N+1):
    X[i] = X[i-1]-(S[i-1] == 'E')
for i in range(N, 0, -1):
    Y[i-1] = Y[i]-(S[i-1] == 'W')
ans = [X[i+1]+Y[i] for i in range(N)]


print(min(ans))

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

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

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

ans=10**9
for i in range(N):
  ans=min(ans,LW[i]+RE[i])
print(ans)

Solution (Python):
N = int(input())
S = input()
rwc = S.count('W')
rec = N-rwc
lwc = 0
lec = 0
if S[0] == 'W':
    rwc -= 1
else:
    rec -= 1
mi = rec
for i in range(1,N):
    if S[i] == 'W':
        rwc -= 1
    else:
        rec -= 1
    if S[i-1] == 'W':
        lwc += 1
    else:
        lec += 1

    muku = lwc + rec
    mi = min(mi,muku)
print(mi)

Solution (Python):
N = int(input())
S = list(map(str,input()))
En = S[1:].count("E")
Sn = S[1:].count("W")
res = [En]
cnt = En
for i, s in enumerate(S):
    if i == 0:
        continue
    if s == "E":
        cnt -= 1
    if S[i-1] == "W":
        cnt += 1
    res.append(cnt)
print(min(res))

Solution (Python):
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 6)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")
al = "abcdefghijklmnopqrstuvwxyz"
AL = al.upper()

n = k()
s = v()
e = list(itertools.accumulate([1 if i=="E" else 0 for i in s]))
w = list(itertools.accumulate([1 if i=="W" else 0 for i in s]))

for i in range(n):
    cnt = e[-1]-e[i]+w[i]
    if mod > cnt:
        mod,ans = cnt,i
if ans == 0:
    mod -= 1

print(mod)

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

cnt = 0
min_cnt = 3 * 10**5
for i in range(1, N):
    if S[i] == 'E':
        cnt += 1
min_cnt = cnt


for i in range(1, N):
    if S[i-1:i+1] == "EE":
        cnt -= 1
    elif S[i-1:i+1] == "EW":
        cnt += 0
    elif S[i-1:i+1] == "WE":
        cnt += 0
    elif S[i-1:i+1] == "WW":
        cnt += 1
    min_cnt = min(min_cnt, cnt)
print(min_cnt)

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

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

Solution (Python):
n=int(input())
s=input()
s=s[::-1]
e=0;w=0
for i in range(n):
    e+=(s[i]=="E")
    w+=(s[i]=="W")

w2=w
e2=0
ans=n-1
for i in range(n):
    w2-=(s[i]=="W")
    ans=min(ans,w2+e2)
    e2+=(s[i]=="E")
print(ans)

Solution (Python):
n=int(input())
s=input()
ans=s[1:].count('E') 
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 = input()
E = [0]*(N+1)
W = [0]*(N+1)
for i in range(N):
  W[i+1]+=W[i]
  if S[i] == "W":
    W[i+1]+=1
for i in reversed(range(N)):
  E[i]+=E[i+1]
  if S[i] == "E":
    E[i]+=1
W = W[1:]
E = E[:-1]

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

Solution (Python):

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


lw = [0]*N
cntw = 0

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


re = [0]*N
cnte = 0

for j in range(N-2,-1,-1):
    if S[j+1] == 'E':
        cnte += 1
    re[j] = cnte

ans = 10**10
for i,j in zip(lw,re):
    tot = i+j
    ans = min(tot,ans)
print(ans)

Solution (Python):
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right 
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import *

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def lgcd(l):
    return reduce(gcd,l)
def llcm(l):
    return reduce(lcm,l)
def intput():
    return int(input())
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x):
        self.setdefault(x,0)
        self[x] += 1

N = intput()
S = input()
re = 0
for i in range(N):
    re = re+1 if S[i]=='E' else re
lw = 0
ans = INF
for i in range(N):
    re = re-1 if S[i]=='E' else re
    ans = min(ans,re+lw)
    lw = lw+1 if S[i]=='W' else lw
print(ans)

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

Solution (Python):
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n = I()
    s = S()
    right, left = 0, 0
    ans = n
    for i in s:
        right += 1 if i=="E" else 0

    for i, char in enumerate(s):
        right -= 1 if char=="E" else 0
        cnt = left + right
        ans = min(ans,cnt)
        left += 1 if char=="W" else 0

    print(ans)
main()

Solution (Python):
import collections
n=int(input())
s=list(input())
cnte=s.count('E')
cntw=s.count('W')

ans=n
a=0
b=0
for i in range(n):
    if s[i]=='E':
        a+=1
    elif s[i]=='W':
        b+=1
    c=cnte-a
    d=cntw-b
    tmp=b+c
    if s[i]=='W':
        tmp-=1
    ans=min(ans,tmp)

print(ans)

Solution (Python):
n = int(input())
s = input()
a = [str(c) for c in s]

x = a.count('E')
if a[0] == 'E':
    x -= 1
y = x
for i in range(n-1):
    if a[i] == 'W':
        if a[i+1] == 'W':
            x += 1
        else:
            x = x
    else:
        if a[i+1] == 'W':
            x = x
        else:
            x -= 1
    if y > x:
        y = x
        



print(y)

Solution (Python):


import sys
from collections import Counter



def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    n = input_int()
    S = input()
    count = Counter(S)
    left_w = 0
    left_e = 0
    ans = len(S) + 1
    for i in range(len(S)):
        if S[i] == "E":
            ans = min(ans, left_w + (count["E"] - left_e - 1))
        else:
            ans = min(ans, left_w + (count["E"] - left_e))
        if S[i] == "W":
            left_w += 1
        else:
            left_e += 1
    print(ans)
    return


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
s = input()
tb1 = [0]*n
tb2 = [0]*n
for i in range(1,n):
  if s[i-1]=='E':
    tb1[i] = tb1[i-1]+1
  else:
    tb1[i] = tb1[i-1]
for i in range(n-2,-1,-1):
  if s[i+1]=='W':
    tb2[i] = tb2[i+1]+1
  else:
    tb2[i] = tb2[i+1]    
tb = [k+l for k,l in zip(tb1,tb2)]
print(n-max(tb)-1)

Solution (Python):
n = int(input())
s = list(input())
w, e = [0] * (n + 1), [0] * (n + 1)
ans = float("inf")
for i in range(n):
    if s[i] == "W":
        w[i + 1] = w[i] + 1
    else:
        w[i + 1] = w[i]

    if s[n - i - 1] == "E":
        e[i + 1] = e[i] + 1
    else:
        e[i + 1] = e[i]

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

print(ans - 1)

Solution (Python):
n = int(input())
s = list(input())
w, e = [0] * (n + 1), [0] * (n + 1)
ans = float("inf")
for i in range(n):
    if s[i] == "W":
        w[i + 1] = w[i] + 1
    else:
        w[i + 1] = w[i]

    if s[n - i - 1] == "E":
        e[i + 1] = e[i] + 1
    else:
        e[i + 1] = e[i]

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

print(ans - 1)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 9)

def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')







def solve():
    N = II()
    S = input()

    wr = S.count('W')
    er = S.count('E')
    
    wl = 0
    el = 0
    ans = INF
    for s in S:
        if s == 'W':
            wr -= 1
            ans = min(ans, er+wl)
            wl += 1
        else:
            er -= 1
            ans = min(ans, er+wl)
            el += 1
        
        
    print(ans)

if __name__ == '__main__':
    solve()

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

cum_sum_w = [0] * (N + 1)
cum_sum_e = [0] * (N + 1)


for i in range(N):

    if (S[i] == "W"):
        cum_sum_w[i+1] = cum_sum_w[i] + 1
        cum_sum_e[i+1] = cum_sum_e[i] + 0
    else:
        cum_sum_w[i+1] = cum_sum_w[i] + 0
        cum_sum_e[i+1] = cum_sum_e[i] + 1

ans = 10**100  

for i in range(N):
    tmp_ans = cum_sum_w[i] + (cum_sum_e[N] - cum_sum_e[i + 1])
    ans = min(ans,tmp_ans)
    
print(ans)

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

cum_sum_w = [0] * (N + 1)
cum_sum_e = [0] * (N + 1)


for i in range(N):

    if (S[i] == "W"):
        cum_sum_w[i+1] = cum_sum_w[i] + 1
        cum_sum_e[i+1] = cum_sum_e[i] + 0
    else:
        cum_sum_w[i+1] = cum_sum_w[i] + 0
        cum_sum_e[i+1] = cum_sum_e[i] + 1

ans = 10**100  

for i in range(N):
    tmp_ans = cum_sum_w[i] + (cum_sum_e[N] - cum_sum_e[i + 1])
    ans = min(ans,tmp_ans)
    
print((ans))

Solution (Python):
import sys
input = sys.stdin.readline
ins = lambda: input().rstrip()
ini = lambda: int(input().rstrip())
inm = lambda: map(int, input().split())
inl = lambda: list(map(int, input().split()))
out = lambda x: print('\n'.join(map(str, x)))

n = ini()
s = ins()
a = [0] * n
b = [0] * n
e = 0
w = 0
for i in range(n):
    a[-i-1] = e
    b[i] = w
    if s[i] == "W":
        w += 1
    if s[-i-1] == "E":
        e += 1
ans = 1e9 + 6
for i in range(n):
    ans = min(ans, a[i] + b[i])
print(ans)

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

e=0
w=0

all_e=s.count("E")
ans=float('inf')
for i in range(n):

  ans=min(all_e-e+w,ans)

  if s[i]=="E":
    e+=1
  else:
    w+=1

ans=min(ans,all_e-e+w,ans)
print(ans)

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

ans=float('inf')
cw=0
ce=S.count('E')

for i in S:
 if i=='W':
   ans=min(ans,cw+ce)
   cw+=1
 else:
   ce-=1
   ans=min(ans,cw+ce)
print(ans)

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

import collections
tw=S.count('W')
te=S.count('E')

ans=float('inf')
cw=[0,0]
ce=[tw,te]

for i in S:
    if i=='W':
        ce[0]-=1
        ans=min(ans,cw[0]+ce[1])
        
        cw[0]+=1
    else:
        ce[1]-=1
        ans=min(ans,cw[0]+ce[1])
        
        cw[1]+=1
    
print(ans)

Solution (Python):
N = int(input())
T = input()
c = T[1:].count('E')
m = c
for i in range(N-1):
    c += T[i].count('W') - T[i+1].count('E')
    m = min(m, c)
print(m)

Solution (Python):
N = int(input())
T = input()
c = T[1:].count('E')
m = c
for i in range(N-1):
    c += T[i].count('W')
    c -= T[i+1].count('E')
    m = min(m, c)
print(m)

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

w_series = [0]
e_series = [0]
for c in s:
    if c == "W":
        w_series.append(w_series[-1]+1)
    else:
        w_series.append(w_series[-1])
for c in s[::-1]:
    if c == "E":
        e_series.append(e_series[-1]+1)
    else:
        e_series.append(e_series[-1])
e_series = e_series[::-1]

ans = n
for x in zip(w_series[1:], e_series[:-1]):
    ans = min(sum(x)-1, ans)
print(ans)

Solution (Python):
n=int(input())
s=list(input())
cnt_w = [0]*(n+1)
for i in range(1,n+1):
  cnt_w[i] = cnt_w[i-1] + (s[i-1]=='W')
ans = n + 1
for i in range(n):
  tmp = cnt_w[i] + (n-i-1) - (cnt_w[n] - cnt_w[i+1])
  ans = min(tmp,ans)
print(ans)

Solution (Python):
n=int(input())
s=list(input())
cnt_w = [0]*(n+1)
for i in range(1,n+1):
  cnt_w[i] = cnt_w[i-1] + (s[i-1]=='W')
ans = n + 1
for i in range(n):
  tmp = cnt_w[i] + (n-i-1) - (cnt_w[n] - cnt_w[i+1])
  ans = min(tmp,ans)
print(ans)

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

left = 0
right = s[1:].count('E')

ans = right

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

print(ans)

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

print(min(ans))

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

print(min(ans))

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

Solution (Python):
from itertools import accumulate
n = int(input())
L = list(input())

R = L.count("E")

for i in range(n):
    if L[i] == "W":
        L[i] = 1
    elif L[i] == "E":
        L[i] = 0
liw = list(accumulate(L))

for i in range(n):
    if L[i] == 0:
        L[i] = 1
    elif L[i] == 1:
        L[i] = 0

lie = list(accumulate(L))

for i in range(n):
    lie[i] = R-lie[i]

ans = 10**10
for i in range(n):
    west_num = liw[i]
    east_num = lie[i]
    
    if L[i] == 0:
        west_num =max(west_num -1,0)

    ans = min(ans,west_num+east_num)
print(ans)

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

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

cur = ans

for i in range(0, len(s)):
    if s[i] == "W":
        cur += 1
    else:
        cur -= 1

    if cur < ans:
        ans = cur

print(ans)

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

leftw=[0]*(x+1)
righte=[0]*(x+1)

for i in range(x):
    leftw[i+1]=leftw[i]
    righte[x-i-1]=righte[x-i]

    leftw[i+1]+=1 if s[i]=='W' else 0
    righte[x-i-1]+=1 if s[x-i-1]=='E' else 0
ans=10**6
for leader in range(x):
    ans=min(ans,leftw[leader]+righte[leader+1])

print(ans)

Solution (Python):
N = int(input())
S = input()
e = [0]
w = [0]
for i in range(N):
    if S[i] == "E":
        e.append(e[-1] + 1)
        w.append(w[-1])
    else:
        e.append(e[-1])
        w.append(w[-1] + 1)
ans = 10**9
for i in range(N):
    turn = w[i] + e[-1] - e[i + 1]
    if turn < ans:
        ans = turn
print(ans)

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

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

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

print(ans)

Solution (Python):
n=int(input())
s=input()
s_=[]
for i in range(n):
  if s[i]=='E':
    s_.append(0)
  else:
    s_.append(1)    
rui=[]
rui.append(s_[0])
for i in range(1,n):
  rui.append(rui[-1]+s_[i])
ans=n+1  
for j in range(1,n-2):
  ans=min(ans,rui[j-1]+n-1-j-rui[n-1]+rui[j])
ans_=n-1-rui[n-1]+rui[0]
ans=min(ans,ans_)
ans_=rui[n-1]
ans=min(ans,ans_)
print(ans)

Solution (Python):







INF = pow(10,9)


def solve(n,s):
  cum = [0]*(n+1)
  for i in range(n):
    cum[i+1] = cum[i] + (s[i] == "W")
  res = INF
  for i in range(n):
    w_inLeft = cum[i] - cum[0]
    e_inRight = n - i - 1 - cum[n] + cum[i+1]
    res = min(res,w_inLeft+e_inRight)
  return res



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

if __name__ == '__main__':
  main()

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

p = s.count("E")
q = 0
ans = float("Inf")

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

Solution (Python):

n = int(input())
s = input()
we = s.count('E')
ww = s.count('W')
ew = 0
ee = 0
ans = 3 * (10 ** 5)
for i in range(n):
    if s[i] == 'W':
        ww -= 1
    else:
        we -= 1
    ans = min(ans,we+ew)
    if s[i] == 'W':
        ew += 1
    else:
        ee += 1
print(ans)

Solution (Python):
N=int(input())
S=input()
e=S.count("E")
w=N-e
ans=10**9
for i in range(N):
    left=i
    right=N-1-i
    if i==0:
        left_e=0
        left_w=0
        if S[i]=="E":
            right_e=e-1
            right_w=w
        else:
            right_e=e
            right_w=w-1
    else:
        if S[i-1]=="E":
            left_e+=1
        elif S[i-1]=="W":
            left_w+=1
        if S[i]=="E":
            right_e-=1
        elif S[i]=="W":
            right_w-=1
    tmp=left_w+right_e
    
    if ans>tmp:
        ans=tmp
print(ans)

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

Solution (Python):
N=int(input())
S=input()
l=0
ans=r=S.count('E')
for c in S:
    if c=='E': r-=1
    ans=min(ans,l+r)
    if c=='W': l+=1
print(ans)

Solution (Python):
N=int(input())
S=input()
ans=cur=S.count('W')
for c in reversed(S):
    if c=='W':
        cur-=1
    else:
        cur+=1
    ans=min(ans,cur)
print(ans)

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

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

Solution (Python):
N=int(input())
S=input()
l,r=[0]*N,[0]*N
for i in range(N-1):
    l[i+1]+=l[i]+(S[i]=='W')
for i in range(N-1,0,-1):
    r[i-1]+=r[i]+(S[i]=='E')
ans=3*10**5
for i in range(N):
    ans=min(ans,l[i]+r[i])
print(ans)

Solution (Python):
import collections


def main():
    N = int(input())
    S = input()
    cnt = collections.Counter(S)
    E_cnt = cnt['E']
    tmp = 0
    if S[0] == 'E':
        E_cnt -= 1
    ans = E_cnt
    for i in range(1, N):
        if S[i] == 'E':
            E_cnt -= 1
        if S[i - 1] == 'W':
            tmp += 1
        ans = min(ans, tmp + E_cnt)

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import collections


def main():
    N = int(input())
    S = input()
    cnt = collections.Counter(S)
    E_cnt = cnt['E']
    W_cnt = cnt['W']
    tmp = 0
    if S[0] == 'E':
        E_cnt -= 1
    ans = E_cnt
    for i in range(1, N):
        if S[i] == 'E':
            E_cnt -= 1
        else:
            W_cnt -= 1
        if S[i - 1] == 'W':
            tmp += 1
        ans = min(ans, tmp + E_cnt)

    print(ans)


if __name__ == "__main__":
    main()

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

lst = []
for moji in s:
    if moji == 'E':
        lst.append(0)
    else:
        lst.append(1)

lst = list(itertools.accumulate(lst))

mae = 0
ans = 10**9
last = lst[-1]
for i in range(n):
    tmp = mae + (n-(i+1)) - (last-lst[i])
    ans = min(ans,tmp)
    mae = lst[i]
print(ans)

Solution (Python):
import collections
N = int(input())
S = input()
pre = collections.Counter(S[1:])["E"]
mi = pre
for i in range(1,N):
    if S[i-1] == "W":
        pre += 1
    if S[i] == "E":
        pre -= 1
    mi = min(mi, pre)
print(mi)

Solution (Python):
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(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)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n=k()
p=X()
e=[1 if i=="E" else 0 for i in p]
w=[1 if i=="W" else 0 for i in p]
e=list(itertools.accumulate(e))
w=list(itertools.accumulate(w))

for i in range(n):
    x = e[-1]-e[i]+w[i]
    if mod > x:
        mod = x
        cnt = i
if cnt == 0:
    mod-=1

print(mod)

Solution (Python):
n = int(input())
s = input()
count = [0 for _ in range(n)]

west, east = 0, 0
for i, j in enumerate(s):
    count[i] += west
    if j == 'W':
        west += 1

for i, j in enumerate(s[::-1]):
    count[-i-1] += east
    if j == 'E':
        east += 1

print(min(count)        )

Solution (Python):
from collections import defaultdict
from collections import deque
from collections import Counter
import itertools
import math

def readInt():
  return int(input())
def readInts():
  return list(map(int, input().split()))
def readChar():
  return input()
def readChars():
  return input().split()

n = readInt()
s = list(input())

cw,ce = 0,0

for i in s:
  if i=="E":
    ce+=1
if s[0]=="E":
  ce -= 1
now = ce
ans = now

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

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

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

Solution (Python):
n = int(input())
s = input()
e = s.count("E")
ans = 10**6
for i in s:
    e -= i == "E"
    ans = min(ans, e)
    e += i == "W"
print(ans)

Solution (Python):

n = int(input())
s = input()
w = 0
e = s.count("E")
ans = 10**6
for i in s:
    e -= i == "E"
    ans = min(ans, e + w)
    w += i == "W"
print(ans)

Solution (Python):

n = int(input())
s = input()
e = [0]
w = [0]
for i in s:
    e.append(e[-1] + (i == "E"))
    w.append(w[-1] + (i == "W"))
ans = 10**6
for i in range(n):
    ans = min(ans, w[i] + e[-1] - e[i + 1])
print(ans)

Solution (Python):
from itertools import accumulate

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

t01 = ""
t10 = ""

for x in s:
    if x == "E":
        t01 += "0"
        t10 += "1"
    else:
        t01 += "1"
        t10 += "0"

l1 = [int(a) for a in t01]
l2 = [int(a) for a in t10]
left = [0] + list(accumulate(l1))[:-1]
right = list(accumulate(l2[::-1]))[-2::-1] + [0]
print(min(w + e for (w, e) in zip(left, right)))

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

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


res = N
for i in range(N):
    res = min(x[i]+y[N-i-1], res)

print(res)

Solution (Python):






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





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

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

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

e = [0 for _ in range(N)]
w = [0 for _ in range(N)]

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

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

Solution (Python):
n = int(input())
s = str(input())
L = [[0]*2 for i in range(n+1)]
R = [[0]*2 for i in range(n+1)]

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

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

ans = 10**18
for i in range(n):
    temp = L[i][1]+R[i+1][0]
    ans = min(ans, temp)
print(ans)

Solution (Python):
n = int(input())
s = str(input())
L = [[0]*2 for i in range(n+1)]
R = [[0]*2 for i in range(n+1)]

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

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

ans = 10**18
for i in range(n):
    temp = L[i][1]+R[i+1][0]
    ans = min(ans, temp)
print(ans)