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())
people = input()
mini = n
for i in range(1,n+1):
    if i ==1:
        left_e = people[:(i-1)].count("E")
        left_w = people[:(i-1)].count("W")
        right_e = people[i:].count("E")
        right_w = people[i:].count("W")
    else:
        if people[i-1] =="E" and people[i-2] =="W":
            right_e = right_e-1
            left_w = left_w+1
        elif people[i-1] =="E" and people[i-2] =="E":
            left_e = left_e +1
            right_e = right_e -1
        elif people[i-1] == "W" and people[i-2] == "E":
            left_e = left_e+1
            right_w = right_w -1
        else:
            left_w = left_w+1
            right_w = right_w -1
    if left_w+right_e < mini:
        mini = left_w+right_e
    
print(mini)

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


left = [0] * N
right = [0] * N

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

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

ans = 300010
for i in range(0,N):
    if i == 0:
        ans = min(ans, right[i+1])
    elif i == N -1:
        ans = min(ans, left[i-1])
    else:
        ans = min(ans, left[i-1] + right[i+1])


print(ans)

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

w = [0]
e = [0]


tmp_e = 0

for i in range(1, n):

    j = (n - 1) - i

    if (s[i-1] == 'W'):
        w.append(w[i-1] + 1)
        
    else:
        w.append(w[i-1])

    if (s[j+1] == 'E'):
        e.append(tmp_e + 1)
        tmp_e += 1
    else:
        e.append(tmp_e)

e.reverse()

ans = 10 ** 6 
for i in range(n):
    ans = w[i] + e[i] if w[i] + e[i] < ans else ans

print(ans)

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

w = [0]
e = [0]

tmp_w = 0
tmp_e = 0

for i in range(1, n):

    j = (n - 1) - i

    if (s[i-1] == 'W'):
        w.append(tmp_w + 1)
        tmp_w += 1
    else:
        w.append(tmp_w)

    if (s[j+1] == 'E'):
        e.append(tmp_e + 1)
        tmp_e += 1
    else:
        e.append(tmp_e)

e.reverse()





result = []

for i in range(n):
    result.append(w[i] + e[i])

print(min(result))

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

w = [0]
e = [0]

tmp_w = 0
tmp_e = 0

for i in range(1, n):

    j = (n - 1) - i

    if (s[i-1] == 'W'):
        w.append(tmp_w + 1)
        tmp_w += 1
    else:
        w.append(tmp_w)

    if (s[j+1] == 'E'):
        e.append(tmp_e + 1)
        tmp_e += 1
    else:
        e.append(tmp_e)

e.reverse()

ans = 10 ** 6 
for i in range(n):
    ans = w[i] + e[i] if w[i] + e[i] < ans else ans

print(ans)

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

Solution (Python):

import string
import sys
from itertools import chain, dropwhile, takewhile


def read(
    *shape, f=int, it=chain.from_iterable(sys.stdin), whitespaces=set(string.whitespace)
):
    def read_word():
        w = lambda c: c in whitespaces
        nw = lambda c: c not in whitespaces
        return f("".join(takewhile(nw, dropwhile(w, it))))

    if not shape:
        return read_word()
    elif len(shape) == 1:
        return [read_word() for _ in range(shape[0])]
    elif len(shape) == 2:
        return [[read_word() for _ in range(shape[1])] for _ in range(shape[0])]


def readi(*shape):
    return read(*shape)


def readi1(*shape):
    return [i - 1 for i in read(*shape)]


def readf(*shape):
    return read(*shape, f=float)


def reads(*shape):
    return read(*shape, f=str)


def arr(*shape, fill_value=0):
    if len(shape) == 1:
        return [fill_value] * shape[fill_value]
    elif len(shape) == 2:
        return [[fill_value] * shape[1] for _ in range(shape[0])]


def dbg(**kwargs):
    print(
        ", ".join("{} = {}".format(k, repr(v)) for k, v in kwargs.items()),
        file=sys.stderr,
    )


def main():
    N = readi()
    a = reads()
    e = arr(N + 1)
    w = arr(N + 1)
    for i in range(N):
        e[i + 1] = e[i] + (1 if a[i] == "E" else 0)
        w[i + 1] = w[i] + (1 if a[i] == "W" else 0)

    dbg(e=e)
    dbg(w=w)

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

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
S = input()
E_count = 0
W_count = 0
lis = []
for s in S:
    lis.append(W_count)
    if s == 'W':
        W_count += 1
for index, s in enumerate(S[::-1]):
    lis[N-1-index] += E_count
    if s == 'E':
        E_count += 1
minimam = 3*10**5
for count in lis:
    minimam = min(minimam, count)
print(minimam)

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

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

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

        ans = min(ans, new)
        old = new

    print(ans)

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

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

Solution (Python):
N=int(input())
A=input()
R=[0]
for i in range(N):
    R.append(R[i]+(A[i]=="W"))
print(min([R[i]+(N-i-1)-(R[-1]-R[i+1]) for i in range(N)]))

Solution (Python):

import sys


def solve(N: int, S: str):

    wc = 0
    ec = S.count("E") 

    min_res = float("inf")
    for s in S:
        if s == "E":
            ec -= 1
        min_res = min(min_res,wc+ec)
        if s == "W":
            wc += 1

    print(min_res)
    

    



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    S = next(tokens)  
    solve(N, S)

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
S = list(input())
temp = S[1:].count('E')
ans = [temp]

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

print(min(ans))

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n=I()
  s=list(S())

  l1=[]
  l2=[]

  a=s[0]
  if a=='W':
    l1.append([1,0])
  else:
    l1.append([0,1])

  for i in range(1,len(s[1:])+1):
    if s[i]=='W':
      l1.append([l1[i-1][0]+1,l1[i-1][1]])
    else:
      l1.append([l1[i-1][0],l1[i-1][1]+1])

  _s=s[:]
  _s.reverse()

  a=_s[0]
  if a=='W':
    l2.append([1,0])
  else:
    l2.append([0,1])

  for i in range(1,len(_s[1:])+1):
    if _s[i]=='W':
      l2.append([l2[i-1][0]+1,l2[i-1][1]])
    else:
      l2.append([l2[i-1][0],l2[i-1][1]+1])

  l2.reverse()

  
  

  mn=inf
  for i in range(n):
    a=s[i]
    if a=='W':
      l1[i][0]-=1
      l2[i][0]-=1
    else:
      l1[i][1]-=1
      l2[i][1]-=1
    mn=min(mn,l1[i][0]+l2[i][1])

  return mn

print(main())

Solution (Python):


N = int(input())
S = input()
cnt = [0] * (N+1)
cnt[0] = 0
ans = N

for i in range(N):
    cnt[i+1] = cnt[i]
    if S[i] == 'W':
        cnt[i+1] += 1

for j in range(1, N+1):
    ans = min(ans, cnt[j-1] + (N - j) - (cnt[N]-cnt[j]))

print(ans)

Solution (Python):
N = input()
S = input()
cnt = S.count('E')
ans = cnt

for v in S:
  if v == 'E':
    cnt -= 1
  else:
    cnt += 1
  ans = min(ans, cnt)
print(ans)

Solution (Python):
N = int(input())
S = input()
lis = []
Wcounter = 0
Ecounter = 0
for s in S:
    lis.append(Ecounter)
    if s == 'E':
        Ecounter += 1
for index, s in enumerate(S[::-1]):
    lis[N - 1 - index] += Wcounter
    if s == 'W':
        Wcounter += 1
max_num = 0
for num in lis:
    max_num = max(max_num, num)
print(N-1-max_num)

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

num_w=0
num_e=s[1:].count('E')
ans=num_e
for i in range(n):
	if i == 0:
		continue
	if s[i]=='E':
		num_e-=1
	if s[i-1]=='W':
		num_w+=1
	ans=min(ans, num_w+num_e)
print(ans)

Solution (Python):
n,s=open(0)
n=int(n)
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)))

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

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

Solution (Python):
N = int(input())
S = input()
e = [0]
w = [0]
for l, r in zip(S[:N-1], S[::-1]):
  nw = w[-1] + (1 if l == 'W' else 0)
  ne = e[-1] + (1 if r == 'E' else 0)
  e.append(ne)
  w.append(nw)
print(min([a + b for a, b in zip(e, w[::-1])]))

Solution (Python):
n=int(input())
s=input()
e_count=[0]
w_count=[0]
for i in range(n):
    if s[i]=='E':
        e_count.append(e_count[i]+1)
    else:
        e_count.append(e_count[i])
    if s[n-1-i]=='W':
        w_count.append(w_count[i]+1)
    else:
        w_count.append(w_count[i])
Min=n
for i in range(n):
    a=i-e_count[i]+n-i-1-w_count[n-1-i]
    if a<Min:
        Min=a
print(Min)

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

turn_cnt = []
east_cnt = 0
total_east = arr.count("E")

for i in range(n):
    left_turn = i - east_cnt
    s = arr[i]
    if s == "E":
        east_cnt += 1
    right_turn = total_east - east_cnt
    turn_cnt.append(left_turn + right_turn)

print(min(turn_cnt))

Solution (Python):
import sys, os

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    n = f()[0]
    s = input()

    lefte= [0] * n
    leftw = [0] * n
    righte = [0] * n
    rightw = [0] * n




    for i in range(n):
        lefte[i] = lefte[i-1] + (s[i] == 'E') if i>=1 else int(s[i] == 'E')
        leftw[i] = leftw[i-1] + (s[i] == 'W') if i>=1 else int(s[i] == 'W')

    for i in range(n-1, -1, -1):
        righte[i] = righte[i+1] + (s[i] == 'E') if i<n-1 else int(s[i] == 'E')
        rightw[i] = rightw[i+1] + (s[i] == 'W') if i<n-1 else int(s[i] == 'W')

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

    print(ans)


solve()

Solution (Python):

import sys


def solve(N: int, S: str):
    W = 0
    E = 0
    for c in S:
        if c == 'W':
            W += 1
        else:
            E += 1
    w, e = 0, 0
    ret = float('inf')
    for i in range(N):
        tmp = w + E - e
        if S[i] == 'W':
            w += 1
        else:
            e += 1
            tmp -= 1
        ret = min(ret, tmp)
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    S = next(tokens)  
    solve(N, S)

if __name__ == '__main__':
    main()

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

lstE = [0] * (N+2)
lstW = [0] * (N+2)

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

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

ans = N
for k in range(1,N+1):
    ans = min(ans, lstE[k-1]+lstW[k+1])

print (ans)

Solution (Python):


from sys import stdout
from bisect import bisect_left as binl
from copy import copy, deepcopy
from collections import defaultdict


mod = 1


def intin():
    input_tuple = input().split()
    if len(input_tuple) <= 1:
        return int(input_tuple[0])
    return tuple(map(int, input_tuple))


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


def intinl(count):
    return [intin() for _ in range(count)]


def modadd(x, y):
    global mod
    return (x + y) % mod


def modmlt(x, y):
    global mod
    return (x * y) % mod


def lcm(x, y):
    while y != 0:
        z = x % y
        x = y
        y = z
    return x


def combination(x, y):
    assert(x >= y)

    if y > x // 2:
        y = x - y

    ret = 1

    for i in range(0, y):
        j = x - i
        i = i + 1

        ret = ret * j
        ret = ret // i

    return ret


def get_divisors(x):
    retlist = []
    for i in range(1, int(x**0.5) + 3):
        if x % i == 0:
            retlist.append(i)
            retlist.append(x // i)
    return retlist


def get_factors(x):
    retlist = []
    for i in range(2, int(x**0.5) + 3):
        while x % i == 0:
            retlist.append(i)
            x = x // i
    retlist.append(x)
    return retlist


def make_linklist(xylist):
    linklist = {}
    for a, b in xylist:
        linklist.setdefault(a, [])
        linklist.setdefault(b, [])
        linklist[a].append(b)
        linklist[b].append(a)
    return linklist


def calc_longest_distance(linklist, v=1):
    distance_list = {}
    distance_count = 0
    distance = 0
    vlist_previous = []
    vlist = [v]
    nodecount = len(linklist)

    while distance_count < nodecount:
        vlist_next = []
        for v in vlist:
            distance_list[v] = distance
            distance_count += 1
            vlist_next.extend(linklist[v])
        distance += 1
        vlist_to_del = vlist_previous
        vlist_previous = vlist
        vlist = list(set(vlist_next) - set(vlist_to_del))

    max_distance = -1
    max_v = None
    for v, distance in distance_list.items():
        if distance > max_distance:
            max_distance = distance
            max_v = v

    return (max_distance, max_v)


def calc_tree_diameter(linklist, v=1):
    _, u = calc_longest_distance(linklist, v)
    distance, _ = calc_longest_distance(linklist, u)
    return distance





def main():
    n = intin()
    s = input()

    now = 0
    for c in s[1:]:
        if c == 'E':
            now += 1

    ans = now

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

    print(ans)


if __name__ == '__main__':
    main()

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

west = [0 for _ in range(N+1)]
east = [0 for _ in range(N+1)]

for i in range(N):
    west[i+1] = west[i]
    east[i+1] = east[i]
    if S[i] == 'W':
        west[i+1] += 1
    else:
        east[i+1] += 1

west = west[1:]
east = east[1:]

turn_nums = [0 for _ in range(N)]
turn_nums[0] = east[N-1] - east[0]
turn_nums[N-1] = west[N-2]
for i in range(1, N-1):
    turn_nums[i] = west[i-1] + east[N-1] - east[i]

print(min(turn_nums))

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

left = [0]*n
for i,c in enumerate(s):
    if i == n-1:
        continue
    left[i+1] = left[i] + ( 1 if c == "W" else 0 )

right = [0] * n
for i,c in reversed(list(enumerate(s))):
    if i == 0:
        continue
    right[i-1] = right[i] + ( 1 if c == "E" else 0 )

min_cost=99999999999999999999

for i in range(n):
    min_cost = min(min_cost, left[i] + right[i])

print(min_cost)

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')


def A():
    return


def B():
    return


def C():
    n = II()
    s = S()
    dp = [0 for i in range(n)]
    for num,si in enumerate(s):
        if si == "E":
            dp[num] = dp[num-1]+1
        else:
            dp[num] = dp[num - 1]
    ans = float("INF")
    a = max(dp)
    for i in range(n):
        ans = min(ans,  a - dp[i] + i - dp[i] + (s[i] == "E"))
    
    print(ans)
    return


def D():
    return


def E():
    return


def F():
    return


def G():
    return


def H():
    return


if __name__ == '__main__':
    C()

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

leader = S[0]
right = 0
left = 0

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

result = right

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

print(N-result-1)

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

leader = S[0]
right = 0
left = 0

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

result = right

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

print(N-result-1)

Solution (Python):
N=int(input())
S=input()
S="E"+S+"W"
E=[0]*(N+2)
W=[0]*(N+2)
for i in range(len(S)-1):
    if S[i]=="W":
        E[i+1]=E[i]+1
    else:
        E[i+1]=E[i]

for i in range(len(S)-1,0,-1):
    if S[i]=="E":
         W[i-1]=W[i]+1
    else:
         W[i-1]=W[i]
ans=10**9
for i in range(N):
    ans=min(ans,E[i+1]+W[i+1])
print(ans)

Solution (Python):
N=int(input())
S=input()
S="E"+S+"W"
E=[0]*(N+2)
W=[0]*(N+2)
for i in range(len(S)-1):
    if S[i]=="W":
        E[i+1]=E[i]+1
    else:
        E[i+1]=E[i]

for i in range(len(S)-1,0,-1):
    if S[i]=="E":
         W[i-1]=W[i]+1
    else:
         W[i-1]=W[i]
ans=10**9
for i in range(N):
    ans=min(ans,E[i+1]+W[i+1])
print(ans)

Solution (Python):
from collections import defaultdict
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())

N = int(input())
S = input()
Esum = [0]
tmp = 0
for s in S:
    if s == 'E':
        tmp += 1
    Esum.append(tmp)


E = Esum[-1]
ans = INF
for i in range(N):
    tmp = 0
    leftW = i - Esum[i]
    rightW = E - Esum[i+1]
    tmp = leftW + rightW
    ans = min(ans,tmp)
print(ans)

Solution (Python):
def main():
    num = int(input())
    data = list(input())
    soppo = data.count('E')

    ans = num

    for i in range(num):
        now_person = data[i]
        kari = soppo
        if now_person == 'E':
            kari -= 1

        if kari < ans:
            ans = kari

        if now_person == 'E':
            soppo -= 1
        else:
            soppo += 1

    print(ans)



if __name__ == '__main__':
    main()

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



leftSide = 0
rigthSide = S[1:].count("E")



minChange = rigthSide
crntCnt = rigthSide
for i in range(1, len(S)):
  if S[i-1] == "W":
    crntCnt += 1
  if S[i] == "E":
    crntCnt -= 1
  if crntCnt < minChange:
    minChange = crntCnt
print(minChange)

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

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

cnt_E = [0] * (N+2)
cnt_W = [0] * (N+2)

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

ans = 10**7
for i in range(N):
    total = cnt_W[i] + cnt_E[i+2]
    if ans > total:
        ans = total
        
print(ans)

Solution (Python):


def read_input():
    n = int(input())
    s = input()

    return n, s

def submit():
    n, s = read_input()

    
    westside_count = [0, 0]
    eastside_count = [len([c for c in s if c == 'W']), len([c for c in s if c == 'E'])]
    min_flip = n
    for i in range(n):
        leader = s[i]

        
        if leader == 'W':
            eastside_count[0] -= 1
        else:
            eastside_count[1] -= 1

        
        flip = westside_count[0] + eastside_count[1]
        if flip < min_flip:
            min_flip = flip

        
        if leader == 'W':
            westside_count[0] += 1
        else:
            westside_count[1] += 1

    print(min_flip)

if __name__ == '__main__':
    submit()

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

ruiseki_w = [0]
ruiseki = 0
for i in s[:-1]:
    if i == 'W':
        ruiseki += 1
        ruiseki_w.append(ruiseki)
    else:
        ruiseki_w.append(ruiseki)

ruiseki_e = [0]
ruiseki = 0
for i in s[::-1][:-1]:
    if i == 'E':
        ruiseki += 1
        ruiseki_e.append(ruiseki)
    else:
        ruiseki_e.append(ruiseki)
ruiseki_e.reverse()
        
ninzu = [ruiseki_w[i] + ruiseki_e[i] for i in range(n)]
print(min(ninzu))

Solution (Python):
def solve():
    N = int(raw_input())
    S = raw_input() 
    count = 0
    for i in range(1, N): 
        if S[i] == 'E':
            count += 1
    min_need_change = count
    for i in range(1,N): 
        if S[i] == 'E':
            count -= 1
        if S[i - 1] == 'W':
            count += 1
        if count < min_need_change:
            min_need_change = count
    print str(min_need_change)

if __name__ == '__main__':
    solve()

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

ruisekiwa = [0]
ans = n

for i in s:
    if i == "W":
        ruisekiwa.append(ruisekiwa[-1]+1)
    else:
        ruisekiwa.append(ruisekiwa[-1])
        
for i in range(1,n+1):
    w = ruisekiwa[i-1]
    e = n-i-(ruisekiwa[-1]-ruisekiwa[i])
    ans = min(ans,w+e)
    
print(ans)

Solution (Python):
def main(input):
    N = int(input())
    S = input().rstrip()
    ansE = [0] * N
    ansW = [0] * N

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

    if S[-1] == "W":
        ansW[-1] += 1
    for i in range(N-2, -1, -1):
        if S[i] == "W":
            ansW[i] = ansW[i+1] + 1
        else:
            ansW[i] = ansW[i+1]
    ans = N - max(x + y for x, y in zip(ansE, ansW))
    print(ans)


import sys; sys.setrecursionlimit(50000)
if __name__ == "__main__":
    if len(sys.argv) > 1 and sys.argv[1] == "local":
        with open("stdin.txt") as stdin:
            main(stdin.readline)
    else:
        main(sys.stdin.readline)

Solution (Python):
N = int(input())
S = 'E' + input() + 'W'
rcnt = []
lcnt = []

prercnt = 0
prelcnt = 0
for i in range(N+2): 
  if S[i] == 'W': prelcnt += 1
  else: prercnt += 1
  rcnt.append(prercnt)
  lcnt.append(prelcnt)
    

print(min([lcnt[i-1] + rcnt[-1] - rcnt[i] for i in range(1, N+1)]))

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

Solution (Python):

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

E = [0 for i in range(n)]
W = [0 for i in range(n)]

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

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

print(ans)

Solution (Python):
from collections import Counter, deque
from fractions import gcd
from functools import lru_cache
from functools import reduce
import functools
import heapq
import itertools
import math
import numpy as np
import sys

sys.setrecursionlimit(10000)
INF = float('inf')

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

ws = []
cnt = 0
for c in S:
    if c == 'W':
        cnt += 1
    ws.append(cnt)

es = []
cnt = 0
for c in reversed(S):
    if c == 'E':
        cnt += 1
    es.append(cnt)
es.reverse()

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

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

count = 0
for i in range(1, N):
    if people[i] == 'E':
        count += 1

counts = [count]
for i in range(1, N):
    if people[i - 1] == 'W':
        count += 1
    if people[i]  == 'E':
        count -= 1
    counts.append(count)

print(min(counts))

Solution (Python):

import sys


def solve(N: int, S: str):
    ws = 0
    es = 0
    for s in S:
        ws += int(s == 'W')
        es += int(s == 'E')

    ce = 0
    cw = 0
    ret = N
    for s in S:
        ce += int(s == 'E')
        ret = min(ret, cw + (es - ce), )
        cw += int(s == 'W')
    print(ret)





def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    S = next(tokens)  
    solve(N, S)

if __name__ == '__main__':
    main()

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

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

x = [0]*n
temp = 0
for i in range(n):
    if s[i] == 'W':
        temp += 1
    x[i] = temp

ans = n - 1 - (x[-1] - x[0])
ans = min(ans, x[-2])

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

print(ans)

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

s = input()


a = [0] * (n+1)
b = [0] * (n+1)
ans = 3 * 10 ** 5

aN = s.count("E")
for i in range(n):
  if s[i] == "E":
    a[i+1] = a[i] + 1
    b[i+1] = b[i]
  else:
    a[i+1] = a[i]
    b[i+1] = b[i] + 1
  
  ans = min(ans, aN-a[i+1]+b[i])
  
  
  
print((ans))

Solution (Python):
def main():
	N = int(input())
	S = input()
	dpe = [0]*(N+1)
	dpw = [0]*(N+1)

	for i in range(N):
		if S[i] == "E" and i > 0:
			dpe[i-1] += 1
		elif S[i] == "W" and i < N-1:
			dpw[i+1] += 1

	for i in range(N-1, 0, -1):
		dpe[i-1] += dpe[i]

	for i in range(N-1):
		dpw[i+1] += dpw[i]

	ans = 3*(10**5)
	for i in range(N):
		ans = min(ans, dpe[i]+dpw[i])

	print(ans)

if __name__ == '__main__':
    main()

Solution (Python):

import sys
from itertools import accumulate
INF = float("inf")


def solve(N: int, S: str):
    ss = [1 if c == "E" else 0 for c in S]
    ss = list(accumulate(ss))

    mi, m = -1, INF
    for i in range(N):
        b = 0
        if i - 1 >= 0:
            b += i - ss[i-1]
        if i+1 <= N-1:
            b += ss[N-1]-ss[i]
        
        if b < m:
            m = b
            mi = i
    print(m)
    

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    S = next(tokens)  
    solve(N, S)


if __name__ == '__main__':
    main()

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

x=[0]*N

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

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

print(min(x))

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

W = [0]

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

answer = W[-1]
for i in range(1, N+1):
    answer = min(answer, W[i-1] + N - i - (W[-1] - W[i]))

print(answer)

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

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

print(minans)

Solution (Python):

import sys


def solve(N: int, S: str):
    eCount = [0]
    wCount = [0]
    for i in range(N):
        ce = S[i]
        cw = S[i]
        eCount.append(eCount[i] + (1 if ce == "W" else 0))
        wCount.append(wCount[i] + (1 if cw == "E" else 0))
    result = 10 ** 18
    for i in range(N):
        result = min(result, eCount[i] - wCount[i+1] + wCount[-1])
    print(result)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    S = next(tokens)  
    solve(N, S)


if __name__ == '__main__':
    main()

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


e = [0]
w = [0]

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

	e.append(cnt_e)
	w.append(cnt_w)

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

print(ans)

Solution (Python):
n = int(input())
s = input()
west = [ 0 for i in range(n+1) ]
east = [ 0 for i in range(n+1) ]
for i in range(1,n+1):
    west[i] = west[i-1]
    east[i] = east[i-1]
    if s[i-1] == 'W':
        west[i] += 1
    if s[i-1] == 'E':
        east[i] += 1

ans = 1e9
for i in range(1,n+1):
    reqd_east_cnt = i-1
    got_east_cnt = east[i-1]
    reqd_west_cnt = n - i
    got_west_cnt = west[n] - west[i]
    
    ans = min( ans, reqd_east_cnt - got_east_cnt + reqd_west_cnt - got_west_cnt)
print(ans)