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()
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 = input()
re = 0
for s in S:
    if s == 'E':
        re += 1
lw = 0
c = len(S)
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):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7


N = INT()
S = input()
 
tmp_W = 0
tmp_E = 0
 
cnt = [0]*len(S)
for i in range(len(S)):
	if S[i] == "W":
		tmp_W += 1
	cnt[i] += tmp_W
 
for i in range(len(S)-1, -1, -1):
	if S[i] == "E":
		tmp_E += 1
	cnt[i] += tmp_E
 
print(min(cnt)-1)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7


N = INT()
S = input()

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

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

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

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

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7


N = INT()
S = input()

tmp_W = 0
tmp_E = 0

cnt = [0]*len(S)
for i in range(len(S)):
	if S[i] == "W":
		tmp_W += 1
	cnt[i] += tmp_W

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

print(min(cnt)-1)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7


N = INT()
S = input()

tmp_W = 0
tmp_E = 0
W = []
E = []
ans = INF

cnt = [0]*len(S)
for i in range(len(S)):
	if S[i] == "W":
		tmp_W += 1
	cnt[i] += tmp_W

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

print(min(cnt)-1)

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

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

W_num = [0] * N
E_num = [0] * N

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

print(min([W_num[i] + E_num[i] for i in range(N)]))

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


ans = n+1
for i in range(1, n+1):
    res = 0
    res += w_count[i-1]
    res += e_count[n] - e_count[i]
    ans = min(ans, res)
print(ans)

Solution (Python):

import sys
input = sys.stdin.readline









N = int(input())
S = input().rstrip()



l = [0]*N
r = [0]*N

cnt = 0
for i in range(N-1):
    if S[i]=="W":
        cnt += 1
    l[i+1] = cnt
cnt = 0
for i in range(N-1):
    if S[N-1-i]=="E":
        cnt += 1
    r[N-1-i-1] = cnt

ans = N
for i in range(N):
    ans = min(ans, l[i]+r[i])

print(ans)

Solution (Python):
n = int(input())
s = input()
left = 0
right = s[1:].count('E')
cnt = [left + right]

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

print(min(cnt))

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

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

cnt = [left+right]

for i in range(1,len(S)):
    if S[i-1] == 'W':
        left += 1
    
    if S[i] == 'E':
        right -= 1
        
    cnt.append(left+right)

print(min(cnt))

Solution (Python):
import collections
N = int(input())
S = list(input())
cnt = 0

ans = [0]*N

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

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

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

ans = [0] * n
left = 0
right = s[1:len(s)].count('E')
ans[0] = right

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

print(min(ans))

Solution (Python):
N = int(input())
S = input()
e = [0]*(N+1)
w = [0]*(N+1)
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 = input()
right_w = 0
right_E = 0
left_E = 0
left_W = 0
ans = len(s)
for i in s:
    if i == "W":
        right_w += 1
    else:
        right_E += 1
for i in range(len(s)):
    tmp = s[i]
    if tmp == 'W':
        right_w -= 1
        tmp_ans = left_W+ right_E
        if tmp_ans < ans:
            ans = tmp_ans
        left_W += 1
    else:
        right_E -= 1
        tmp_ans = left_W+ right_E
        if tmp_ans < ans:
            ans = tmp_ans
        left_E += 1
print(ans)

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

Solution (Python):
n = int(input())
S = input()
W = [0] * (n+1)
w = 0
ans = float("inf")

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

Solution (Python):
n = int(input())
S = input()
WE = [[] for _ in range(n)]
w = 0
e = 0

for i in range(n):
    if S[i] == "W":
        w += 1
    else:
        e += 1
    
    WE[i].append(w)
    WE[i].append(e)
    
ans = WE[n-1][1] - WE[0][1]

for i in range(1,n):
    res = WE[i-1][0] + (WE[n-1][1] - WE[i][1])
    ans = min(ans,res)
    
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] = 1
    
    if S[i] == "E":
        e[i+1] = 1

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

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

print(ans)

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

accum_w = [0] * (n+1)  
accum_e = [0] * (n+1)  

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

cnt_e = 0
for j in range(n-1, -1, -1):
  if s[j] == 'E':
    cnt_e += 1
  accum_e[j] = cnt_e




ans = 2**64-1
for i in range(n):
  ans = min(ans, accum_w[i]+accum_e[i+1])

print(ans)

Solution (Python):
def resolve():
	n = int(input())
	s = input()
	west_sum = [0]*(n+1)
	east_sum = [0]*(n+1)

	for i in range(n):
		if s[i] == 'E':
			east_sum[i+1] = east_sum[i] + 1
			west_sum[i+1] = west_sum[i]
		else:
			west_sum[i+1] = west_sum[i] + 1
			east_sum[i+1] = east_sum[i]
	ans = float('inf')
	for i in range(1, n):
		if i == 1:
			ans = min(ans, east_sum[-1] - east_sum[1])
		elif i == (n - 1):
			ans = min(ans, west_sum[-2])
		else:
			v = west_sum[i-1] + (east_sum[-1] - east_sum[i])
			ans = min(ans, v)
	print(ans)
resolve()

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()

from collections import defaultdict, deque, Counter
from sys import exit
import heapq
import math
import fractions
import copy
from itertools import permutations
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7
N = getN()
S = input()

up = [0 for i in range(N)]

down = [0 for i in range(N)]

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

for i in range(2, N + 1):
    if S[-i + 1] == "E":
        down[-i] = down[-i + 1] + 1
    else:
        down[-i] = down[-i + 1]
ans = float('inf')
for i in range(N):
    opt = up[i] + down[i]
    ans = min(ans, opt)
print(ans)

Solution (Python):
N = int(input())
S = list(input())
cnt_E = [S.count("E")]
flag = True
if S[0] == "W":
    cnt_W = [1]
else:
    cnt_W = [0]
    flag = False
SUM = [cnt_E[0]+cnt_W[0]]
for i in range(1,N):
    if S[i] == "W":
        cnt_W.append(cnt_W[i-1]+1)
        cnt_E.append(cnt_E[i-1])
    else:
        cnt_W.append(cnt_W[i-1])
        if flag:
            cnt_E.append(cnt_E[i-1])
            flag = False
        else:
            cnt_E.append(cnt_E[i-1]-1)
    SUM.append(cnt_W[i]+cnt_E[i])
print(min(SUM)-1)

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




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

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




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

print(ans)

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

i = S[1:].count('E')
r = [i]

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

    if S[n - 1] == 'W':
        i += 1
    r.append(i)

print(min(r))

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

i = S[1:].count('E')
ans = [i]

for n in range(N):
    if n == 0:
        continue

    if S[n] == 'E':
        i -= 1

    if S[n - 1] == 'W':
        i += 1
    ans.append(i)

print(min(ans))

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

Ecnt = S.count('E')
Wcnt = S.count('W')

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

if S[0]=='E':
    E[0] = 1
else:
    W[0] = 1

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


score=[]
for i in range(N):
        score.append(W[N]-W[i+1] + E[i]-E[0])
print(N-max(score)-1)

Solution (Python):






import sys
input = sys.stdin.readline

import math

import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache






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






def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()


def DD(arg): return defaultdict(arg)



def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr


def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd (a, b)


def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count


def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))


def int_log(n, a):
  count = 0
  while n>=a:
    n = n//a
    count += 1
  if a**count == n:
    return count
  else:
    return count+1





N = I()
S = S()

X = []
Y = []

count = 0
for i in range(N):
  if S[i] == "W":
    count += 1
  X.append(count)
  
count = 0
for i in range(N):
  if S[N-1-i] == "E":
    count += 1
  Y.append(count)

P(min(X[i]+Y[N-1-i] for i in range(N))-1)

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
	else:
		sum_W[i+1]=sum_W[i]
		
	if S[i]=="E":
		sum_E[i+1]+=sum_E[i]+1
	else:
		sum_E[i+1]=sum_E[i]
ans=N
count=0
for i in range(1,N+1):
	
	tmp=sum_W[i-1]+(sum_E[-1]-sum_E[i])
	if tmp<ans:
		ans=tmp
		count=i
print(ans)

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

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

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

    print(n - cntmax -1)


resolve()

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

tot = S[1:].count('E')
tot_list = [tot]
former_leader = S[0]

for l in range(1, N):
    leader = S[l]
    if (former_leader, leader)  == ('E', 'E'):
        tot -= 1
    if (former_leader, leader)  == ('W', 'W'):
        tot += 1
        
    former_leader = leader  
    tot_list.append(tot)
    
print(min(tot_list))

Solution (Python):
N = int(input())
S = [d for d in input()]

tot = S[1:].count('E')
tot_list = [tot]
former_leader = S[0]

for l in range(1, N):
    leader = S[l]
    if (former_leader, leader)  == ('E', 'E'):
        tot -= 1
    if (former_leader, leader)  == ('W', 'W'):
        tot += 1
        
    former_leader = leader  
    tot_list.append(tot)
    
print(min(tot_list))

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

Solution (Python):
from collections import Counter
 
N = int(input())
S = list(input())
c = Counter(S)
left_facing_west = 0
right_facing_east = c['E']
ans = float('inf')
for s in S:
    if s == 'W':
        ans = min(ans,left_facing_west+right_facing_east)
        left_facing_west += 1
    else:
        right_facing_east -= 1
        ans = min(ans,left_facing_west+right_facing_east)
print(ans)

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

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

print(ans)

Solution (Python):


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

answer_sum = [0] * n
e_sum = [0] * (n+1)
w_sum = [0] * (n+1)

for i in range(n):
    w_sum[i+1] = (s[i] == 'W') + w_sum[i]
    e_sum[i+1] = (s[i] == 'E') + e_sum[i]

for i in range(n):
    answer_sum[i] = w_sum[i] + (e_sum[n] - e_sum[i+1])

print(min(answer_sum))

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

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

w=s.count('W')
e=s.count('E')
r_w=w
r_e=e
l_w=0
l_e=0
ans=n

for i in range(n):
    if s[i]=='W':
        r_w-=1
        tmp=l_w+r_e
        l_w+=1
    else:
        r_e-=1
        tmp=l_w+r_e
        l_e+=1
    if tmp<ans:
        ans=tmp
print(ans)

Solution (Python):
def main():
    N = int(input())
    S = input()
    west_counts = [0 for _ in range(N)]
    east_counts = [0 for _ in range(N)]
    for i, c in enumerate(S):
        if c == 'W':
            west_counts[i] = 1
        else:
            east_counts[i] = 1
    for i in range(1, N):
        west_counts[i] += west_counts[i - 1]  
        east_counts[N - 1 - i] += east_counts[N - i]  
    ans = N
    for i in range(N):
        d = 0
        if i < N - 1:
            d += east_counts[i + 1]  
        if i > 0:
            d += west_counts[i - 1]  
        ans = min([ans, d])
    print(ans)


if __name__ == '__main__':
    main()

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

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

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

    ans = 10 ** 6
    sum_count = S.count('E')
    for s in S:
        aft = 0
        if s == 'W':
            aft = 1
        else:
            sum_count -= 1
        ans = min(ans, sum_count)
        sum_count += aft
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
s = list(input())
ans = 1001001
wn = 0
en = sum([1 for i in s[1:] if i == 'E'])
for i in range(n):
    ans = min(ans, wn + en)
    if s[i] == 'W':
        wn += 1
    if i != n - 1:
        if s[i + 1] == 'E':
            en -= 1
print(ans)

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

Solution (Python):
from collections import Counter

def resolve():
	n = int(input())
	s = input()
	sum_e = [0]*(n+1)
	sum_w = [0]*(n+1)
	for i in range(n):
		ev = 1 if s[i] == 'E' else 0
		wv = 1 if s[i] == 'W' else 0
		sum_e[i+1] = sum_e[i] + ev
		sum_w[i+1] = sum_w[i] + wv
	ans = float('inf')
	if n == 2:
		print(1 if len(Counter(list(s)).keys()) == 2 else 0)
		return
	e_w = sum_e[-1] - sum_e[1]
	e_e = sum_w[n-1]
	ans = min(e_w, e_e)
	for i in range(n):
		v = sum_w[i-1] + (sum_e[-1] - sum_e[i])
		ans = min(ans, v)
	print(ans)
resolve()

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

N = int(input())
S = input().rstrip()
W = [0] * N
E = [0] * N

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

min = float("inf")
for i in range(N):
    if W[i] + E[i] < min:
        min = W[i] + E[i]
print(min)

Solution (Python):



import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

n=I()
s=SI()
east=[]
west=[]
state=s[0]
count=1
if s[0]=="W":
    east.append(0)
for i in range(1,n):
    if s[i]==state:
        count+=1
    else:
        if state=="W":
            west.append(count)
            count=1
            state="E"
        else:
            east.append(count)
            count=1
            state="W"
if state=="W":
    west.append(count)
else:
    east.append(count)
if s[-1]=="E":
    west.append(0)


x=len(east)
E=sum(east)
W=sum(west)
anse=[]
answ=[]
vale=E-east[0]
for i in range(1,x):
    anse.append(vale)
    vale+=(west[i-1]-east[i])
anse.append(vale)
'''if east[0]==0:
    anse[0]=inf'''
print(min(anse))

Solution (Python):
"""
3*10**5
単純に両側の差を全探索だと間に合わないO(N*2)

情報を先に保存しておきそれを使う
"""

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

E_list = [0] * N 
W_list = [0] * N

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

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

print(min([e+w for e,w in zip(E_list, W_list)]))

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

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

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

print(min([e + w for e, w in zip(e_list, w_list)]))

Solution (Python):
from collections import Counter

N = int(input())
S = list(input())
c = Counter(S)
left_facing_west = 0
right_facing_east = c['E']
ans = float('inf')
for s in S:
    if s == 'W':
        ans = min(ans,left_facing_west+right_facing_east)
        left_facing_west += 1
    else:
        right_facing_east -= 1
        ans = min(ans,left_facing_west+right_facing_east)
print(ans)

Solution (Python):
N = int(input())
S = input()
S_rep = S.replace('W', '1').replace('E', '0') 

lst1 = [int(S_rep[0])]
for s in S_rep[1:]:
    lst1.append(lst1[-1] + int(s))
ans = [N - lst1[-1] + int(S_rep[0]) - 1]
for i in range(1, N):
    ans.append(lst1[i-1] + N - i - 1 - lst1[-1] + lst1[i])

print(min(ans))

Solution (Python):
n = int(input().strip())
s = input().strip()
arr = [0] * n
east = 0
west = 0

for i in range(n):
    arr[i] = east
    if s[i] == 'E':
        east += 1
for i in range(n - 1, -1, -1):
    arr[i] += west
    if s[i] == 'W':
        west += 1
ma = max(arr)
idx = i
for i in range(n):
    if arr[i] == ma:
        idx = i
if idx == (n - 1):
    print(s[:idx].count('W'))
else:
    print(s[:idx].count('W') + s[(idx + 1):].count('E'))

Solution (Python):
from collections import Counter

N = int(input())
arr = input()
west_side = Counter()
east_side = Counter(arr)
costs = []

for i in range(N):
    leader = arr[i]
    east_side[leader] -= 1

    costs.append(west_side['W'] + east_side['E'])
    west_side[leader] += 1

print(min(costs))

Solution (Python):

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

"""ans=100000000000
for i in range(n):
    count=0
    for j in range(n):
        if i==j:
            continue

        if (i<j)&(s[j]=="E"):
            count+=1
        elif (j<i)&(s[j]=="W"):
            count+=1

    ans=min(ans,count)

print(ans)"""

count_W=[]
for i in range(n):
    count_W.append(0)

if s[0]=="W":
    count_W[0]=1
else:
    count_W[0]=0

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

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

print(ans)

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

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

ans = 10 ** 9
for i in range(n):
    if s[i] == 'E':
        e = count_e[-1] - count_e[i]
        w = count_w[i]
    else:
        e = count_e[-1] - count_e[i]
        w = count_w[i] - 1
    ans = min(ans,e+w)
print(ans)

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

e = 1
w = 1
count_e = [0] * n
count_w = [0] * n
for i in range(n):
    if i == 0:
        if s[i] == 'E':
            count_e[i] = e
            e += 1
        else:
            count_w[i] = w
            w += 1
        continue
    if s[i] == 'E':
        count_e[i] = e
        count_w[i] = count_w[i-1]
        e += 1
    else:
        count_w[i] = w
        count_e[i] = count_e[i-1]
        w += 1

ans = 10 ** 9
for i in range(n):
    if s[i] == 'E':
        e = count_e[-1] - count_e[i]
        w = count_w[i]
    else:
        e = count_e[-1] - count_e[i]
        w = count_w[i] - 1
    ans = min(ans,e+w)
print(ans)

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

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

tmp=W[0]+E[0]
for i in range(1, N):
    if tmp > W[i]+E[i]:
        tmp = W[i]+E[i]

print(tmp)

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

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

cnt=ans

for i in range(n):
    if s[i:i+2]=='WW':
        cnt=cnt+1
    elif s[i:i+2]=='EE':
        cnt=cnt-1
    else:
        0
        
    
    ans=min(ans,cnt)
print(ans)

Solution (Python):
N = int(input())
S = input()
Wl = []
El = []
c = 0
for i in range(N):
    if S[i] == 'W':
        c += 1
    Wl.append(c)
c = 0
for i in range(N):
    if S[-(1 + i)] == 'E':
        c += 1
    El.append(c)
El.reverse()
Amin = min(El[1], Wl[N - 2])
for i in range(1, N - 2):
    A = Wl[i - 1] + El[i + 1]
    Amin = min(Amin, A)
print(Amin)

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

w_e = [0] * n
e_w = [0] * n
ans = []

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

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

for i in range(n):
    ans.append(w_e[i] + e_w[i])

print(min(ans))

Solution (Python):

n = int(input())
ln = list(map(int, input().replace('W', '0 ').replace('E', '1 ').split()))


w_sum = 0                           
sum_min = e_sum = sum(ln) - ln[0]   

for i in range(1, n-1):
    
    w_sum += 1 if ln[i-1] == 0 else 0
    e_sum -= ln[i]
    sum_min = min(sum_min, w_sum + e_sum)


else:
    w_sum += 1 if ln[n-1] == 0 else 0
    sum_min = min(sum_min, w_sum)


print(sum_min)

Solution (Python):
import numpy as np

N = int(input())
S = input()
left_W = 0
right_E = S.count("E")
ans = N

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

print(ans)