B - Ticket Gate Log
Source: atcoder
Difficulty: 250

Problem Statement
Takahashi aggregated usage records from ticket gates.
However, he accidentally erased some records of entering and exiting stations.
He is trying to restore the erased records.
You are given a string
S
consisting of
i
and
o
. We want to insert zero or more characters at arbitrary positions in
S
so that the resulting string satisfies the following conditions:
Its length is even, and every odd-numbered (1st, 3rd, ...) character is
i
while every even-numbered (2nd, 4th, ...) character is
o
.
Find the minimum number of characters that need to be inserted. It can be proved under the constraints of this problem that by inserting an appropriate finite number of characters,
S
can be made to satisfy the conditions.
Constraints
S
is a string of length between
1
and
100
, consisting of
i
and
o
.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
ioi
Sample Output 1
1
We can insert
o
after the 3rd character to form
ioio
to satisfy the conditions. The conditions cannot be satisfied by inserting zero or fewer characters.
Sample Input 2
iioo
Sample Output 2
2
We can insert
o
after the 1st character and
i
after the 3rd character to satisfy the conditions. The conditions cannot be satisfied by inserting one or fewer characters.
Sample Input 3
io
Sample Output 3
0
S
already satisfies the conditions.

C++ solution:
#include<bits/stdc++.h>
using namespace std;
#define int long long

signed main(){
    string s;
    cin >> s;
    int n = s.size();
    s = 'o' + s;
    int ans =0 ;
    for(int i = 1; i <= n; ++i){
        if(s[i] == s[i-1]) ans++;
    }
    if((n+ans)%2) ans++;
    cout << ans << '\n';
    
    return 0;
}

Go solution:
package main

import (
	"bufio"
	"fmt"
	"os"
)

var rdr = bufio.NewReader(os.Stdin)
var wtr = bufio.NewWriter(os.Stdout)

func main() {
	defer wtr.Flush()

	var S string
	fmt.Fscan(rdr, &S)

	op := 0
	for i := 0; i < len(S); {
		if S[i] != 'i' {
			op++ // insert 'i'
		} else {
			i++
		}
		if i >= len(S) || S[i] != 'o' {
			op++ // insert 'o'
		} else {
			i++
		}
	}
	puts(op)
}

// ==========

func readIntSlice(N int) []int {
	A := make([]int, N)
	for i, _ := range A {
		fmt.Fscan(rdr, &A[i])
	}
	return A
}

func puts(a ...any) {
	fmt.Fprintln(wtr, a...)
}

func printf(format string, a ...any) {
	fmt.Fprintf(wtr, format, a...)
}

func exit(i int) {
	wtr.Flush()
	os.Exit(i)
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

Java solution:
import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) {
        FastScanner sc = new FastScanner();

        // okay so count all the i's without o's after and all the o's without i's after and then count whether they're in the right place
        char[] s = sc.next().toCharArray();
        int cnt = 0;

        for (int i=0;i<s.length-1;i++) {
            if (s[i] == s[i+1]) cnt++;
        }

        if (s[0] != 'i') cnt++;
        if ((s.length + cnt) % 2 != 0) cnt++;

        System.out.println(cnt);
    }

    public static class FastScanner {

        BufferedReader br;
        StringTokenizer st;

        public FastScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}

JavaScript solution:
const main = (lines) => {
    const str = lines.trim();
    let cou =0;
    for(let i =-0; i <str.length-1; i++){
        if(str.slice(i,i+2)==="ii" || str.slice(i,i+2)==="oo"){
            cou++
        }
    }
    if(str.slice(0,1) === "o"){
        cou++
    }
    if(str.slice(-1) === "i"){
        cou++
    }
    console.log(cou)
}
main(require("fs").readFileSync("/dev/stdin","utf8"));

Python solution:
S = input()
ans = 0

if S[0] == "o":
  #print(f"1: S[0]={S[0]}")
  ans += 1
if S[-1] == "i":
  #print(f"2: S[-1]={S[-1]}")
  ans += 1
  
for i in range(len(S)-1):
  if S[i] == S[i+1]:
    ans += 1
    #print(f"3: S[{i}]={S[i]} == S[{i+1}]={S[i+1]}")
print(ans)

Ruby solution:
S = gets.strip

ans = 0
q = S.chars
if q.last != 'o'
  ans += 1
  q << 'o'
end
oe = 1
while (u = q.shift)
  case [u, oe]
  when ['o', 1]
    ans += 1
    q.unshift(u)
  when ['i', 0]
    ans += 1
    q.unshift(u)
  end
  oe = (oe + 1)%2
end
puts ans