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

Wnum = []
Wnum.append(0)
for c in S:
        if c == 'W':
                Wnum.append(Wnum[-1]+1)
        else:
                Wnum.append(Wnum[-1])

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

ans = N
for i in range(N):
        ans_temp = Wnum[i]+Enum[i]
        if ans_temp < ans:
                ans = ans_temp

print(ans)

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

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

m = n
turn = s[1:].count('E')
m = min(turn, m)
for i in range(1, n):
    if s[i-1] == 'W': turn += 1
    if s[i] == 'E': turn -= 1

    m = min(turn, m)

print(m)

Solution (Python):
def main():
    n = int(input())
    s = list(input())
    w = 0
    e = 0
    r = [0]*n
    r[0] = s[1:].count('E')
    for i in range(n-1):
        r[i+1] = r[i]
        if s[i] == 'W':
            r[i+1] += 1
        if s[i+1] == 'E':
            r[i+1] -= 1
    print(min(r))
main()

Solution (Python):
from collections import Counter
n = int(input())
s = list(input())
num_w = 0
num_e = Counter(s[1:])['E']
ans = num_e
for i in range(1, n):
    if s[i-1] == 'W':
        num_w += 1
    if s[i] == 'E':
        num_e -= 1
    ans = min(ans, num_w + num_e)
print(ans)

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

    print(resolve(s))

def resolve(s):
    
    total_east = 0
    total_west = 0

    for c in s:
        if c == 'E':
            total_east += 1
        else:
            total_west += 1
    
    ww = 0
    ee = total_east
    
    m = len(s) - 1
    for i, c in enumerate(s):
        if c == 'E':
            ee -= 1

        x = ee + ww
        m = min(m, x)

        if c == 'W':
            ww += 1
    
    return m


main()

Solution (Python):

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

a = []
t = 0
for i,s in enumerate(S):
    if s == "E":
        t += 1
        a.append(t)
    else:
        t -= 1
        a.append(t)

index = a.index(max(a))

X = S[:index]
Y = S[index+1:]

print(X.count("W")+Y.count("E"))

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

X = ""
Y = S[1:]

leader = S[0]
num_x = 0
num_y = Y.count("E")

num = num_y

for i in range(1, N):
    
    X_new = leader
    
    if X_new == "W":
        num_x += 1
    
    leader = S[i]
    
    if leader == "E": 
        num_y -= 1
    
    num = min(num, num_x+num_y)
    
print(num)

Solution (Python):


import math
import sys
import itertools
import numpy as np

n =int(input())
s = list(input())
a=[]
k = 0
for i in range(1,n,1):
    if s[i] == 'E':
        k = k+1
a.append(k)

for i in range(1,n,1):
    if s[i] == 'E' and s[i-1] == 'E':
        a.append(a[i-1]-1)
    elif s[i] == 'E' and s[i-1] == 'W':
        a.append(a[i-1])
    elif s[i] == 'W' and s[i-1] == 'E':
        a.append(a[i-1])
    else :
        a.append(a[i-1]+1)

print(min(a))

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

for i in range(n):
    if S[i] == "E":
        s[i] = 1
min = n

count = 0
for j in range(n):
    i = 0
    if i < j and s[j] == 1:
        count += 1
    elif i > j and s[j] == 0:
        count += 1
if count < min:
    min = count

for i in range(1, n):
    if s[i-1] == 0:
        count += 1
    if s[i] == 1:
        count -= 1
    if count < min:
        min = count
print(min)

Solution (Python):
n = int(input())
s = list(input())
w_l = [0]*(n+1)
e_l = [0]*(n+1)
ans = float('Inf')
for i in range(0, len(s)):
  if s[i] == 'W':
    w_l[i+1] = w_l[i] + 1
    e_l[i+1] = e_l[i]
  else:
    e_l[i+1] = e_l[i] + 1
    w_l[i+1] = w_l[i]

for i in range(len(s)):
  ans = min(ans, e_l[-1] - e_l[i-1] + w_l[i-1])
print(ans)

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

dpL=[0 for _ in range(N)]
dpR=[0 for _ in range(N)]

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

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

ans=10**6
for i in range(N):
  ans=min(ans,dpR[i]+dpL[i])
  
print(ans)

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

a = []
count = 0

for i in range(n):
    if s[i] == "W":
        a.append(count)
    else:
        count += 1
        a.append(count)

num = 10**6

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


print(num)

Solution (Python):
n = int(input())
s = input()
ans = n
c = s[1:]
d = c.count('E')
ans = d
count = d
for i in range(1,n):
    a = s[i-1]
    b = s[i]
    if a == 'W' and b == 'W':
        count += 1
    elif a == 'E' and b == 'E':
        count -= 1
    ans = min(ans,count)
print(ans)

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

w = [0] * n
for i in range(n-1):
  w[i+1] = w[i] + int(s[i] == 'W')
    
    
e = [0] * n
for i in reversed(range(1, n)):
  e[i-1] = e[i] + int(s[i] == 'E')
    
print(min(ww+ee for ww, ee in zip(w, e)))

Solution (Python):
def c_attention(N, S):
    count_lookme = S.count('W')
    if S[0] == 'W':
        count_lookme -= 1
    ans = N - count_lookme - 1
    for k in range(1, N):
        if S[k] == 'W':
            count_lookme -= 1
        if S[k - 1] == 'E':
            count_lookme += 1
        ans = min(ans, N - count_lookme - 1)
    return ans

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

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

w_fwd, e_fwd, w_bwd, e_bwd = 0, 0, 0, 0
res = len(S)
for c in S:
    if c == 'W':
        w_fwd += 1
    else:
        e_fwd += 1

for c in S:
    if c == 'W':
        w_fwd -= 1
        res = min(res, w_bwd + e_fwd)
        w_bwd += 1
    else:
        e_fwd -= 1
        res = min(res, w_bwd + e_fwd)
        e_bwd += 1

print(res)

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


count_list = []

prevous_count = s[1:].count("E")
current_min = prevous_count

for index in range(len(s) - 1):
    add_man = s[index]
    leader = s[index+1]

    if add_man == "W":
        prevous_count += 1
    if leader == "E":
        prevous_count -= 1
    if current_min > prevous_count:
        current_min = prevous_count


print(current_min)

Solution (Python):
def main():
    n, s = int(input()), input()

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

    p_lst = []
    for i in range(n):
        p_lst.append(w[i]+e[n]-e[i+1])

    print(min(p_lst))


if __name__ == '__main__':
    main()

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

    east = [0] * (N + 1)
    west = east[:]
    for i, s in enumerate(S, start=1):
        if s == 'E':
            east[i] += east[i-1] + 1
            west[i] += west[i-1]
        else:
            east[i] += east[i-1]
            west[i] += west[i-1] + 1

    people = float('inf')
    for i in range(1, N + 1):
        people = min(people, west[i-1] + east[-1] - east[i])
    print(people)

main()

Solution (Python):
N = int(input())
S = input()
res = 1000000
prev_W = 0
prev_E = S.count('E')
for i in range(N):
    if i >= 1: prev_W += S[i-1].count('W')
    prev_E -= S[i].count('E')
    res = min(res,prev_W + prev_E)
print(res)

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

forward_cost = []

cost = 0
for i in range(len(S)):
  if S[i] == "W":
    cost = cost + 1
  forward_cost.append(cost)

cost = 0
SR = S[::-1]
for i in range(len(S)):
  if SR[i] == "E":
    cost = cost + 1
  forward_cost[len(S)-i-1] = forward_cost[len(S)-i-1] + cost


print(min(forward_cost)-1)

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

Solution (Python):
N = int(input())
S = input()
a = S[1:]
hazime = a.count("E")
tei = a.count("E")
for i in range(N-1):
  a = S[i]
  b = S[i+1]
  if a == "E" and b == "E":
    hazime += -1
    tei = min(tei,hazime)
  elif a == "E" and b == "W":
        continue
        tei = min(tei,hazime)
  elif a == "W" and b == "E":
     continue
     tei = min(tei,hazime)
  elif a == "W" and b == "W":
       hazime += 1
       tei = min(tei,hazime)
b = S[:N-1]
go = b.count("W")
tei = min(tei,go)
print(tei)

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

e_count = S.count('E')
w_count = S.count('W')





left_w = 0
right_e = e_count
min_cost = len(S)-1

for i in range(len(S)):
    if i > 0:
        if S[i-1] == 'W':
            left_w += 1
    if S[i] == 'E':
        right_e -= 1
    cost = left_w + right_e
    if cost < min_cost:
        min_cost = cost

print(min_cost)

Solution (Python):
N = int(input())
S = list(input())
w = 0
e = 0

lw = 0
le = 0

for i in range(len(S)):
    if S[i] == "W":
        w+= 1
    else:
        e+= 1

min = 100000000
for i in range(len(S)):
    temp = 0
    if S[i] == "W":
        temp = lw+e
        lw+=1
        w-=1
    else:
        temp = lw+e
        le +=1
        e -= 1
        temp-=1
    if temp<min:
        min = temp

print(min)

Solution (Python):
n=int(input())
s = input().rstrip()
numw,nume=0,0
if 'W' in s:
  numw = s.count('W')
  nume = n - numw
else:
  print(0)
  exit()
donew,donee=0,0
result = pow(10,6)
for i in range(n):
  if s[i] == 'W':
    numw -= 1
  else:
    nume -= 1
  result = min(result,(nume+donew))
  if s[i] == 'W':
    donew += 1
  else:
    donee += 1
print(result)

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

W = []
w_count = 0
for s in S:
    W.append(w_count)
    if s == "W":
        w_count += 1

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

mi = N
for i in range(N):
    mi = min(mi, E[i] + W[i])

print(mi)

Solution (Python):
def main():
    N = int(input())
    S = input()
    n_E = [0 for _ in range(N+1)]
    n_W = [0 for _ in range(N+1)]
    for i in range(N):
        if S[i] == "E":
            n_E[i+1] = n_E[i] + 1
            n_W[i+1] = n_W[i]
        elif S[i] == "W":
            n_E[i+1] = n_E[i]
            n_W[i+1] = n_W[i] + 1

    count = N
    for i in range(N):
        count = min(count,n_W[i] + (n_E[-1]-n_E[i+1]))

    return count

if __name__ == "__main__":
    print(main())

Solution (Python):
n=int(input())
s=list(input())
cost=s[1:].count("E")
min_cost=cost

for i in range(n-1):
    if s[i]=="W":
        cost+=1
    if s[i+1]=="E":
        cost-=1
    if min_cost>cost:
        min_cost=cost
    
print(min_cost)

Solution (Python):
from collections import Counter
N = int(input())
S = input()

west_side = Counter()
east_side = Counter(S)


min_num = 1000000
for k in range(N):
    tmp = S[k]

    if tmp == "E":
        east_side["E"] -= 1
    else:
        east_side["W"] -= 1
    num_of_change_vector = east_side["E"] + west_side["W"]
    if min_num > num_of_change_vector:
        min_num = num_of_change_vector

    if tmp == "E":
        west_side["E"] += 1
    else:
        west_side["W"] += 1

print(min_num)

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

count = s[1:].count("E")
e = s[1:].count("E")
w = s[1:].count("W")
temp = []
temp.append(count)

for i in range(1,len(s)):
    count = temp[-1]
    if s[i-1] == "W":
        count += 1
    if s[i]=="E":
        count -= 1
    temp.append(count)

print(min(temp))

"""
n = input()
s = input()
l = int(n)
count = 0
for i in range(1,l):
    s1 = set(s[:i])
    s2 = set(s[i:])
    count = max(count,len(s1 & s2))
print(count)




"""

Solution (Python):
from itertools import accumulate as AC
n = int(input())
s = input()
    

w = [0]*n
e = [0]*n

for i in range(n):
    if s[i] == 'W':
        w[i] = 1
    else:
        e[i] = 1
        
aw = list(AC(w))
ae = list(AC(e))

ans = 1000000

for i in range(n):
    if i == 0:
        current = ae[n-1]-ae[0]
    elif 0 < i < n-1:
        current = aw[i-1]+ae[n-1]-ae[i]
    else:
        current = aw[i-1]
        
    if ans > current:
        ans = current
        

     
print(ans)

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


count_E = [0]
count_W = [0]

for s in S:
    if s == 'E':
        count_E.append(count_E[-1] + 1)
        count_W.append(count_W[-1])
    else:
        count_E.append(count_E[-1])
        count_W.append(count_W[-1] + 1)

ans = 10**6

for i in range(N):
    rotate_left = count_W[i]
    rotate_right = count_E[-1] - count_E[i+1]
    total = rotate_left + rotate_right
    if total < ans:
        ans = total

print(ans)

Solution (Python):
n = int(input())
s = input()
l = []
count = s[1:].count('E')
r = count
for i in range(n-1):
  if s[i] == 'W':
    count += 1
  if s[i + 1] == 'E':
    count -= 1
    if count < r:
      r = count
print(r)

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

e, w = 0, 0
for si in s:
    if si == 'E':
        e += 1
    else:
        w += 1

pri_e, pri_w= 0, 0
cnt_max = 0
for i in range(n):
    cnt = 0
    if s[i] == 'W':
        w -= 1
    
    cnt += pri_e
    cnt += w
    cnt_max = max(cnt, cnt_max)
    if s[i] == 'E':
        e -= 1
        pri_e += 1

print(n - (cnt_max+1))

Solution (Python):
from sys import stdin


def main():
    N = int(stdin.readline().rstrip())
    S = stdin.readline()
    count = 0
    s1 = []
    for i, x in enumerate(S):
        if x == 'W':
            count += 1
        s1.append(count)
    count = 0
    s2 = []
    for i, x in enumerate(S[::-1]):
        if x == 'E':
            count += 1
        s2.append(count)
    s2.reverse()

    result = N
    for i in range(N):
        count = 0
        idx = i - 1
        if idx >= 0:
            count += s1[idx]
        idx = i + 1
        if idx < N:
            count += s2[idx]
        result = min(result, count)

    print(result)


if __name__ == '__main__':
    main()

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


right = 0
left = 0




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

        sum = right + left
        if sum < min: min = sum

print(min)

Solution (Python):
n = int(input())
s = input()
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":
        a = 1
    else:
        a = 0
    E[i] = E[i - 1] + a
    W[i] = W[i - 1] + 1 - a

num = float("inf")
for i in range(n):
    if i == 0:
        num = min(num, E[n - 1] - E[i])
    elif i == n - 1:
        num = min(num, W[i - 1])
    else:
        num = min(num, E[n - 1] - E[i] + W[i - 1])

print(num)

Solution (Python):
N = int(input())
S = input()
result = S[1:].count("E")
cost = result
l0 = S[0]
for n in range(1, N):
    l1 = S[n]
    if l0 != l1:
        l0 = l1
        continue
    if l0 == "W":
        cost += 1
        continue
    cost -= 1
    if cost < result:
        result = cost

print(result)

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

ans = 10**6
e_list, w_list = [0], [0]
for i in range(n):
    if s[i] == 'E':
        e_list.append(e_list[i]+1)
        w_list.append(w_list[i])
    elif s[i] == 'W':
        w_list.append(w_list[i]+1)
        e_list.append(e_list[i])
e_list.append(e_list[n])
w_list.append((w_list[n]))

for leader in range(1, n+1):
    num = w_list[leader-1] + e_list[n+1]-e_list[leader]
    if num < ans:
        ans = num

print(ans)

Solution (Python):

N = int(input())

direction = input()

ruiseki_W = [0]
ruiseki_E = [0]
num_W = 0
num_E = 0
ans = 1145141919
for i in range(N):
	if direction[i] == 'W':
		num_W += 1
	else:
		num_E += 1
	ruiseki_W.append(num_W)
	ruiseki_E.append(num_E)
	
for i in range(N):
	ans = min(ans,ruiseki_W[i]+(ruiseki_E[N] - ruiseki_E[i+1]))
	

print(ans)

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

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

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


    mincount=min(count,mincount)

print(mincount)

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

n = S[1:].count("E")
ans = n
for i, s in enumerate(S):
    if i == 0:
        continue
    if s == "E":
        n -= 1
    if S[i-1] == "W":
        n += 1
    ans = min(ans, n)

print(ans)

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

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

print(min(a))

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

l=[ 0 for i in range(N)]
r=[ 0 for i in range(N)]

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

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


ans=float("inf")
for i,j in zip(l,r):
	ans=min(ans,i+j)
print ans

Solution (Python):
N = int(raw_input())
S = list(raw_input())

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

print(min_ans)

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

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

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

print(ans)

Solution (Python):
n=int(input())
s=input()
w=s.count('W')
a=[0]*n
for i in range(n):
    p=1 if s[i]=='W' else 0
    if i==0:a[0]=p
    else:a[i] = a[i-1]+p
r=min(min(n-i-1-w+2*a[i]for i in range(n)),n-w)
print(r)

Solution (Python):
N = int(input())
S = list(input())
left = [0]*N
for i in range(1,N):
    if S[i-1] == 'W':
        left[i] = left[i-1]+1
    else:
        left[i] = left[i-1]
right = [0]*N
for i in range(N-1)[::-1]:
    if S[i+1] == 'E':
        right[i] = right[i+1]+1
    else:
        right[i] = right[i+1]
leftright = [left[i]+right[i] for i in range(N)]
print(min(leftright))

Solution (Python):
n = int(input())
s = input()
ans = []
for i in range(n):
    if i == 0:
        counter = 0
        for k in range(1,n):
            if s[k] == 'E':
                counter += 1
        ans.append(counter)
    else:
        counter = ans[i-1]
        if s[i-1] == 'W' and s[i] == 'W':
            counter += 1
            ans.append(counter)
        elif s[i-1] == 'E' and s[i] == "E":
            counter += -1
            ans.append(counter)
        else:
            ans.append(counter)
print(min(ans))

Solution (Python):
n = int(input())
s = input()
dp = [float("inf") for i in range(n)]
dp[0] = 0
for i in range(1,n):
    if s[i] == "E":
        dp[0] += 1
for i in range(1,n):
    dp[i] = dp[i-1]
    if s[i-1] == "W":
        dp[i] += 1
    if s[i] == "E":
        dp[i] -= 1
print(min(dp))

Solution (Python):
from collections import defaultdict as dd
from collections import Counter
from pprint import pprint as pp

YN = {True: 'Yes', False: 'No'}
ri = lambda: int(input())
ria = lambda: list(map(int, input().split()))
rian = lambda n: [ria() for _ in range(n)]
rs = lambda: input()
rsa = lambda: input().split()
rsn = lambda n: [rs() for _ in range(n)]

N = ri()
S = input()
cnt = {'E': [0, S.count('E')],
       'W': [0, S.count('W')]}

ret = N
for c in S:
  cnt[c][1] -= 1
  ret = min(ret, cnt['W'][0] + cnt['E'][1])
  cnt[c][0] += 1
print(ret)

Solution (Python):
N = int(input())
S = input()
pos = 0
minpos = 0
mini = 0
for i in range(N):
    if S[i]=="W":
        pos += 1
    else:
        pos -= 1
    if pos < minpos:
        minpos = pos
        mini = i

s1 = S[0:mini].count("W")
s2 = S[mini+1:N].count("E")
print(s1+s2)

Solution (Python):
INF = 10 ** 20
n = int(input())
s = input()

w_cum = [0]
e_cum = [0]

w_acc = 0
e_acc = 0
for i in range(n):
  if s[i] == "W":
    w_acc += 1
  else:
    e_acc += 1
  w_cum.append(w_acc)
  e_cum.append(e_acc)

ans = INF
for i in range(1, n + 1):
  ans = min(ans, w_cum[i - 1] + e_cum[-1] - e_cum[i])

print(ans)

Solution (Python):
n = int(input())
S = list(map(lambda x: 1 if x=='E' else 0, list(input())))

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

S.insert(0, 0)
ans = n
for i in range(1, n+1):
    c = i-S[i-1]-1
    
    c += S[-1]-S[i]
    ans = min(ans, c)
print(ans)