Problem:
Score : 700 points

Problem Statement

You are given a string S consisting of a,b and c. Find the number of strings that can be possibly obtained by repeatedly performing the following operation zero or more times, modulo 998244353:

Choose an integer i such that 1\leq i\leq |S|-1 and the i-th and (i+1)-th characters in S are different. Replace each of the i-th and (i+1)-th characters in S with the character that differs from both of them (among a, b and c).

Constraints

2 \leq |S| \leq 2 × 10^5

S consists of a, b and c.

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of strings that can be possibly obtained by repeatedly performing the operation, modulo 998244353.

Sample Input 1

abc

Sample Output 1

3

abc, aaa and ccc can be obtained.

Sample Input 2

abbac

Sample Output 2

65

Sample Input 3

babacabac

Sample Output 3

6310

Sample Input 4

ababacbcacbacacbcbbcbbacbaccacbacbacba

Sample Output 4

148010497

Solution in Rust (1.15.1) (judge verdict: Accepted)

use std::collections::{BTreeSet, VecDeque};

const MOD: u64 = 998244353;
fn main() {
    let s = std::io::stdin();
    let mut sc = Scanner { stdin: s.lock() };
    let s = sc.chars();
    let n = s.len();

    if n <= 4 {
        println!("{}", naive(s));
        return;
    }

    if s.iter().all(|&c| c == s[0]) {
        println!("1");
        return;
    }

    let mut dp: Vec<Vec<Vec<u64>>> = vec![vec![vec![0; 2]; 3]; 3];
    dp[0][0][0] = 1;
    for i in 0..n {
        let mut next_dp = vec![vec![vec![0; 2]; 3]; 3];
        for cur in 0..3 {
            for remain in 0..3 {
                for next in 0..3 {
                    let same = if cur == next && i > 0 { 1 } else { 0 };
                    let next_remain = (remain + next) % 3;
                    next_dp[next][next_remain][same] += dp[cur][remain][0];
                    next_dp[next][next_remain][1] += dp[cur][remain][1];
                }
            }
        }

        for i in 0..3 {
            for j in 0..3 {
                for k in 0..2 {
                    next_dp[i][j][k] %= MOD;
                }
            }
        }
        dp = next_dp;
    }

    let remain: usize = s.iter().map(|&c| c as usize - 'a' as usize).sum::<usize>() % 3;
    let mut ans = dp[0][remain][1] + dp[1][remain][1] + dp[2][remain][1];
    if (1..n).all(|i| s[i] != s[i - 1]) {
        ans += 1;
    }
    println!("{}", ans % MOD);
}

fn naive(s: Vec<char>) -> usize {
    let mut set = BTreeSet::new();
    let mut q = VecDeque::new();
    q.push_back(s.clone());
    set.insert(s);
    while let Some(s) = q.pop_front() {
        for i in 1..s.len() {
            if s[i - 1] == s[i] {
                continue;
            }
            let c = match (s[i - 1], s[i]) {
                ('a', 'b') | ('b', 'a') => 'c',
                ('a', 'c') | ('c', 'a') => 'b',
                ('b', 'c') | ('c', 'b') => 'a',
                _ => unreachable!(),
            };
            let mut t = s.clone();
            t[i - 1] = c;
            t[i] = c;
            if !set.contains(&t) {
                set.insert(t.clone());
                q.push_back(t);
            }
        }
    }
    set.len()
}

pub struct Scanner<R> {
    stdin: R,
}

impl<R: std::io::Read> Scanner<R> {
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .stdin
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}