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()
te,tw = [],[]
te.append(1 if S[0]=='E' else 0)
tw.append(1 if S[0]=='W' else 0)
for i in range(1,N):
    if S[i]=='E':
        te.append(te[i-1]+1)
    else:
        te.append(te[i-1])
    if S[i]=='W':
        tw.append(tw[i-1]+1)
    else:
        tw.append(tw[i-1])
ans = min(te[-1],tw[-1])
for i in range(1,N-1):
    ans = min(ans,tw[i-1]+te[N-1]-te[i])
print(ans)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N = int(input())
    S = list(input())
    w, e = [0 for i in range(N)], [0 for i in range(N)]
    for i, s in enumerate(S):
        if s == 'W':
            if i > 0:
                w[i] = w[i-1] + 1
                e[i] = e[i-1]
            else:
                w[0] = 1
                e[i] = e[i-1]
        if s == 'E':
            if i > 0:
                w[i] = w[i-1]
                e[i] = e[i-1] + 1
            else:
                w[i] = w[i-1]
                e[0] = 1
    ans = 9999999
    for i in range(N):
        if i > 0:
            ans = min(ans, w[i-1]+(e[-1]-e[i]))
        else:
            ans = min(ans, e[-1]-e[i])
    print(ans)

Submission status: Accepted

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

countE = S.count('E')
countW = 0
min = countE + countW
for i in range(N):
    if S[i] == 'E':
        countE -= 1
    elif S[i] == 'W':
        countW += 1

    if countE + countW < min:
        min = countE + countW

print(min)

Submission status: Accepted

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

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

for reader in range(1, N):
    minus = 0
    if S[reader] == "E":
        minus = 1
    if S[reader - 1] == "W":
        ans += 1
    elif S[reader - 1] == "E":
        ans -= 1
    if ans - minus < min_ans:
        min_ans = ans - minus
        count = reader

print(min_ans)

Submission status: Accepted

Solution code:
def opt(N, S):
    if N == 1:
        return 0
    else:
        split = int(N/2)
        return min(opt(split,S[:split])+S[split:].count('E'),S[:split].count('W')+opt(N - split,S[split:]))

Num = int(raw_input())
Str = str(raw_input())
print(opt(Num, Str))

Submission status: Accepted

Solution code:
a=int(input())
b=input()
c=b.count('E')
d=0
e=c

for i in range(a):
 if b[i]=='E':
  c-=1
 else:
  d+=1
 e=min(e,c+d)


print(e)

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    N = int(stdin.readline().rstrip())
    S = stdin.readline().rstrip()
    spam1 = [0]
    for x in S[:-1]:
        spam1 += [spam1[-1] + 1 if x == 'W' else spam1[-1]]
    spam2 = [0]
    for x in S[::-1][:-1]:
        spam2 += [spam2[-1] + 1 if x == 'E' else spam2[-1]]
    spam2 = spam2[::-1]
    print(min([spam1[i] + spam2[i] for i in range(N)]))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

min_turns = n
for i in range(n):
  if i == 0:
    turns = n - 1 - (w[n - 1] - w[i])
  elif i == n - 1:
    turns = w[i - 1]
  else:
    turns = w[i - 1] + n - (i + 1) - (w[n - 1] - w[i])
  
  if turns < min_turns:
    min_turns = turns

print(min_turns)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
sample = s.replace("W","1").replace("E","0")
num = 0
num2 = sample.count("0")
sample2 = list(map(int, sample))
sample2.insert(0,0)
sample3 = s.replace("W","0").replace("E","1")
sample4 = list(map(int, sample3))
out = n
for i in range(n):
    num += sample2[i]
    num2 -= sample4[i]
    out = min(num+num2,out)
print(out)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = input()
    ec = S.count('E')
    wc = 0
    min = ec
    for k in S:
        if k == 'E':
            ec -= 1
            if min > ec + wc: min = ec + wc
        else:
            wc += 1
    print(min)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = input()
    ec = S.count('E')
    wc = 0
    min = ec
    for k in S:
        if k == 'E':
            ec -= 1
        else:
            wc += 1
        if min > ec + wc: min = ec + wc
    print(min)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = input()
    ec = S.count('E')
    tec = ec
    for k in S:
        if k == 'E':
            tec -= 1
        else:
            tec += 1
        ec = min(ec, tec)
    print(ec)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
m=list(input())
p=m.count("E")
s=p
for i in m:
    if i=="E":
        p-=1
    else:
        p+=1
    s=min(s,p)
print(s)

Submission status: Accepted

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

w_cusum_list = []
w = 0
for i in range(N):
  if S[i] == 'W':
    w += 1
  w_cusum_list.append(w)
    
e_cusum_list = []
e = 0
for i in range(1, N):
  if S[-i] == 'E':
    e += 1
  e_cusum_list.append(e)

minn = min(w_cusum_list[-1], e_cusum_list[-1])
for i in range(1, N):
  sumn = w_cusum_list[i-1] + e_cusum_list[N-i-1]
  if minn > sumn:
    minn = sumn

print(minn)

Submission status: Accepted

Solution code:
N = int(input())
str = input()
str_list=list(str)
num_list=[]
ans_list=[]
num=0
for i in range (N):
  if str_list[i]=="E":
    num=num+1
    num_list.append(num)
  else:
    num_list.append(num)
   
for i in range (N):
  if i==0:
    ans=num_list[N-1]-num_list[i]
    ans_list.append(ans)
  else:
    ans=num_list[N-1]-num_list[i]+i-num_list[i-1]
    ans_list.append(ans)

print(min(ans_list))

Submission status: Accepted

Solution code:
n=int(input())
s=input()
li=[]
w,e=0,0
wli,eli=[],[]
ans=[]

for i in range(n):
    if s[i]=="W":
        w+=1
    wli.append(w)
for i in reversed(range(n)):
    if s[i]=="E":
        e+=1
    eli.append(e)
for i in range(n):
    if i!=0 and i!=n-1:
        ans.append(wli[i-1]+eli[n-i-2])
    elif i==0:
        ans.append(eli[n-2])
    elif i==n-1:
        ans.append(wli[n-2])

print(min(ans))

Submission status: Accepted

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

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

right = []
left = []
bufl = 0
bufr = 0
for i in range(n):
    if 'W' == s[i]:
        bufl += 1
    left.append(bufl)
    if 'E' == s[(n-1)-i]:
        bufr += 1
    right.append(bufr)
right.reverse()

m = n
for i in range(n):
    if m > (right[i] + left[i]) - 1:
        m = (right[i] + left[i]) - 1
print(m)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
st = input()
l = []
stc = Counter(st)
ma = [Counter(st[:0])["W"] + Counter(st[1:])["E"]]
previous = st[0]
for i in range(1,n):
    temp = ma[-1]
    if(previous == "W"):
        temp+=1
    previous = st[i]
    if(st[i] == "E"):
        temp-=1
    ma.append(temp)
print(sorted(ma)[0])

Submission status: Accepted

Solution code:
n=int(input())
s=str(input())
arr=[]
k=0
for i in range(n):
    if(s[i]=='W'):
        k+=1
    arr.append(k)
arr1=[]
k=0
for i in range(n-1,-1,-1):
    if(s[i]=='E'):
        k+=1
    arr1.append(k)
sumx=300000
sumx=arr1[n-2]
#print(sumx)
if(arr[n-2]<sumx):
    sumx=arr[n-2]
for i in range(1,n-1):
    if(arr[i-1]+arr1[n-2-i]<sumx):
        sumx=arr[i-1]+arr1[n-2-i]
print(sumx)

Submission status: Accepted

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

d = {"W" : 0, "E" : 0}
for i in range(N):
    d[S[i]] += 1

t = {"W" : 0, "E" : 0}
ans = 300000
for i in range(N):
    ans = min(ans,t["W"] + d["E"] - t["E"] - (S[i] != "W"))
    t[S[i]] += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
min_num = 10**7
west_num = [0]
east_num = [0]
w_max = 0
e_max = 0
for i in range(0, N): #累積和を求める.
    if S[i] == 'W':
        w_max += 1
    else:
        e_max += 1
        
    west_num.append(w_max)
    east_num.append(e_max)

for i in range(0, N):
    num = west_num[i] + east_num[-1] - east_num[i+1]
    if num < min_num:
        min_num = num
        
print(min_num)

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
if __name__ == "__main__":
    n = int(input())
    s = list(input())
    ans = float("inf")
    a = [0]+[ 0 if tmp != 'W' else 1 for tmp in s ]
    b = list(reversed([ 0 if tmp != 'E' else 1 for tmp in s ]+[0]))
    for tmp in range(n-1):
        a[tmp+1] += a[tmp]
        b[tmp+1] += b[tmp]
    else:
        a.pop()
        b.pop()
        b = list(reversed(b))
    for tmp in range(n):
        ans = min(ans, a[tmp]+b[tmp])
    else:
        print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
lis =[0]
minn =N
for i in range(N):
  if S[i]=="E":
    lis.append(lis[i]+1)
  elif S[i]=="W":
    lis.append(lis[i])
#print(lis)
minn = lis[N]
#print(minn)
for i in range(2,N+1):
#  print("try:{0}".format(i))
#  print((i-1)-lis[i-1]+(lis[N]-lis[i]))
  minn = min(minn,(i-1)-lis[i-1]+(lis[N]-lis[i]))
print(minn)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
ans = S.count("E")
temp = ans
for i in range(N):
  if i == 0:
    if S[i] == "E":
      temp -= 1
  else:
    if S[i] == "E":
      temp -= 1
    if S[i - 1] == "W":
      temp += 1
  if temp < ans:
    ans = temp
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
counter = s.count("E")
ans = counter
for i in range(1,n+1):
	if s[i-1] == "W":
		counter += 1
	elif s[i-1] == "E":
		counter -= 1
	if ans >= counter:
		ans = counter
print(ans)

Submission status: Accepted

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

e_sum= S.count('E')

w_c=e_c=0
ans = N

for i in range(N):

    if S[i]=='E':
        e_c += 1
    else:
        w_c += 1

    if S[i]=='E':
        k = w_c + e_sum -e_c
    else:
        k = w_c -1 + e_sum -e_c
    
    ans = min(ans,k)

print(ans)

Submission status: Accepted

Solution code:
n,s=int(input()),input()
l,r=0,s[1:].count('E')
mn=r
for i in range(1,n):
    if s[i]=='E':
        r-=1
    if s[i-1]=='W':
        l+=1
    mn=min(mn,r+l)
print(mn)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
m=n+5
w=0
e=0
for i in range(1,n):
    if s[i]=='E':
        e+=1
m=min(m,w+e)
for i in range(1,n):
    if s[i-1]=='W':
        w+=1
    if s[i]=='E':
        e-=1
    m=min(m,w+e)
print(m)

Submission status: Accepted

Solution code:


N = int(input())
S = input()
#N = 12
#S = 'WEWEWEEEWWWE'


min = 1000000
e = S.count('E')
w = 0

for i in range(0,N):
    flag = 0
    now = S[i]
    if now == 'E':
        e = e - 1
    else:
        flag = 1
    sum = w + e
    if min > sum:
        min = sum
    if flag:
        w = w + 1
print(min)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
ans=[]
alle=list(s).count("E")
allw=n-alle
ecnt=wcnt=0
for i in range(n):
    if s[i] == "W":
        ans.append(wcnt+alle-ecnt)
        wcnt+=1
    else:
        ans.append(wcnt+alle-ecnt-1)
        ecnt+=1
print(min(ans))

Submission status: Accepted

Solution code:
def directions(n, s):
    left = []
    right = []

    change = 0
    for p in s:
        if p == 'W':
            change += 1
        left.append(change)

    change = 0
    for p in s[::-1]:
        if p == 'E':
            change += 1
        right.append(change)
    right.reverse()

    return min(map(lambda x, y: x + y - 1, left, right))



if __name__ == '__main__':
    n = int(input())
    s = input()
    print(directions(n, s))

Submission status: Accepted

Solution code:
n = int(input())
S = list(input())
dp = [0 for i in range(n)]
dp[0] = S[1:].count("E")

for i in range(n-1):
    if S[i] == S[i+1] == "E":
        dp[i+1] = dp[i]-1
    elif S[i] == S[i+1] == "W":
        dp[i+1] = dp[i]+ 1
    else:
        dp[i+1] = dp[i]

print(min(dp))

Submission status: Accepted

Solution code:
#abc098c

n=int(raw_input())
s=raw_input()
cnt=s[1:].count('E')
res=cnt
for i in xrange(1,n):
 if s[i-1]=='W':
  cnt+=1
 if s[i]=='E':
  cnt-=1
 res=min(cnt,res)
print res

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#-*- coding:utf-8 -*-
"""
"""

if __name__ == '__main__':
    N = int(input())
    S = input()

    wn = [0]
    en = [0]

    for i in range(N):
        if S[i] == "W":
            wn.append(wn[-1]+1)
            en.append(en[-1])
        if S[i] == "E":
            wn.append(wn[-1])
            en.append(en[-1]+1)

    m = N
    for i in range(len(S)):
        t = (wn[i] - wn[0]) + (en[N] - en[i+1])
        m = min(m, t)

    print(m)

Submission status: Accepted

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

Submission status: Accepted

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

W_cnt, E_cnt = [0], [0]
W_sum, E_sum = 0, 0

for S_i in S[:-1]:
    if S_i == 'W':
        W_sum += 1
    W_cnt.append(W_sum)
for S_i in list(reversed(S))[:-1]:
    if S_i == 'E':
        E_sum += 1
    E_cnt.append(E_sum)

ans = []
for W_i, E_i in zip(W_cnt, reversed(E_cnt)):
    ans.append(W_i+E_i)
print(min(ans))

Submission status: Accepted

Solution code:
n=int(input())
s=input()
tcnt=0
for i in range(1,n):
    tcnt+=s[i]=="E"

tmin=n
for i in range(n):
    if tmin>tcnt:
        tmin=tcnt
    if s[i]=="W":
        tcnt+=1
    try:
        if s[i+1]=="E":
            tcnt-=1
    except:
        pass
print(tmin)

Submission status: Accepted

Solution code:
import numpy as np

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


s = list(S)


s.reverse()
s = np.array([1 if i == 'E' else 0 for i in s])
#str = S.replace('E','1')
#bit_str = str.replace('W','0')
#bit = int(bit_str, base=2)

cost = [0]*N
cost[0] = (N-1) - sum(s[1:N])
for i in range(1,N):
    now = s[i]
    bef = s[i-1]
    cost[i] = cost[i-1] + bef - (1^now)

print(min(cost))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ans = [s[1:n].count("E")]
for i in range(1, n):
    #ans.append(s[:i].count('W') + s[i+1:].count('E'))
    ans.append(ans[i-1]-int(s[i] is "E")+int(s[i-1] is "W"))
print(min(ans))

Submission status: Accepted

Solution code:
n = int(input())

s = input()

l = []

a = 0

for i in range(0, n):
    if s[i:i+1] == "E":
        a += 1
    l.append(a)

#print(l)

ans = n

for i in range(0, n):
    e = w = 0
    if i > 0:
        e = i-l[i-1]
    w = l[n-1]-l[i]
#    print(str(e)+' '+str(w))
    ans = min(ans, e+w)

print(ans)

Submission status: Accepted

Solution code:
n = int(raw_input())
s = raw_input()
l = [(0,0)]
for i,c in enumerate(s):
    if c == 'E':
        l.append((l[i][0]+1, l[i][1]))
    else:
        l.append((l[i][0], l[i][1]+1))
mn = n
for i in range(1,n+1):
    mn = min(mn, l[i-1][1]+l[-1][0]-l[i][0])
print mn

Submission status: Accepted

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

result = s[1:].count("E")
temp = result
for i in range(n-1):
    if s[i] == "W":
        temp += 1
    if s[i+1] == "E":
        temp -= 1
    if temp < result:
        result = temp

print(result)

Submission status: Accepted

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

total = s.count("E")

min_val = total

for char in s:
    if char == "E":
        total -= 1
    else:
        total += 1
    min_val = min(min_val, total)

print(min_val)

Submission status: Accepted

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

count = len([s for s in S[1:] if s == "E"])
min_count = count
for i in range(1, N):
    if S[i - 1] == "W":
        count += 1
    if S[i] == "E":
        count -= 1
    min_count = min(min_count, count)
print(min_count)

Submission status: Accepted

Solution code:
num=int(input())
word=str(input())
w=[0]
e=[0]
if word[0]=='W':
	w=[1]
	e=[0]
else:
	w=[0]
	e=[1]
for i in range(1,num):
	if word[i]=='W':
		w.append(w[i-1]+1)
		e.append(e[i-1])
	else:
		w.append(w[i-1])
		e.append(e[i-1]+1)
n_th=e[num-1]
for i in range(1,num):
	n=w[i-1]+e[num-1]-e[i]
	if n_th>n:
		n_th=n

print (n_th)

Submission status: Accepted

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

pre_count = 0
post_count = len([k for k in range(1, N) if S[k] == 'E'])
count = post_count
for i in range(1, N):
    if S[i-1] == 'W':
        pre_count += 1
    if S[i] == 'E':
        post_count -= 1
    if count > pre_count + post_count:
        count = pre_count + post_count

print(count)

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
cnt=s[1:].count("E")
e_cnt=[0]*(n+1)
w_cnt=[0]*(n+1)
e_cnt[0]=cnt
for i in range(n):
    w_cnt[i+1]=w_cnt[i]+(s[i]=="W")#Wの数を累積和でとる
for i in range(1,n):
    e_cnt[i]=e_cnt[i-1]-(s[i]=="E")#Eの数を累積和でとる
ans=float("inf")
for i in range(n):
    ans=min(ans,w_cnt[i]+e_cnt[i])
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
cnt=s[1:].count("E")
e_cnt=[0]*(n+1)
w_cnt=[0]*(n+1)
e_cnt[0]=cnt
for i in range(n):
    w_cnt[i+1]=w_cnt[i]+(s[i]=="W")
for i in range(1,n):
    e_cnt[i]=e_cnt[i-1]-(s[i]=="E")
ans=float("inf")
for i in range(n):
    ans=min(ans,w_cnt[i]+e_cnt[i])
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
w=[0]*(n+1)
for i in range(n):
    w[i+1]=w[i]+(s[i]=="E")
ans=n
for i in range(n):
    ans=min(ans,(i-w[i])+(w[n]-w[i+1]))
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
min = S[1:N].count("E")
now = S[1:N].count("E")

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

Submission status: Accepted

Solution code:
def C(n, s):
    n = len(s)
    cr = 0
    cl = 0
    for i in range(1, n):
        if s[i] == "E":
            cl += 1
    
    min = cr + cl
    for i in range(1, n):
        if s[i - 1] == "W":
            cr += 1
        
        if s[i] == "E":
            cl -= 1
        
        if cr + cl < min:
            min = cr + cl
    
    return min

n = int(input())
s = input()
print(C(n, s))

Submission status: Accepted

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

n = p = S[1:].count("E")

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

print(n)

Submission status: Accepted

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

w=e=0
for s in S:
  if s=='W':
    w+=1
  else:
    e+=1

if S[0]=='W':
  v=e
else:
  v=e-1

r=v
for p in range(1, N):
  if S[p]=='W':
    t=v
    if S[p-1]=='W':
      t+=1
  else:
    t=v-1
    if S[p-1]=='W':
      t+=1
  if t<r:
    r=t
  v=t

print(r)

Submission status: Accepted

Solution code:
n=int(input())
a=list(input().strip())
w=0
wa=[]
for i in a:
  if i=="W":
    w+=1
  wa.append(w)

e=0
ea=[]
for i in a[::-1]:
  if i=="E":
    e+=1
  ea.append(e)
ea=ea[::-1]

ans=3 * 10**5
for i,j in zip(wa,ea):
  ans=min(ans,i+j-1)

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
SW=[0]
SE=[0]
for i in range(n):
  if(s[i]=="W"):
    SW.append(SW[-1]+1)
    SE.append(SE[-1])
  else:
    SE.append(SE[-1]+1)
    SW.append(SW[-1])
mi=n
SE.append(SE[-1])
for i in range(n):
  mi=min(SW[i]+SE[-1]-SE[i+1],mi)
print(mi)

Submission status: Accepted

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

    cnt = [s[1:n].count("E")]

    for i in range(1, n):
        cnt.append(cnt[i-1] - int(s[i] is "E") + int(s[i-1] is "W"))

    print(min(cnt))

if __name__=='__main__':
   solve()

Submission status: Accepted

Solution code:
n = input()
s = raw_input()
n_w = len(filter(lambda c: c=="W", s))
n_e = n - n_w
n_i_w = 0
n_i_e = 0
ans = n
for i in range(n):
    if s[i] == "W":
        n_i_w += 1
    else:
        n_i_e += 1
    cur = n_i_w + n_e - n_i_e
    if s[i] == "W":
        cur -= 1
    ans = min(ans, cur)
print ans

Submission status: Accepted

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

es = [0]
for c in S:
    es.append(es[-1] + int(c=='E'))

ans = N
for i in range(N):
    tmp = i - es[i] + es[N] - es[i+1]
    ans = min(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
def EW(x, char):
    re = 0
    if x == char:
        re = 1
    return re
        
def main(num, string):
    e = list(map(lambda x:EW(x, 'E'), string))
    w = list(map(lambda x:EW(x, 'W'), string))
    
    for i in range(1, num):
        w[i] = w[i-1] + w[i]
        e[num - i -1] = e[num - i  - 1] + e[num - i]
    
    w.insert(0, 0)
    w.insert(num+1,0)
    e.insert(0, 0)
    e.insert(num+1, 0)
    cnt = num
    for i in range(1, num + 1):
        tmp = w[i-1] + e[ i + 1]
        if tmp < cnt:
            cnt = tmp
    return cnt

num = int(input())
string = input()
 
print(main(num, string))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
    
total = []
_sum1 = []
_sum2 = []
if S[0] == "W":
    _sum1.append(1)
else:
    _sum1.append(0)
if S[-1] == "E":
    _sum2.append(1)
else:
    _sum2.append(0)

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

j = 0
for k in _sum2[::-1]:
    total.append(_sum1[j] + k)
    j += 1

print(min(total)-1)

Submission status: Accepted

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

Submission status: Accepted

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

def cal(data, r, c):
    for i in range(1,N):
        if data[i-1] == "W" and data[i] == "W":
            c += 1
        elif data[i-1] == "E" and data[i] == "E":
            c -= 1
            if r > c:
                r = c
    return r

if S[0] == "E":
    count = S.count("E") -1
    result = count
    result = cal(S, result, count)
else:
    count = S.count("E")
    result = count
    result =  cal(S, result, count)


print(result)

Submission status: Accepted

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

if S[0] == "E":
    count = S.count("E") -1
    result = count
    for i in range(1,N):
        if S[i-1] == "W" and S[i] == "W":
            count += 1
        elif S[i-1] == "E" and S[i] == "E":
            count -= 1
            if result > count:
                result = count
else:
    count = S.count("E")
    result = count
    for i in range(1,N):
        if S[i-1] == "W" and S[i] == "W":
            count += 1
        elif S[i-1] == "E" and S[i] == "E":
            count -= 1
            if result > count:
                result = count

print(result)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
ss=s[::-1]
ans=10**7
front_bucket=[0,0]
back_bucket=[0,0]

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

for i in range(n):
  if ss[i]=="E":
    front_bucket[0]-=1
    ans=min(ans, front_bucket[1]+back_bucket[0])
    back_bucket[0]+=1

  else:
    front_bucket[1]-=1    
    ans=min(ans, front_bucket[1]+back_bucket[0])
    back_bucket[1]+=1

print(ans)

Submission status: Accepted

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

#w[i]は, s[0]～s[i]に含まれるWの個数
w = [0] * n
bf = 0
#文字列sを順に調べて、Wの累積和を求める
for i in range(n):
    w[i] = bf
    if s[i] == 'W':
        w[i] += 1
    bf = w[i]

ans = n - 1
#リーダーをi番目とする
for i in range(n):
    #i番目より手前のW数とi+1番目以降のE数の和を最小にする
    myE = (n - w[n - 1]) - (i + 1 - w[i])#E数
    if i == 0:
        myW = 0#W数
    else:
        myW = w[i - 1]#W数
    ans = min(ans, myE + myW)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(raw_input())
S = raw_input()
l = [0] * N
r = [0] * N
w = 0
e = 0
for i in range(N-1):
  w += (1 if S[i]=="W" else 0)
  l[i+1] = w
  e += (1 if S[N-1-i]=="E" else 0)
  r[N-1-i-1] = e
ret = N
for i in range(N):
  ret = min(ret, l[i]+r[i])
print ret

Submission status: Accepted

Solution code:
import numpy
n = int(input())
s = input()
a = [0]*n
ans = 10**6
for i in range(n):
    if s[i] == 'W':
        a[i] = 1
res = numpy.cumsum(a)
for i in range(n):
    x = res[i]-str(a[i]).count('1')
    y = n-i-1-(res[n-1]-res[i])
    ans = min(ans,x+y)
print(ans)

Submission status: Accepted

Solution code:
s = ["", ""]

s[0] = input()
s[1] = input()

WtoE = []
wtoec = 0
EtoW = []
etowc = 0

for c in s[1]:
    if c == "W":
        wtoec = wtoec + 1
    WtoE.append(wtoec)

for c in s[1][::-1]:
    if c == "E":
        etowc = etowc + 1
    EtoW.append(etowc)
EtoW.reverse()

a = []
for i in range(0, int(s[0])):
    ans = WtoE[i] + EtoW[i]
    if WtoE[i] != 0 and s[1][i] == "W" or EtoW[i] != 0 and s[1][i] == "E":
        ans = ans - 1

    a.append(ans)

print(min(a))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def c_attention(N, S):
    count_east = [0] * N  # i番より西にいる人のうち西向きの人数
    count_west = [0] * N  # i番より東にいる人のうち東向きの人数

    for k in range(N - 2, -1, -1):
        count_east[k] = count_east[k + 1] + 1 \
            if S[k + 1] == 'E' else count_east[k + 1]
    for k in range(1, N):
        count_west[k] = count_west[k - 1] + 1 \
            if S[k - 1] == 'W' else count_west[k - 1]

    count = [0] * N  # i番は、i番の人を向いていない人数
    for k in range(N):
        count[k] = count_east[k] + count_west[k]
    ans = min(count)
    return ans

N = int(input())
S = input().strip()
print(c_attention(N, S))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

# AtCoder Beginner Contest
# Problem C

if __name__ == '__main__':
    n = int(input())
    s = input()
    count = 0
    ws = [0] * n
    es = [0] * n

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

    for i in range(len(s) - 1):
        if i == 0:
            count = es[-1] - es[1]
        else:
            count = min(count, (ws[i]) + (es[-1] - es[i + 1]))

    print(count)

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
e_cnt=s[1:].count("E")
cnt=e_cnt
for i in range(1,n):
    if s[i-1]=="W":
        e_cnt+=1
    if s[i]=="E":
        e_cnt-=1
    if e_cnt<cnt:
        cnt=e_cnt
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
x = []
if s[0]=="E": x.append(1)
else: x.append(0)
for i in range(1,n):
	if s[i]=="E": x.append(x[-1]+1)
	else: x.append(x[-1])
ans = x[-1]-x[0]
for i in range(1,n): ans = min(ans,x[-1]-x[i]+i-x[i-1])
print(ans)

Submission status: Accepted

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

change = S[1:].count('E')
min_ = change

for i in range(1, N):
    if S[i-1] == 'W':
        change += 1
    if S[i] == 'E':
        change -= 1
    if change < min_:
        min_ = change
print(min_)

Submission status: Accepted

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

w = [0] * (n + 1)
e = [0] * (n + 1)
a = [0] * n
for i, x in enumerate(s):
    if x == 'W':
        w[i+1] = w[i] + 1
        e[i+1] = e[i]
    else:
        w[i+1] = w[i]
        e[i+1] = e[i] - 1
    a[i] = w[i] + e[i+1]

print(n - w[-1] + min(a))

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
E, W = [0], [0]
for v in S:
    E.append(0)
    W.append(0)
    if v == "E" :
        E[-1] = 1
    else:
        W[-1] = 1
    E[-1] += E[-2]
    W[-1] += W[-2]
print(min(W[i] + E[N] - E[i+1]  for i in range(N)))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
point = [0]

def we_counter(pos):
  front = s[:pos]
  back = s[pos+1:]
  people = front.count('W') + back.count('E')
  return people


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

max_point = max(point)
pos = point.index(max_point)

print(we_counter(pos))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
t = []
x = 0
y = s.count('E')
for ch in s:
    if ch == 'E':
        y -= 1
    t.append(x+y)
    if ch == 'W':
        x += 1
print(min(t))

Submission status: Accepted

Solution code:
n=int(input())
s=input()
a=[0 for i in range(n)]
east,west =0,0
for i in range(n):
    a[i]+=west
    if s[i]=="W":
        west+=1
for i in range(n-1,-1,-1):
    a[i]+=east
    if s[i]=="E":
        east+=1
print(min(a))

Submission status: Accepted

Solution code:
from numpy import cumsum
n = int(input())
s = input()
def conv(i):
    if i == 'E': return 1
    return 0
s2 = [conv(i) for i in s]
cum = cumsum(s2)
ans = n - 1
b = cum[-1]
for i in range(n):
    a = cum[i]
    s1 = b - a
    s3 = (i+1) - cum[i]
    if s2[i] == 0:
        s3 -= 1
    ans = min(ans, s1+s3)
print(ans)

Submission status: Accepted

Solution code:
N = int(raw_input())
S = list(raw_input())
S = ['X']+S+['X']
D = [[0, 0]]
W = [0] * (N+2)
E = [0] * (N+2)
for i in range(1,N+2):
 W[i] = W[i-1]+(1 if S[i-1]== 'W' else 0)
for i in range(N+1)[::-1]:
 E[i] = E[i+1]+(1 if S[i+1]== 'E' else 0)
ans = N
for i in range(N):
  if W[i+1]+E[i+1] < ans:
   ans = W[i+1]+E[i+1]
print ans

Submission status: Accepted

Solution code:
n = int(input())
S = input()
W = 0
E = 0
T = [0 for i in range(n)]  
for i in range(n-1):
    if S[i] == "W": W+=1
    if i > 0:
        T[i+1] += W 

for i in range(n-1,0,-1):
    if S[i] == "E": E+=1
    if i < n-1:
        T[i-1] += E
print(min(T))