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



Sample input:
5
WEEWW


Sample output:
1

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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]
    #print(west_num,east_num,L[i])
    if L[i] == 0:
        west_num =max(west_num -1,0)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

# from numba import njit

# input = stdin.readline


INF = pow(10,9)

# @njit
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()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# E = 右向き, W = 左向き
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)

Submission status: Accepted

Solution code:
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
    #print(left,left_e,left_w,right,right_e,right_w,tmp)
    if ans>tmp:
        ans=tmp
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
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)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[14]:


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


# In[15]:


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)


# In[ ]:

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 2020/07/24
# AtCoder Regular Contest 098 - C

# Input
n = int(input())
s = input()

# Init
acme = list()
acmw = list()
acme.append(0)
acmw.append(0)
ans = n+1

# Make accumed list
for i in range(n):
    if s[i] == 'E':
        acme.append(acme[i] + 1)
        acmw.append(acmw[i])
    elif s[i] == 'W':
        acme.append(acme[i])
        acmw.append(acmw[i] + 1)

acme.append(acme[n])
acmw.append(acmw[n])

# Calc
for i in range(1, n+1):
    ecnt = acme[n] - acme[i]
    wcnt = acmw[i-1]
    wcnt = ecnt + wcnt
    ans = min(ans, wcnt)

# Output
print(ans)

Submission status: Accepted

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

e_cnt = S.count('E')
w_cnt = 0
ans = float('inf')
for i in range(N):
    if S[i] == 'E':
        e_cnt -= 1
        w = w_cnt
    else:
        w_cnt += 1
        w = w_cnt - 1
    
    ans = min(ans, e_cnt+w)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input().strip()
A = [0 for _ in range(N+1)]
B = [0 for _ in range(N+1)]
for i in range(1,N+1):
    if S[i-1]=="E":
        A[i] = A[i-1]+1
        B[i] = B[i-1]
    else:
        A[i] = A[i-1]
        B[i] = B[i-1]+1
cmin = 10**6
for i in range(N):
    cnt = B[i]+A[N]-A[i+1]
    cmin = min(cmin,cnt)
print(cmin)

Submission status: Accepted

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

# E:右　W:左
cnt = [0] * (n+1)
for i in range(1, n+1):
    cnt[i] += cnt[i-1]
    if s[i-1] == "E": cnt[i] += 1

ans = float("INF")
for i in range(1,n+1):
    a = ((i-1) - cnt[i-1]) + (cnt[-1]-cnt[i])
    if ans > a: ans = a
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
C = [0] * N
t = 0
for i in range(N):
    C[i] = t
    if S[i] == 'W':
        t += 1
t = 0
for i in range(N - 1, -1, -1):
    C[i] += t
    if S[i] == 'E':
        t += 1
print(min(C))

Submission status: Accepted

Solution code:
n=int(input())
s=input()
tmp=0
ans=10**6
W=s.count('W')
for i in range(n):
  if s[i]=='W':
    ans=min(ans,n-1-(W-1)+tmp)
    tmp+=1
  else:
    ans=min(ans,n-1-W+tmp)
    tmp-=1
print(ans)

Submission status: Accepted

Solution code:
#%%
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)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
w=[0]*(n+2)
e=[0]*(n+2)
for i in range(n):
  if s[i]=='W':w[i+1]=1
  else:e[i+1]=1
for i in range(1,n+2):
  w[i]+=w[i-1]
  e[-i-1]+=e[-i]
ans=10**9
for i in range(1,n+1):
  ans=min(ans,w[i-1]+e[i+1])
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')


def solve():
    # n, m = [int(x) for x in input().split()]
    n = int(input())
    s = input()
    w = 0 # W
    e = 0 # E
    for i in range(n):
        if s[i] == 'W':
            w += 1
        else:
            e += 1
    ww = 0
    ee = 0
    ans = n
    for i in range(n):
        if s[i] == 'W':
            w -= 1
        else:
            e -= 1
        
        ans = min(ans, ww + e)

        if s[i] == 'W':
            ww += 1
        else:
            ee += 1

    print(ans)
    
t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

R s
S p
P r

"""

Submission status: Accepted

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

cnt_W = [0]*N #西を向いている人の累積和を求める
if S[0] == 'W':
    cnt_W[0] = 1

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

most_E = cnt_W[-1] #全体で西を向いている人数
ans = []
for i in range(N):
    if i == 0:
        if S[i] == "W":
            ans.append(N-most_E)
        else:
            ans.append((N-most_E)-1)
    elif i == N-1:
        if S[i] == "W":
            ans.append(most_E-1)
        else:
            ans.append(most_E)
    else:
        ans.append(cnt_W[i-1] + (N-most_E) - (i+1-cnt_W[i]))

print(min(ans))

Submission status: Accepted

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

cnt_W = [0]*N #西を向いている人の累積和を求める
if S[0] == 'W':
    cnt_W[0] = 1

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

most_E = cnt_W[-1] #全体で西を向いている人数
#print(most_E)
ans = float('inf')
for i in range(N):
    tmp = 0
    if i == 0:
        if S[i] == "W":
            tmp = (N-most_E)
        else:
            tmp = (N-most_E)-1
        #print(tmp)
    elif i == N-1:
        if S[i] == "W":
            tmp = most_E-1
        else:
            tmp = most_E
        #print(tmp)
    else:
        tmp = cnt_W[i-1] + (N-most_E) - (i+1-cnt_W[i])
        #print(tmp)

    ans = min(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

    N = int(input())
    S = input().rstrip('\n')
    ans = N
    w = 0
    e = S.count("E")
    for s in S:
        if s == "E":
            e -= 1
        ans = min(ans, w+e)
        if s == "W":
            w += 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

ec, wc = 0, 0
for i in range(1, n):
    if s[i] == "E":
        ec += 1

ans = ec+wc
for i in range(1, n):
    wc += int(s[i - 1] == "W")
    ec -= int(s[i] == "E")
    ans = min(ans, wc+ec)

print(ans)

Submission status: Accepted

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

def solve():
    right_E=0
    left_W=0
    ans=10**7

    for i in range(len(s)):
        if s[i]=="E":
            right_E+=1
     
    for i in range(len(s)):
        if s[i]=="E":right_E-=1
        cnt=left_W+right_E
        ans=min(ans,cnt)
        if s[i]=="W":left_W+=1
    
    print(ans)
    
if __name__ == "__main__":
    solve()

Submission status: Accepted

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

def solve():
    right_E=0
    right_W=0
    left_E=0
    left_W=0
    ans=10**7

    for i in range(0,len(s)):
        if s[i]=="E":
            right_E+=1
        else:
            right_W+=1
    
    for i in range(0,len(s)):
        if s[i]=="E":
            right_E-=1
            cnt=left_W+right_E
            ans=min(ans,cnt)
            left_E+=1
        else:
            right_W-=1
            cnt=left_W+right_E
            ans=min(ans,cnt)
            left_W+=1
    
    print(ans)
    
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
n, s = int(input()), input()

ans, e, w = n, s.count('E'), 0
for i in s:
    if i == 'E':
        e -= 1
    ans = min(ans, e + w)
    if i == 'W':
        w += 1
print(ans)

Submission status: Accepted

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

numbers=[]
number=[0,0]


for s in S:
    if s=='W':
        number[0]+=1
        suuji=number[1]-number[0]
        numbers.append(suuji)
    else:
        number[1]+=1
        suuji=number[1]-number[0]
        numbers.append(suuji)
place=numbers.index(max(numbers))


if place==0:
    answer=S[1:].count('E')
    print(answer)
elif place==N-1:
    answer=S[:-1].count('W')
    print(answer)
else:
    first=S[:place]
    last=S[place+1:]
    answer=first.count('W')+last.count('E')
    print(answer)

Submission status: Accepted

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

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

sum_left_w = 0
sum_right_e = alle
result = n
for i in range(n):
  if s[i] == 'E':
    sum_right_e -= 1
  result = min(result, sum_left_w+sum_right_e)
  
  if s[i] == 'W':
    sum_left_w += 1
print(result)

Submission status: Accepted

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

from collections import Counter
we=Counter(list(S))
enum=we["E"]
wnum=0
ans=enum
for s in S:
  if s=="W":
    wnum+=1
  elif s=="E":
    enum-=1
  ans=min(ans, enum+wnum)
print(ans)

Submission status: Accepted

Solution code:
import bisect,copy,heapq,string
from collections import *
from math import *
from itertools import *
import sys
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
s = input()
a = [0]*n
b = [0]*n
c = [0]*n

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

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

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

for i in range(n-2,-1,-1):
        b[i] += b[i+1]

ans = min(a[i]+b[i] for i in range(n))
print(ans)

Submission status: Accepted

Solution code:
def Csum(a,x):
    b,c=[0],0
    for i in range(len(a)):
        if a[i]==x:
            c+=1
        b.append(c)
    return b

n=int(input())
s=input()
a,b,w,e=Csum(s,'W'),Csum(s,'E'),s.count('W'),s.count('E')
a.append(w)
b.append(e)
ans=n
for i in range(1,n+1):
    ans=min(ans,e+a[i-1]-b[i])
print(ans)

Submission status: Accepted

Solution code:
N = input()
s = input()

#累積和 前処理 Eの数を集計
count = s.count('E')

ans = count
#左端1番目からリーダーをずらしながらチェック。
for ch in s:
    if ch == 'E':
        count -= 1
        ans = min(count,ans)
    else:
        count += 1
    
print(ans)

Submission status: Accepted

Solution code:
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


def calc_cumsum(l):
    cumsum = [l[0]]
    for i in l[1:]:
        cumsum.append(cumsum[-1] + i)
    return cumsum


n = I()
s = S()
ruiseki_e = [0] * n
ruiseki_w = [0] * n
if s[0] == 'E':
    ruiseki_e[0] += 1
else:
    ruiseki_w[0] += 1
for i in range(1, n):
    if s[i] == 'E':
        ruiseki_e[i] = ruiseki_e[i - 1] + 1
        ruiseki_w[i] = ruiseki_w[i - 1]
    else:
        ruiseki_w[i] = ruiseki_w[i - 1] + 1
        ruiseki_e[i] = ruiseki_e[i - 1]
ans = ruiseki_e[-1] - ruiseki_e[1]  # 一番左端をリーダーにした時
for i in range(1, n):
    ans = min(ans,
              ruiseki_w[i - 1] + (ruiseki_e[-1] - ruiseki_e[i]))
print(ans)

Submission status: Accepted

Solution code:
N = input()
s = input()

#累積和 前処理 Eの数を集計
count = s.count('E')

ans = count
#左端1番目からリーダーをずらしながらチェック。
for ch in s:
    if ch == 'E':
        count -= 1
        ans = min(count,ans)
    else:
        count += 1
    
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
left = [0]
right = [0]
for i in range(n):
    if s[i] == 'W':
        tmp = left[-1] + 1
        left.append(tmp)
    else:
        left.append(left[-1])
    if s[n-1-i] == 'E':
        tmp = right[-1] + 1
        right.append(tmp)
    else:
        right.append(right[-1])
mi = 10**18
right = right[::-1]
for i in range(n+1):
    if mi > left[i] + right[i]:
        mi = left[i] + right[i]
print(mi)

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))

n = ii()
s = input()
left = s[1:].count('W')

dp = [[0,left]]
ans = n-1-left
last = s[0]
for i in s[1:]:
    ri,le = dp[-1]
    if i == 'W':
        le -= 1
    if last == 'E':
        ri += 1
    dp.append([ri,le])
    last = i
    ans = min(ans,n-1-ri-le)
print(ans)

Submission status: Accepted

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

EAST = []
WEST = []
e = 0
w = 0
for s in S:
  if s == "E":
    e += 1
  else:
    w += 1
  EAST.append(e)
  WEST.append(w)
  
ans = N - 1
for i in range(N):
  c = 0
  if i > 0:
    c += WEST[i-1]
  if i < N - 1:
    c += EAST[N-1] - EAST[i]
  
  ans = min(c,ans)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l = list(input())

ans = 100000000
east = 0
west = 0
for i in range(1, n):
    if l[i] == 'E':
        east+=1
ans = east

if l[0] == 'E':
    east+=1

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

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/arc098/tasks/arc098_a
from collections import Counter, defaultdict
n = int(input())
s = input()

left = defaultdict(int)
right = Counter(s)

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

Submission status: Accepted

Solution code:
import numpy as np

n=int(input())
l=np.array(list(x for x in input()))
w_left=0
e_right=n-(len(l[l=='W']))
ans=n

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
from collections import Counter
c = Counter([i for i in s])
left_w = 0
right_e = c['E']
if s[0] == 'E':
    right_e -= 1

ans = left_w + right_e
for i in range(1, len(s)):
    if s[i-1] == 'W':
        left_w += 1
    if s[i] == 'E':
        right_e -= 1
    ans = min(ans, left_w+right_e)
print(ans)