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 in Java7 (OpenJDK 1.7.0) (judge verdict: Wrong Answer)


import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {

	public static void main(String[] args) {
		//		new Main().solveA();
		//		new Main().solveB();
		//		new Main().solveC();
		new Main().solveC2();
		// new Main().solveD();
	}

	private void solveA() {
		Scanner scanner = null;
		int numA = 0;
		int numB = 0;

		try {
			scanner = new Scanner(System.in);
			numA = scanner.nextInt();
			numB = scanner.nextInt();

			System.out.println(Math.max(numA + numB, Math.max(numA - numB, numA * numB)));

		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
	}

	private void solveB() {
		Scanner scanner = null;
		int numN = 0;
		String numS = "";

		try {
			scanner = new Scanner(System.in);
			numN = scanner.nextInt();
			numS = scanner.next();
			String numWk = "";
			int res = 0;
			numWk = numS;
			for (int i = numN - 1; i >= 0; i--) {
				char[] val1 = numWk.substring(0, i).toCharArray();
				char[] val2 = numWk.substring(i, numN).toCharArray();

				Set<Character> wkSet1 = new HashSet<Character>();
				for (int j = 0; j < val1.length; j++) {
					wkSet1.add(val1[j]);
				}

				Set<Character> wkSet2 = new HashSet<Character>();
				for (int j = 0; j < val2.length; j++) {
					wkSet2.add(val2[j]);
				}

				Set<Character> chkSet = null;
				Set<Character> resSet = null;
				if (wkSet1.size() > wkSet2.size()) {
					chkSet = wkSet1;
					resSet = wkSet2;
				} else {
					chkSet = wkSet2;
					resSet = wkSet1;
				}

				int cnt = 0;
				for (Character character : chkSet) {
					if (resSet.contains(character)) {
						cnt++;
					}
				}

				res = Math.max(res, cnt);
			}

			System.out.println(res);

		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
	}

	private void solveC() {
		Scanner scanner = null;
		int numN = 0;
		char[] numS;

		try {
			scanner = new Scanner(System.in);
			numN = scanner.nextInt();
			numS = scanner.next().toCharArray();

			int[] eArray = new int[numS.length];
			int[] wArray = new int[numS.length];
			int[] resArray = new int[numS.length];

			wArray[numN - 1] = 0;
			for (int i = numN - 2; i >= 0; i--) {
				if ('E' == numS[i + 1]) {
					wArray[i] = wArray[i + 1] + 1;
				} else {
					wArray[i] = wArray[i + 1];
				}
			}
			eArray[0] = 0;
			for (int i = 0; i < numN; i++) {
				if (i >= 1 && 'W' == numS[i - 1]) {
					eArray[i] = eArray[i - 1] + 1;
				} else if (i >= 1) {
					eArray[i] = eArray[i - 1];
				}
				resArray[i] = eArray[i] + wArray[i];
			}
			Arrays.sort(resArray);
			System.out.println(resArray[0]);

		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
	}

	private void solveC2() {
		Scanner scanner = null;
		int numN = 0;
		char[] numS;

		try {
			scanner = new Scanner(System.in);
			numN = scanner.nextInt();
			numS = scanner.next().toCharArray();

			int eCnt = 0;
			int wCnt = 0;
			for (int i = 0; i < numN; i++) {
				if ('E' == numS[i]) {
					eCnt++;
				}
			}
			int res = numN;
			for (int i = 0; i < numN; i++) {
				if (i != 0) {
					if ('W' == numS[i - 1]) {
						wCnt++;
					} else {
						eCnt--;
					}
				}
				res = Math.min(wCnt + eCnt, res);
			}
			System.out.println(res);

		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
	}

	private void solveD() {
		Scanner scanner = null;
		int numN = 0;
		int numK = 0;
		int numS = 0;

		try {
			scanner = new Scanner(System.in);
			numN = scanner.nextInt();

			System.out.println("");

		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
	}
}