Problem:


Score : 400 points

Problem Statement

We have a board with a 2 \times N grid.
Snuke covered the board with N dominoes without overlaps.
Here, a domino can cover a 1 \times 2 or 2 \times 1 square.

Then, Snuke decided to paint these dominoes using three colors: red, cyan and green.
Two dominoes that are adjacent by side should be painted by different colors.
Here, it is not always necessary to use all three colors.

Find the number of such ways to paint the dominoes, modulo 1000000007.

The arrangement of the dominoes is given to you as two strings S_1 and S_2 in the following manner:

Each domino is represented by a different English letter (lowercase or uppercase).

The j-th character in S_i represents the domino that occupies the square at the i-th row from the top and j-th column from the left.

Constraints

1 \leq N \leq 52

|S_1| = |S_2| = N

S_1 and S_2 consist of lowercase and uppercase English letters.

S_1 and S_2 represent a valid arrangement of dominoes.

Input

Input is given from Standard Input in the following format:

N
S_1
S_2

Output

Print the number of such ways to paint the dominoes, modulo 1000000007.

Sample Input 1

3
aab
ccb

Sample Output 1

6

There are six ways as shown below:

Sample Input 2

1
Z
Z

Sample Output 2

3

Note that it is not always necessary to use all the colors.

Sample Input 3

52
RvvttdWIyyPPQFFZZssffEEkkaSSDKqcibbeYrhAljCCGGJppHHn
RLLwwdWIxxNNQUUXXVVMMooBBaggDKqcimmeYrhAljOOTTJuuzzn

Sample Output 3

958681902



Sample input:
3
aab
ccb


Sample output:
6

Submission status: Accepted

Solution code:
n = int(input())
s1 = input()
s2 = input()

count = 0
i = 0
if s1[0] == s2[0]:
    i += 1
    count = 3
else:
    count = 6
    i += 2
while i < n:
    if s1[i - 1] == s2[i - 1]:
        if s1[i] == s2[i]:
            count *= 2
            i += 1
        else:
            count *= 2
            i += 2
    else:
        if s1[i] == s2[i]:
            i += 1
        else:
            count *= 3
            i += 2
print(count % (10 ** 9 + 7))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

MOD = 1000000007

N = int(input())
S1 = input()
S2 = input()

ptns = []
i = 0
# パターンXとYに分けて区切る
while i < N:
    if S1[i] == S2[i]:
        ptns.append('X')
        i += 1
    else:
        ptns.append('Y')
        i += 2

if ptns[0] == 'X':
    ans = 3
elif ptns[0] == 'Y':
    ans = 6
for i in range(1, len(ptns)):
    # XとYの並びを4つパターン毎に処理
    if ptns[i-1] == 'X' and ptns[i] == 'X':
        ans *= 2
    elif ptns[i-1] == 'X' and ptns[i] == 'Y':
        ans *= 2
    elif ptns[i-1] == 'Y' and ptns[i] == 'X':
        ans *= 1
    elif ptns[i-1] == 'Y' and ptns[i] == 'Y':
        ans *= 3
    ans %= MOD
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
s1 = input()
s2 = input()
mod = 10 ** 9 + 7

i = 0
prev = -1
ans = 1
while i < N:
    if s1[i] == s2[i]:
        if prev == 1:
            ans *= 1
        elif prev == 0:
            ans *= 2
        else:
            ans *= 3
        prev = 0
    else:
        if prev == 0:
            ans *= 2
        elif prev == 1:
            ans *= 3
        else:
            ans *= 6
        i += 1
        prev = 1
    i += 1

print(ans % mod)

Submission status: Accepted

Solution code:
N = int(input())
S1 = input()
S2 = input()
mod = 10 ** 9 + 7

# 番兵
S1 += '*'
P = []
i = 0
while i < N:
    if S1[i] == S1[i + 1]:
        P.append('横')
        i += 1
    else:
        P.append('縦')
    i += 1

ans = 0
if P[0] == '縦':
    ans = 3
    one_before = '縦'
elif P[0] == '横':
    ans = 6
    one_before = '横'

for p in P[1:]:
    if p == '縦' and one_before == '縦':
        ans = ans * 2 % mod
        one_before = p

    elif p == '縦' and one_before == '横':
        one_before = p

    elif p == '横' and one_before == '縦':
        ans = ans * 2 % mod
        one_before = p

    elif p == '横' and one_before == '横':
        ans = ans * 3 % mod
        one_before = p

print(ans)

Submission status: Accepted

Solution code:
Mod = 10**9+7
def f(s,t):
  if s[-1] == t[-1]:
    if len(s) == 1:
      return 3
    elif s[-2] == t[-2]:
      return 2*f(s[:-1],t[:-1])%Mod
    else:
      return 1*f(s[:-1],t[:-1])%Mod
  else:
    if len(s) == 2:
      return 6
    elif s[-3] == t[-3]:
      return 2*f(s[:-2],t[:-2])%Mod
    else:
      return 3*f(s[:-2],t[:-2])%Mod

n = int(input())
x = input()
y = input()
print(f(x,y))

Submission status: Accepted

Solution code:
N = int(input())
s1 = input()
s2 = input()

MOD = 10 ** 9 + 7

ans = 1
i = 0
if s1[0] == s2[0]:
    ans *= 3
    i += 1
elif s1[0] == s1[1] and s2[0] == s2[1]:
    ans *= 3 * 2
    i += 2

t = (s1[0] == s2[0])  # 直前が縦
while i < N:
    if t:
        ans *= 2
        ans %= MOD
    else:
        if i + 1 < N:
            if s1[i] == s1[i + 1] and s2[i] == s2[i + 1]:
                ans *= 3
                ans %= MOD
    t = (s1[i] == s2[i])
    i += 1 if t else 2

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
S1 = list(input())
S2 = list(input())

MOD = 10**9 + 7
ans = []
skip = False
pre_tate = False
for u,l in zip(S1,S2):
    if skip == True:
        skip = False
        continue

    if len(ans) == 0:
        if u == l:
            ans.append(3)
            pre_tate = True
        else:
            ans.append(6)
            skip = True
            pre_tate = False
    else:
        if u == l:
            if pre_tate:
                ans.append(2)
            else:
                ans.append(1)
            pre_tate = True
        else:
            if pre_tate:
                ans.append(2)
            else:
                ans.append(3)
            skip = True
            pre_tate = False

tmp = 1
for a in ans:
    tmp *= a

print(tmp%MOD)

Submission status: Accepted

Solution code:
N = int(input())
S1 = input()
S2 = input()
dp_v = 0
dp_h = 0
mod = 1000000007
is_first = True
while S1:
    if len(S1) > 1 and S1[0] == S1[1]:
        S1 = S1[2:]
        if is_first:
            dp_h = 6
            dp_v = 0
            is_first = False
        else:
            dp_h = dp_v*2+dp_h*3
            dp_v = 0
    else:
        S1 = S1[1:]
        if is_first:
            dp_v = 3
            dp_h = 0
            is_first = False
        else:
            dp_v = dp_v*2+dp_h
            dp_h = 0
    dp_v %= mod
    dp_h %= mod
print((dp_h+dp_v) % mod)

Submission status: Accepted

Solution code:
N=int(input())
d=list(input())
do=""
if N==1:
  print(3)
  exit()
if d[0]==d[1]:
  ans=6
  flag=0
  do+=d[0]*2+","
else:
  ans=3
  flag=1
  do+=d[0]+","
for i in range(1,N-1):
  if flag==1:
    if d[i]==d[i+1]:
      ans*=2
      flag=0
      do+=d[i]*2+","
    else:
      ans*=2
      flag=1
      do+=d[i]+","
  elif flag==2:
    if d[i]==d[i+1]:
      ans*=3
      flag=0
      do+=d[i]*2+","
    else:
      ans*=1
      flag=1
      do+=d[i]+","
  elif flag==0:
    flag=2
if flag==1:
  ans*=2
  do+=d[N-1]
elif flag==2:
  ans*=1
  do+=d[N-1]
  
print(ans%(10**9+7))

Submission status: Accepted

Solution code:
N = int(input())
MOD = 10**9+7
u,d = input(),input()
ans,i = 0,0
if u[0]==d[0]:
    ans = 3
    i = 1
else:
    ans = 6
    i = 2
while i<N:
    if u[i-1]==d[i-1]:
        ans *= 2
        if u[i]==d[i]:
            i += 1
        else:
            i += 2
    else:
        if u[i]==d[i]:
            ans *= 1
            i += 1
        else:
            ans *= 3
            i += 2
print(ans%MOD)

Submission status: Accepted

Solution code:
n = int(input())
s1 = input()
s2 = input()
mod = 10 ** 9 + 7
count = 1
p = 0
for i in range(n):
    if i == 0:
        if s1[i] == s2[i]:
            count *= 3
        else:
            count *= 6
            p = 1
    elif p == 0:
        if s1[i - 1] == s2[i - 1] and s1[i] != s2[i]:
            count *= 2
            p = 1
        elif s1[i - 1] == s2[i - 1] and s1[i] == s2[i]:
            count *= 2
        elif s1[i - 1] != s2[i - 1] and s1[i] == s2[i]:
            count *= 1
        elif s1[i - 1] != s2[i - 1] and s1[i] != s2[i]:
            count *= 3
            p = 1
    elif p == 1:
        p = 0
print(count % mod)

Submission status: Accepted

Solution code:
ans=1
n=int(input())
s=input()
t=input()
m=False
if s[0]==t[0]:
    ans*=3
    flag=True
else:
    ans*=6
    flag=False
    m=True
for i in range(1,n):
    if m==True:
        m=False
        continue
    if flag==True and s[i]==t[i]:
        ans*=2
        continue
    if flag==True and s[i]!=t[i]:
        ans*=2
        m=True
        flag=False
        continue
    if flag==False and s[i]==t[i]:
        flag=True
        continue
    if flag==False and s[i]!=t[i]:
        ans*=3
        m=True
        continue
print(ans%(10**9+7))

Submission status: Accepted

Solution code:
n=int(input())
s1=input()
s2=input()
if s1[0]!=s2[0]:
    ans=6
else:
    ans=3
mod=10**9+7
for i in range(1,n):
    if s1[i]==s1[i-1]:
        continue
    elif s1[i]==s2[i] and s1[i-1]==s2[i-1]:
        ans*=2
    elif s1[i]==s2[i] and s1[i-1]!=s2[i-1]:
        ans*=1
    elif s1[i]!=s2[i] and s1[i-1]==s2[i-1]:
        ans*=2
    else:
        ans*=3
print(ans%mod)

Submission status: Accepted

Solution code:
n = int(input())
s = [list(input()) for _ in range(2)]
MOD = 10 ** 9 + 7

if n == 1:
    print(3)
    exit()

ans = 0
p = 0
b = 0
if s[0][0] == s[1][0]:
    ans = 3
    p = 1
    b = 1
else:
    ans = 6
    p = 2
    b = 2

while p < n:
    if s[0][p] == s[1][p]:
        if b == 1:
            ans *= 2
            ans %= MOD
        else:
            b = 1
        p += 1
    else:
        if b == 1:
            ans *= 2
            ans %= MOD
            b = 2
        else:
            ans *= 3
            ans %= MOD
        p += 2

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S1 = input()
S2 = input()
mod = 10 ** 9 + 7

X = []
i = 0
while i < N:
    if i + 1 < N and S1[i] == S1[i + 1]:
        X.append("Y")
        i += 2
    else:
        X.append("T")
        i += 1


if X[0] == "Y":
    ans = 6
    before = "Y"
elif X[0] == "T":
    ans = 3
    before = "T"

for x in X[1:]:
    if before == "Y" and x == "Y":
        ans = ans * 3 % mod
        before = "Y"

    elif before == "Y" and x == "T":
        ans = ans * 1 % mod
        before = "T"

    elif before == "T" and x == "Y":
        ans = ans * 2 % mod
        before = "Y"

    elif before == "T" and x == "T":
        ans = ans * 2 % mod
        before = "T"

print(ans % mod)

Submission status: Accepted

Solution code:
n=int(input())
a=input()
b=input()
mod=10**9+7
x=[]
j=True
for i in range(n):
    if a[i]!=b[i]:
        if j:
            x.append(2)
            j=False
        else:
            j=True
    else:
        x.append(1)
res=1
j=0
for t in x:
    if t==1:
        if j==0:
            res*=3
        elif j==1:
            res*=2
        else:
            res*=1
        j=1
    else:
        if j==0:
            res*=6
        elif j==1:
            res*=2
        else:
            res*=3
        j=2
print(res%mod)

Submission status: Accepted

Solution code:
N = int(input())
s1 = input()
s2 = input()
mod = 1000000007
ans = 1
tate = True
if s1[0]==s2[0]:
    ans *= 3
    s1 = s1[1:]
else:
    ans *= 6
    s1 = s1[2:]
    tate = False
while len(s1) > 1:
    if s1[0] == s1[1]:
        s1 = s1[2:]
        if tate:
            ans *= 2
        else:
            ans *= 3
        ans %= mod
        tate = False
    else:
        if tate:
            ans *= 2
            ans %= mod
        s1 = s1[1:]
        tate = True
if tate and len(s1) == 1:
    ans *= 2
    ans %= mod
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = []
for i in range(2):
    S.append(input())
mod = 7 + 10**9

if S[0][0] == S[1][0]: #縦置き
    ans, pre = 3, "v"
    i = 1
else: #横置き
    ans, pre = 6, "w"
    i = 2

while i < N:
    if S[0][i] == S[1][i]: #縦置き
        if pre == "v": #左が縦置き
            ans *= 2
            ans %= mod
        else: #左が横置き
            pre = "v"
        i += 1
    else: #横置き
        if pre == "v": #左が縦置き
            ans *= 2
            ans %= mod
            pre = "w"
        else: #左が横置き
            ans *= 3
            ans %= mod
        i += 2

print(ans)

Submission status: Accepted

Solution code:
N=int(input())

tmp=''
arr=[]
for x in input()+'0':
    if tmp =='':
        tmp=x
        continue
    if tmp == x:
        tmp=''
        arr.append(2)
    else:
        tmp=x
        arr.append(1)

tmp=arr.pop(0)
ans=tmp*3
for x in arr:
    if tmp == 1:
        ans*=2
    else:
        if x==2:
            ans*=3
    tmp=x
    ans%=1000000007
print(ans)

Submission status: Accepted

Solution code:
 # -*- coding: utf-8 -*-
"""
Created on Sun Sep 30 11:46:57 2018
ABC071D
@author: maezawa
"""

n = int(input())
s1 = input()
s2 = input()
mod = 10**9+7

comb = 1
adj = 0
i = 0
while i < n:
    if i < n-1 and s1[i] == s1[i+1]:
        if adj == 2:
            comb *= 3
        else:
            comb *= 3 - adj
            comb *= 3 - adj -1
        i += 2
        adj = 2
    else:
        comb *= 3 - adj
        i += 1
        adj = 1

print(comb%mod)

Submission status: Accepted

Solution code:
N = int(input())
S = input()+"1"
T = input()+"1"
U = ""

k = 0
while k < N:
    if S[k] == S[k+1]:
        U = U + "Y"
        k += 2
    else:
        U = U + "T"
        k += 1

if U[0] == "Y":
    ans = 6
else:
    ans = 3

mod = 1000000007
U = "1" + U
for k in range(2,len(U)):
    if U[k] == U[k-1] == "T":
        ans *= 2
        ans %= mod
    elif U[k-1] == "Y" and U[k] == "T":
        ans *= 1
        ans %= mod
    elif U[k-1] == "T" and U[k] == "Y":
        ans *= 2
        ans %= mod
    elif U[k-1] == "Y" and U[k] == "Y":
        ans *= 3
        ans %= mod

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s1 = input()
s2 = input()

mod = 10**9 + 7

# dp[i][j] : i番目まで見たときに、i番目の要素が j = 0 だと縦に、
# j = 1 だと横にブロックが並んでいた時の色の塗りかた (mod 10**9+7)
dp = [[0 for j in range(2)] for i in range(n+1)]

if s1[0] == s2[0]:
    dp[0][0] = 3
    i = 0
else:
    dp[1][1] = 3 * 2
    i = 1
while i < n - 1:
    if s1[i] == s2[i]:
        if s1[i+1] == s2[i+1]:
            dp[i+1][0] = dp[i][0] * 2 % mod
            i += 1
        else:
            dp[i+2][1] = dp[i][0] * 2 % mod
            i += 2
    else:
        if s1[i+1] == s2[i+1]:
            dp[i+1][0] = dp[i][1] % mod
            i += 1
        else:
            dp[i+2][1] = dp[i][1] * 3 % mod
            i += 2

print((dp[n-1][0] % mod + dp[n-1][1] % mod) % mod)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

MOD = 10**9+7

# X,Yに変換
n = ni()
s1 = lc()
s2 = lc()

xy = []

cnt = 0
while cnt < n:
    if s1[cnt] == s2[cnt]:
        xy.append("x")
        cnt += 1
    else:
        xy.append("y")
        cnt += 2
        
ans = 0

if xy[0] == "x":
    ans = 3
else:
    ans = 6
   
    
for pr,nx in zip(xy[:-1], xy[1:]):
    if pr == "x" and nx == "x":
        ans = (ans * 2) % MOD
        
    elif pr == "x" and nx == "y":
        ans = (ans * 2) % MOD
        
    elif pr == "y" and nx == "y":
        ans = (ans * 3) % MOD
        
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = input()
B = input()

ptn = []
i = 0
while i < N:
    if A[i] == B[i]:
        ptn.append(1)
        i += 1
    else:
        ptn.append(2)
        i += 2

MOD = 10**9+7
ans = 3 * ptn[0]
for a,b in zip(ptn,ptn[1:]):
    if a == 1 and b == 1:
        ans *= 2
    elif a == 1 and b == 2:
        ans *= 2
    elif a == 2 and b == 2:
        ans *= 3
    ans %= MOD
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]

def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def main():
    N = int(input())
    S = [input(), input()]

    if S[0][0] == S[1][0]:
        ans = 3
        i = 1
        pre = 0
    else:
        ans = 6
        i = 2
        pre = 1

    while i < N:
        if S[0][i] == S[1][i]:
            if pre == 0:
                ans *= 2
            else:
                ans *= 1

            i += 1
            pre = 0
        else:
            if pre == 0:
                ans *= 2
            else:
                ans *= 3
            i += 2
            pre = 1

        ans %= 1000000007
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
mod = 10 ** 9 + 7
n = int(input())
s1 = input()
s2 = input()
pre = 0
i = 0
while i < len(s1):
    if pre == 0:
        if s1[i] == s2[i]:
            num = 3
            i += 1
            pre = 1
        else:
            num = 6
            i += 2
            pre = 2
    elif pre == 1:
        if s1[i] == s2[i]:
            num *= 2
            num %= mod
            i += 1
            pre = 1
        else:
            num *= 2
            num %= mod
            i += 2
            pre = 2
    else:
        if s1[i] == s2[i]:
            i += 1
            pre = 1
        else:
            num *= 3
            num %= mod
            i += 2
            pre = 2

print(num)

Submission status: Accepted

Solution code:
N = int(input())
S1 = input()
S2 = input()
p = 10 ** 9 + 7
dp = [0] * N
if S1[0] == S2[0]:
  dp[0] = 3
else:
  dp[0] = 6
for i in range(1, N):
  if S1[i] == S1[i - 1]:
    dp[i] = dp[i - 1]
  elif S1[i - 1] == S2[i - 1]:
    dp[i] = dp[i - 1] * 2 % p
  elif S1[i] == S2[i]:
    dp[i] = dp[i - 1]
  else:
    dp[i] = dp[i - 1] * 3 % p
print(dp[-1])

Submission status: Accepted

Solution code:
N = int(input())
S1 = input()
S2 = input()
if S1[0] == S2[0]:
    count = 3
    start_idx = 1
    prev = 'v'
else:
    count = 6
    start_idx = 2
    prev = 'h'

skip = False

for i in range(start_idx, len(S1)):
    if skip:
        skip = False
        continue
    if S1[i] == S2[i]:  # current = v
        if prev == 'v':
            count *= 2
            prev = 'v'
        else:
            count *= 1
        prev = 'v'
    else:               # current = h
        if prev == 'v':
            count *= 2
        else:
            count *= 3
        prev = 'h'
        skip = True
print(count % (10**9 + 7))

Submission status: Accepted

Solution code:
N = int(input())
S1 = input()
S2 = input()

mod = 10 ** 9 + 7

i, cnt = 0, 0
prev = ''
if S1[i] == S2[i]:
    cnt += 3
    prev = '|'
    i += 1
else:
    cnt += 6
    prev = '='
    i += 2

while i < N:
    if S1[i] == S2[i]:
        currnet = '|'
        i += 1
    else:
        currnet = '='
        i += 2
    if prev == '|':
        cnt *= 2        
    elif prev == '=' and currnet == '=':
        cnt *= 3
    cnt %= mod
    prev = currnet
print(cnt % mod)

Submission status: Accepted

Solution code:
MOD = 10**9+7
n = int(input())
s1 = input()
s2 = input()

i = 0
ans = 1
pre_state = None
while i < n:
    if s1[i] == s2[i]:
        if pre_state == 'same':
            ans = (ans*2)%MOD
        elif pre_state == 'diff':
            pass
        else:
            ans = 3
        pre_state = 'same'
        i += 1
    elif s1[i] != s2[i]:
        if pre_state == 'same':
            ans = (ans*2)%MOD
        elif pre_state == 'diff':
            ans = (ans*3)%MOD
        else:
            ans = 6
        pre_state = 'diff'
        i += 2

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s1=input()
s2=input()

v=3
for i in range(1,n):
  if s1[i-1]==s2[i-1] or i==1:
    v*=2
  elif s1[i-1]!=s1[i] and s1[i]!=s2[i]:
    v*=3

print(v%1000000007)

Submission status: Accepted

Solution code:
n=int(input())
s1=input()
s2=input()

v=3
for i in range(1,n):
  if s1[i-1]==s2[i-1]:
    v*=2
  elif s1[i-1]!=s1[i] and s1[i]!=s2[i]:
    v*=3
  elif i==1:
    v*=2

print(v%1000000007)

Submission status: Accepted

Solution code:
N = int(input())

S = input()
T = input()

MODN = 1000000007

# t:tate, y;yoko
pat = []
i = 0
while i < N:
  if S[i] == T[i]:
    pat.append('t')
    i += 1
  else:
    pat.append('y')
    i += 2

#print("".join(pat))

cnt = 3 if pat[0] == 't' else 6

for i in range(1,len(pat)):
  if (pat[i-1], pat[i]) == ('t', 't'):
    cnt = cnt * 2 % MODN
  elif (pat[i-1], pat[i]) == ('t', 'y'):
    cnt = cnt * 2 % MODN
  elif (pat[i-1], pat[i]) == ('y', 't'):
    cnt = cnt
  elif (pat[i-1], pat[i]) == ('y', 'y'):
    cnt = cnt * 3 % MODN
  else:
    print("Error") # impossible

print(cnt)

Submission status: Accepted

Solution code:
N=int(input())
S1=input()
S2=input()
l=0
ans=1
flag='a'
if S1[0]==S2[0]:
    l+=1
    ans*=3
    flag='t'
else:
    l+=2
    ans*=6
    flag='y'

for i in range(l,N):
    
    if flag=='t':
        if S1[l]==S2[l]:
            l+=1
            ans*=2
            ans=ans%1000000007
            if l>=N:
                break
        else:
            l+=2
            ans*=2
            ans=ans%1000000007
            flag='y'
            if l>=N:
                break
    else:
        if S1[l]==S2[l]:
            l+=1
            flag='t'
            if l>=N:
                break
        else:
            l+=2
            ans*=3
            ans=ans%1000000007
            if l>=N:
                break

print(ans%1000000007)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
s2 = input()
i = 0
flag = -1
ans = 1
mod = 1000000007
while i<n:
  if s[i] == s2[i]:
    if flag == -1:
      ans *= 3
      flag = 0
    elif flag == 0:
      ans *= 2
      flag=0
    else:
      flag=0
    i+=1
  else:
    if flag == -1:
      ans *= 6
      flag = 1
    elif flag == 0:
      ans *= 2
      flag = 1
    else:
      ans *= 3
      flag = 1
    i += 2
  ans %= mod
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(10000)


# Debug output
def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))


# Binary converter
def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in input().split()]


# --------------------------------------------

dp = None


def main():
    N = int(input())
    S = [input() for _ in range(2)]
    is_cols = []

    i = 0
    while i < N:
        if S[0][i] == S[1][i]:
            is_cols.append(True)
            i += 1
        else:
            is_cols.append(False)
            i += 2

    if is_cols[0]:
        ans = 3
    else:
        ans = 6

    i = 1
    while i < len(is_cols):
        if is_cols[i] and is_cols[i - 1]:
            ans *= 2
        elif not is_cols[i] and not is_cols[i - 1]:
            ans *= 3
        elif not is_cols[i] and is_cols[i - 1]:
            ans *= 2

        i += 1

    print(ans % 1000000007)


main()

Submission status: Accepted

Solution code:
N = int( input())
SU = list( input())
SD = list( input())
Q = 10**9 + 7
i = 0
yokocnt = 0
tatecnt = 0
ans = 3
while i <= N-1:
    if SU[i] == SD[i]:
        yokocnt = 0
        i += 1
        tatecnt += 1
        if tatecnt >= 2:
            ans = (ans*2)%Q
    else:
        tatecnt = 0
        i += 2
        yokocnt += 1
        if yokocnt >= 2:
            ans = (ans*3)%Q
        else:
            ans = (ans*2)%Q
print(ans)

Submission status: Accepted

Solution code:
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
AtoZ = [chr(i) for i in range(65,65+26)]
atoz = [chr(i) for i in range(97,97+26)]

def inpl(): return list(map(int, input().split()))
def inpl_s(): return list(input().split())

N = int(input())
S1 = input()
S2 = input()
li = []

i = 0
while i < N:
	if i == N-1 or S1[i] != S1[i+1]:
		li.append(1)
		i += 1
	else:
		li.append(2)
		i += 2

if li[0] == 1:
	ans = 3
else:
	ans = 6

L = len(li)

for i in range(1,L):
	bef = li[i-1]
	now = li[i]
	if bef == 2 and now == 2:
		ans *= 3
	elif bef == 2 and now == 1:
		ans *= 1
	elif bef == 1 and now == 2:
		ans *= 2
	elif bef == 1 and now == 1:
		ans *= 2
	ans %= mod

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
S1 = input()
S2 = input()
m = 1000000007

if S1[0] == S2[0]:
    p = 3
    v = True
    i = 1
else:
    p = 6
    v = False
    i = 2
while(i < n):
    if S1[i] == S2[i]:
        p = (p*2 if v else p) % m
        v = True
        i += 1
    else:
        p = (p*2 if v else p*3) % m
        v = False
        i += 2
print(p)

Submission status: Accepted

Solution code:
N = int(input())
S1 = input()
S2 = input()
patterns = []
flag = False
for i in range(N):
    if S1[i] == S2[i]:
        patterns.append('row')
    elif flag is False:
        patterns.append('column')
        flag = True
    else:
        flag = False

if patterns[0] == 'row':
    count = 3
else:
    count = 6
for i in range(1, len(patterns)):
    prev = patterns[i-1]
    current = patterns[i]
    if prev == 'row':
        if current == 'row':
            count *= 2
        else:
            count *= 2
    else: # column
        if current == 'row':
            count *= 1
        else:
            count *= 3
print(count % (10**9  + 7))

Submission status: Accepted

Solution code:
N = int(input())
S1 = input()
S2 = input()

mod = 10 ** 9 + 7

ans = 0
ptn = 0
i = 0
if S1[i] == S2[i]:
    ans = 3
    ptn = 0
    i += 1
else:
    ans = 6
    ptn = 1
    i += 2

while i < N:
    if S1[i] == S2[i]:
        if ptn == 0:
            ans *= 2
        ptn = 0
        i += 1
    else:
        if ptn == 0:
            ans *= 2
        else:
            ans *= 3
        ptn = 1
        i += 2
print(ans % mod)

Submission status: Accepted

Solution code:
n = int(input())
s_1 = list(input())
s_2 = list(input())
m = 1000000007
# h: 3
# w: 6(3*2)
# h->h: 2, h->w: 1
# w->h: 2, w->w: 3
combi, hist = 0, ''
for i in range(n):
    try:
        if s_1[i] == s_2[i]: # h
            if hist == '':
                combi = 3
            elif hist == 'h':
                combi *= 2
            else:
                combi *= 1
            hist = 'h'
        else: # w
            if hist == '':
                combi = 6
            elif hist == 'h':
                combi *= 2
            else:
                combi *= 3
            hist = 'w'
            s_1.pop(i)
            s_2.pop(i)
    except:
        break
print(combi%m)
exit(0)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, S1, S2):
    ans = 0
    MOD = 1000000007

    if S1[0] == S2[0]:
        ans = 3
        i = 1
        p = 0
    else:
        ans = 6
        i = 2
        p = 1

    while i < N:
        if p == 0:
            if S1[i] == S2[i]:
                ans *= 2
                i += 1
                p = 0
            else:
                ans *= 2
                i += 2
                p = 1
        else:
            if S1[i] == S2[i]:
                p = 0
                i += 1
            else:
                ans *= 3
                i += 2
                p = 1
        ans %= MOD

    return ans


def main():
    N = read_int()
    S1 = read_str()
    S2 = read_str()

    print(slv(N, S1, S2))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
upper = list(input())
lower = list(input())
ans = 0

def count(upper, lower, state):
    global ans
    if upper[0] == lower[0]:
        if state == 1:
            ans *= 2
            try:
                count(upper[1:],lower[1:],state=1)
            except:
                return ans
        if state == 2:
            try:
                count(upper[1:], lower[1:], state=1)
            except:
                return ans
    else:
        if state == 1:
            ans *= 2
            try:
                count(upper[2:], lower[2:], state=2)
            except:
                return ans
        if state == 2:
            ans *= 3
            try:
                count(upper[2:], lower[2:], state=2)
            except:
                return ans

if upper[0] == lower[0]:
    ans = 3
    try:
        count(upper[1:], lower[1:], state=1)
    except:
        pass
else:
    ans = 6
    try:
        count(upper[2:], lower[2:], state=2)
    except:
        pass

print(ans%1000000007)

Submission status: Accepted

Solution code:
N = int(input())
S1 = input()
S2 = input()
res = 6
idx = 0
s = []
mod = 1000000007
while idx < N:
    if S1[idx] == S2[idx]:
        s.append(0)
    else:
        s.append(1)
        idx += 1
    idx += 1

res = 3 if s[0] == 0 else 6

for idx in range(1, len(s)):
    prev, cur = s[idx-1], s[idx]
    if prev == 0:
        if cur == 0:
            res *= 2
        else:
            res *= 2
    else:
        if cur == 0:
            res *= 1
        else:
            res *= 3

res %= mod

print(res)

Submission status: Accepted

Solution code:
N = int(input())
S1 = input()
S2 = input()
res = 6
idx = 0
s = []
mod = 1000000007
while idx < N:
    if S1[idx] == S2[idx]:
        s.append(0)
    else:
        s.append(1)
        idx += 1
    idx += 1

res = 3 if s[0] == 0 else 6

for idx in range(1, len(s)):
    prev, cur = s[idx-1], s[idx]
    if prev == 0:
        if cur == 0:
            res *= 2
        else:
            res *= 2
    else:
        if cur == 0:
            res *= 1
        else:
            res *= 3

    res %= mod

print(res)

Submission status: Accepted

Solution code:
n = int(input())
u = list(input())
v = list(input())

mod = 10**9+7

l = []

pointer = 0
while pointer < n:
    if u[pointer] == v[pointer]:
        pointer += 1
        l.append(0)
    else:
        pointer += 2
        l.append(1)


a = 1
for i,x in enumerate(l):
    if i == 0:
        if x == 0:
            a *= 3
        else:
            a *= 6
    else:
        if x == 0:
            if l[i-1] == 0:
                a *= 2
            else:
                pass
        else:
            if l[i-1] == 0:
                a *= 2
            else:
                a *= 3
    a %= mod

print(a)