Problem:
Score : 300 points

Problem Statement

There are N stones arranged in a row. Every stone is painted white or black.
A string S represents the color of the stones. The i-th stone from the left is white if the i-th character of S is ., and the stone is black if the character is #.

Takahashi wants to change the colors of some stones to black or white so that there will be no white stone immediately to the right of a black stone.
Find the minimum number of stones that needs to be recolored.

Constraints

1 \leq N \leq 2\times 10^5

S is a string of length N consisting of . and #.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the minimum number of stones that needs to be recolored.

Sample Input 1

3
#.#

Sample Output 1

1

It is enough to change the color of the first stone to white.

Sample Input 2

5
#.##.

Sample Output 2

2

Sample Input 3

9
.........

Sample Output 3

0

Solution in Rust (1.15.1) (judge verdict: Time Limit Exceeded)

use std::io::Read;
use std::cmp::min;

fn main() {
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();

    let answer = solve(&buf);

    println!("{}", answer);
}

fn solve(input: &str) -> String {
    let mut iterator = input.split_whitespace();

    let n: usize = iterator.next().unwrap().parse().unwrap();
    let mut s: Vec<char> = iterator.next().unwrap().chars().collect();
    while !s.is_empty() && *s.first().unwrap() == '.' {
        s.remove(0);
    }
    while !s.is_empty() && *s.last().unwrap() == '#' {
        s.pop().unwrap();
    }

    let n: usize = s.len();
    if n == 0 {
        return "0".to_string();
    }

    let mut s1 = s.clone();
    let mut c1: usize = 0;
    for i in 0..(n - 1) {
        if s1[i] == '#' && s1[i + 1] == '.' {
            s1[i + 1] = '#';
            c1 += 1;
        }
    }

    let mut s2 = s.clone();
    let mut c2: usize = 0;
    for i in (1..n).rev() {
        if s2[i - 1] == '#' && s2[i] == '.' {
            s2[i - 1] = '.';
            c2 += 1;
        }
    }

    let mut ans = min(c1, c2);

    let mut b: Vec<usize> = vec![0; n];
    b[0] = 1;
    for i in 1..n {
        if s[i] == '#' {
            b[i] = b[i - 1] + 1;
        } else {
            b[i] = b[i - 1];
        }
    }
    let mut w: Vec<usize> = vec![0; n];
    w[n - 1] = 1;
    for i in (0..(n - 1)).rev() {
        if s[i] == '.' {
            w[i] = w[i + 1] + 1;
        } else {
            w[i] = w[i + 1];
        }
    }

    for i in 0..(n - 1) {
        if s[i] == '.' && s[i + 1] == '#' {
            ans = min(ans, b[i] + w[i + 1]);
        }
    }

    return ans.to_string();
}