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())
S0 = input()
S1 = input()

tate = []
ikkome = True
S = ''
for i in range(N):
  if S0[i] == S1[i]:
    tate.append(i)
    S = S + 'T'
  else:
    if ikkome:
      S = S + 'Y'
      ikkome = False
    else:
      ikkome = True

now = S[0]
if now == 'T':
  ans = 3
else:
  ans = 6
  
if len(S) == 1:
  print(ans)
  exit()
  
S = S[1:]
MOD = 1000000007
for si in S:
  if now == 'T':
    if si == 'T':
      ans = ans * 2 % MOD
    else:
      ans = ans * 2 % MOD
      now = 'Y'
  else:
    if si == 'Y':
      ans = ans * 3 % MOD
    else:
      now = 'T'
print(ans)

Submission status: Accepted

Solution code:
def solve():
    mod = 10**9+7
    N = int(input())
    s1 = input()
    s2 = input()
    lis = []
    i=0
    while i<N-1:
        if s1[i]==s1[i+1]:
            lis.append(2)
            i += 2
        else:
            lis.append(1)
            i += 1
    if i==N-1:
        lis.append(1)
    if lis[0]==1:
        ans = 3
    else:
        ans = 6
    for i in range(len(lis)-1):
        if lis[i]==1:
            ans *= 2
            ans %= mod
        if lis[i]==2 and lis[i+1]==2:
            ans *=3
            ans %= mod
    return ans
print(solve())

Submission status: Accepted

Solution code:
def solve():
    mod = 10**9+7
    N = int(input())
    s1 = input()
    s2 = input()
    lis = []
    i=0
    while i<N-1:
        if s1[i]==s1[i+1]:
            lis.append(2)
            i += 2
        else:
            lis.append(1)
            i += 1
    if i==N-1:
        lis.append(1)
    if N==1:
        lis = [1]
    if lis[0]==1:
        ans = 3
    else:
        ans = 6
    for i in range(len(lis)-1):
        if lis[i]==1:
            ans *= 2
            ans %= mod
        if lis[i]==2 and lis[i+1]==2:
            ans *=3
            ans %= mod
    return ans
print(solve())

Submission status: Accepted

Solution code:
def main():
    MOD = 10**9 + 7
    N = int(input())
    S = [input() for _ in range(2)]
    # pattern 0 | pattern 1
    #     X     |    XX
    #     X     |    YY
    # before | current
    #    0   |    0    -> 2
    #    0   |    1    -> 2
    #    1   |    0    -> 1
    #    1   |    1    -> 3
    before_pattern = 0 if S[0][0] == S[1][0] else 1
    ans = 3 if before_pattern == 0 else 3 * 2
    i = 1 if before_pattern == 0 else 2
    while i < N:
        pattern = 0 if S[0][i] == S[1][i] else 1
        if pattern == 0:
            if before_pattern == 0:
                ans *= 2
            i += 1
        else:
            if before_pattern == 0:
                ans *= 2
            else:
                ans *= 3
            i += 2
        before_pattern = pattern
        ans %= MOD
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    S1 = input()
    S2 = input()
    # 縦
    if S1[0] == S2[0]:
        ans = 3

        # 1は縦，0は横
        prev = 1
    else:
        ans = 3 * 2
        prev = 0

    for i in range(1, N):
        # 1つ前が横
        if prev == 0:
            # 縦
            if S1[i] == S2[i]:
                prev = 1
                continue
            else:
                # 同じドミノ
                if S1[i - 1] == S1[i]:
                    continue
                # 違うドミノ
                else:
                    ans *= 3
                    ans %= MOD
        else:
            # 一つ前が縦
            # 縦
            if S1[i] == S2[i]:
                ans *= 2
                ans %= MOD
            else:
                prev = 0
                ans *= 2
                ans %= MOD

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
#import numpy as np
import math
#from fractions import Fraction
#import itertools
#from collections import deque
#import heapq
#from fractions  import gcd

#input=sys.stdin.readline
mod=1000000007
n=int(input())
s=[input() for _ in range(2)]
d=[]
i=0
while i<n:
    if s[0][i]==s[1][i]:
        d.append(1)
        i+=1
    else:
        d.append(2)
        i+=2
m=len(d)
ans=0
if d[0]==1:
    ans=3
if d[0]==2:
    ans=6
for i in range(1,m):
    if d[i]==1:
        if d[i-1]==2:
            ans*=1
        else:
            ans*=2
    else:
        if d[i-1]==2:
            ans*=3
        else:
            ans*=2
    ans=ans%mod
print(ans%mod)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from itertools import accumulate #list(accumulate(A))

N = ii()
S = [input() for _ in range(2)]
mod = 10**9+7
pat = []
i = 0
ans = 1
while i < N:
    if S[0][i] == S[1][i]:
        if i > 0:
            if pat[-1] == 0:
                ans = (ans * 2) % mod
        else:
            ans *= 3
        pat.append(0)
        i += 1
    else:
        if i > 0:
            if pat[-1] == 0:
                ans = (ans * 2) % mod
            else:
                ans = (ans * 3) % mod
        else:
            ans *= 6
        pat.append(1)
        i += 2

print(ans)

Submission status: Accepted

Solution code:
mod = 1000000007
n = int(input())
s = input()
l = []
for i in range(1, n):
  if s[i - 1] == s[i]:
    l.append(2)
  elif i < 2 or s[i - 2] != s[i - 1]:
    l.append(1)
if n < 2 or s[n - 2] != s[n - 1]:
  l.append(1)
if l[0] == 1:
  ans = 3
else:
  ans = 6
for i in range(1, len(l)):
  if l[i - 1] == 1:
    ans = 2 * ans % mod
  elif l[i] == 2:
    ans = 3 * ans % mod
print(ans)

Submission status: Accepted

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

if S1[0] == S2[0]:
    result = 3
else:
    result = 6
x = 0
W = len(S1)
while x < W:
    if S1[x] == S2[x]:
        if x + 1 == W:
            break
        result *= 2
        result %= 1000000007
        x += 1
    else:
        if x + 2 == W:
            break
        if S1[x + 2] != S2[x + 2]:
            result *= 3
            result %= 1000000007
        x += 2
print(result)

Submission status: Accepted

Solution code:
n=int(input())
mod=10**9+7
ans=1
pre=0
s,t=input()+"#",input()+"#"
now=0
yoko=[6,2,3]
tate=[3,2,1]
while now<n:
    if s[now]==s[now+1]:
        ans=ans*yoko[pre]%mod
        pre=2
        now+=2
    else:
        ans=ans*tate[pre]%mod
        pre=1
        now+=1
print(ans)

Submission status: Accepted

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

MOD = 10**9 + 7

pre = [S1[0], S2[0]]
ans = 6 if pre[0] != pre[1] else 3
for i in range(1,N):
    current =[S1[i], S2[i]]
    if pre[0] == pre[1]:
        if current[0] == current[1]:
            ans *= 2
            ans %= MOD
        else:
            ans *= 2
            ans %= MOD
    else:
        if current[0] != current[1] and pre[0] != current[0]:
            ans *= 3
            ans %= MOD
    pre = current[:]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

S1 = input()
S2 = input()
MOD = 1000000007

if S1[0] == S2[0]:
    ans = 3
    pre = 1
else:
    ans = 6
    pre = 2
for i in range(1, N):
    if S1[i] == S1[i - 1]: continue
    if S1[i] == S2[i]:
        if pre == 1:
            ans *= 2
        pre = 1
    else:
        if pre == 1:
            ans *= 2
        else:
            ans *= 3
        pre = 2
    ans %= MOD
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
S_1 = input()
S_2 = input()

S = []
S.append([ord(s) for s in S_1])
S.append([ord(s) for s in S_2])
S = np.array(S).T

mod = 10 ** 9 + 7

ans = 1
flag = False

for i in range(N):
    if flag:
        flag = False
        continue
    if S[i][0] == S[i][1]: # 横
        if i == 0:
            ans *= 3
        else:
            if S[i - 1][0] == S[i - 1][1]:
                ans *= 2
    else: # 縦
        if i == 0:
            ans *= 6
        else:
            if S[i - 1][0] == S[i - 1][1]:
                ans *= 2
            else:
                ans *= 3
        flag = True
    ans %= mod

print(ans)

Submission status: Accepted

Solution code:
import bisect
import collections
import functools
import heapq
import math
import sys
from collections import deque
from collections import defaultdict
input = sys.stdin.readline
MOD = 10**9+7

N = int(input())
S1 = list(input())
S2 = list(input())
a = []
now = 0
while now < N:
    if S1[now] == S2[now]:
        a.append("s")
        now += 1
    else:
        a.append("d")
        now += 2

for i in range(len(a)):
    if i == 0:
        if a[i] == "s":
            ans = 3
        else:
            ans = 6
    else:
        if a[i-1] == "s":
            ans *= 2
        else:
            if a[i] == "d":
                ans *= 3

print(ans%MOD)

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7
N = int(input())
S = iter(input() + " ")
prev = next(S)
count = 0
counts = []
for c in S:
    count += 1
    if prev != c:
        counts.append(count)
        count = 0
    prev = c
counts = iter(counts)
prev = next(counts)
ans = 3 * prev
for count in counts:
    if prev == 1:
        ans *= 2
    elif count == 2:
        ans *= 3
    prev = count
print(ans % MOD)

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()

from collections import defaultdict, deque, Counter
from sys import exit
import heapq
import math
import copy
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

N = getN()
S1 = input()
S2 = input()
dp = [0 for i in range(N)]
if S1[0] == S2[0]:
    dp[0] = 3
else:
    dp[0] = 6
# i - 1個目、i個目が
# 横ドミノ１個目→横ドミノ２個目
# 横ドミノ→横ドミノ
# 横ドミノ→縦ドミノ
# 縦ドミノ→縦ドミノ
# 縦ドミノ→横ドミノそれぞれについて場合分け
# 各回について
for i in range(1, N):
    # 横ドミノ２つ目だった場合
    if S1[i] == S1[i - 1]:
        dp[i] = dp[i - 1]
    # 横ドミノ１つめor縦ドミノ１つ目の場合
    else:
        # 縦ドミノ１つ目
        if S1[i] == S2[i]:
            # 一つ前も縦ドミノ
            if S1[i - 1] == S2[i - 1]:
                dp[i] = (dp[i - 1] * 2) % mod
            # 横ドミノ
            else:
                dp[i] = dp[i - 1]
        # 横ドミノ1つ目
        else:
            # 一つ前が縦ドミノ
            if S1[i - 1] == S2[i - 1]:
                dp[i] = (dp[i - 1] * 2) % mod
            # 一つ前が２つ目横ドミノ
            else:
                dp[i] = (dp[i - 1] * 3) % mod
print(dp[-1])

Submission status: Accepted

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

mod=1000000007

x=0
cnt=1
while x<n:
    if x==0:
        if s1[x]==s2[x]:
            cnt*=3%mod
            x+=1
        else:
            cnt*=6%mod
            x+=2
    else:
        if s1[x-1]==s2[x-1]:
            #x-1:AA 
            #x  :BB CC
            if s1[x]==s2[x]:
                cnt*=2%mod
                x+=1
            #x-1:AA 
            #x  :BC  CB
            #    BC  CB
            else:
                cnt*=2%mod
                x+=2
        else:
            #x-1:AB
            #   :CC
            if s1[x]==s2[x]:
                cnt*=1%mod
                x+=1
            #x-1:AB
            #x  :BA CA BC 
            else:
                cnt*=3%mod
                x+=2

print(cnt%mod)

Submission status: Accepted

Solution code:
N = int(input())
s = input()
t = input()

mod = int(1e9)+7

ans = 1
bef = 0
i = 0
while i < N:
    ##print(bef,ans,i)
    if bef == 0:##最初
        if s[i] == t[i]:##たてならべ
            bef = 1
            ans *= 3
        else:
            bef = 2
            ans *= 6
            i += 1
    elif bef == 1:##前がたてならべ
        if s[i] == t[i]:
            ans = (ans*2)%mod
        else:
            bef = 2
            ans = (ans*2)%mod
            i += 1
    else:##前がよこならべ
        if s[i] == t[i]:
            bef = 1
        else:
            ans = (ans*3)%mod
            i += 1
    i += 1

print(ans%mod)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    s1 = input()
    s2 = input()
    mod = 10**9 + 7
    h = "h"
    v = "v"
    d = ""
    i = 0
    while i < n:
        if s1[i] == s2[i]:
            d += v
        else:
            d += h
            i += 1
        i += 1
    c = 3 + 3 * (d[0] == h)
    for i in range(len(d) - 1):
        if d[i] == v:
            c = c * 2 % mod
        else:
            if d[i + 1] == h:
                c = c * 3 % mod
    print(c)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(10 ** 9)


MOD = 1000000007


def main():
    N = int(input())
    S1 = input()
    S2 = input()
    answer = 1
    cnt = 0
    while cnt < N:
        if cnt == 0:
            if S1[0] == S2[0]:
                answer *= 3
                cnt += 1
            else:
                answer *= 6
                cnt += 2
        else:
            if S1[cnt - 1] == S2[cnt - 1]:
                if S1[cnt] == S2[cnt]:
                    answer *= 2
                    cnt += 1
                else:
                    answer *= 2
                    cnt += 2
            else:
                if S1[cnt] == S2[cnt]:
                    answer *= 1
                    cnt += 1
                else:
                    answer *= 3
                    cnt += 2
        answer %= MOD

    print(answer % MOD)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
ty = [0]*n # 0:縦 1:横
s1 = input()
prev = ""
cnt = 0
for i in range(n):
    if prev == s1[i]:
        ty[i] = 1
        ty[i-1] = 1
    prev = s1[i]
ans = 1
mod = 10**9+7
f = 0
if ty[0] ==0:
    ans = 3
else:
    ans = 6
    f = 1

for i in range(1,n):
    if f==1:
        f = 0
        continue

    if ty[i] ==1:
        f = 1
        if ty[i-1] ==1:
            ans = ans*3 %mod
        else:
            ans = ans*2 %mod
    elif ty[i] == 0:
        if ty[i-1] == 0:
            ans = ans*2 %mod
        else:
            ans = ans*1
print(ans)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
MOD = 10 ** 9 + 7

def main():
    N = int(readline())
    S1 = readline().rstrip()
    S2 = readline().rstrip()
    if S1[0] == S2[0]:
        cnt = 3
        i = 1
        pre = 'x'
    else:
        cnt = 6
        i = 2
        pre = 'y'
    while i < N:
        if S1[i] == S2[i]:
            next = 'x'
        else:
            next = 'y'
        if pre == next == 'x':
            cnt *= 2
            i += 1
        elif pre == next == 'y':
            cnt *= 3
            i += 2
        elif pre == 'x' and next == 'y':
            cnt *= 2
            i += 2
        else:
            i += 1
        pre = next

    print(cnt % MOD)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
S = [list(input()) for i in range(2)]
i = 0
ans = 1
mod = 10**9+7
if S[0][i]==S[1][i]:
    ans = 3
    i += 1
    flag = 0
else:
    ans = 6
    i += 2
    flag = 1
while i<N:
    if flag==0 and S[0][i]==S[1][i]:
        ans *= 2
        ans %= mod
        i += 1
        flag = 0
    elif flag == 1 and S[0][i]==S[1][i]:
        i += 1
        flag = 0
    elif flag == 0 and S[0][i]!=S[1][i]:
        ans *= 2
        ans %= mod
        i += 2
        flag = 1
    else:
        ans *= 3
        ans %= mod
        i += 2
        flag = 1
print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    S1 = input()
    S2 = input()

    Domino = []
    i = 0
    while i < n:
        if S1[i] == S2[i]:
            Domino.append("X")
            i += 1
        else:
            Domino.append("Y")
            i += 2

    res = 3 if Domino[0] == "X" else 6
    for i in range(1, len(Domino)):
        if Domino[i] == "X" and Domino[i - 1] == "X":
            res = res * 2 % mod
        elif Domino[i] == "X" and Domino[i - 1] == "Y":
            pass
        elif Domino[i] == "Y" and Domino[i - 1] == "X":
            res = res * 2 % mod
        else:
            res = res * 3 % mod

    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
#dekimasen-deshita-
n = int(input())
s = [input(), input()]
mod = 10**9+7
i = 0
ans = 3
pre = 0

while i < n:
    if s[0][i] == s[1][i]:
        typ = 1
        i += 1
    else:
        typ = 2
        i += 2
    if pre == 0 and typ == 2:
        ans *= 2
    elif pre == 1:
        ans = (ans*2)%mod
    elif pre == 2 and typ == 2:
        ans = (ans*3)%mod
    pre = typ
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l1=[str(x) for x in input()]
l2=[str(x) for x in input()]

if l1[0]==l2[0]:
    pat=3
    typ='lon'
else:
    pat=6
    typ='lat'
for i in range(1,n):
    if l1[i]!=l1[i-1]:
        if typ=='lon':
            if l1[i]==l2[i]:
                pat *= 2
            else:
                pat *= 2
                typ='lat'
        else:
            if l1[i]==l2[i]:
                typ='lon'
            else:
                pat *= 3
print(pat%(10**9+7))

Submission status: Accepted

Solution code:
N = int(input())
S = [input() for _ in range(2)]

ans = 0
mod = 10 ** 9 + 7
prev = False
cur = False
for i in range(N):
    if cur:
        cur = False
        continue

    if S[0][i] == S[1][i]:
        if i == 0:
            ans = 3
        elif not prev:
            ans = ans * 2 % mod
        cur = False
        prev = False
    else:
        if i == 0:
            ans = 6
        elif prev:
            ans = ans * 3 % mod
        else:
            ans = ans * 2 % mod
        cur = True
        prev = True

print(ans)

Submission status: Accepted

Solution code:
MOD=10**9+7
N=int(input())

mat=[]
for i in range(2):
  mat.append(list(input()))
#print(mat)

dp=[1]+[0]*N
i=1
while i<N+1:
  if mat[0][i-1]==mat[1][i-1]:
    if i==1:
      dp[1]=3
    else:
      if mat[0][i-2]==mat[1][i-2]:
        dp[i]=2*dp[i-1]
        dp[i]%=MOD
      else:
        dp[i]=dp[i-1]
  else:
    if i==1:
      dp[1]=dp[2]=6
      i+=1
    else:
      if mat[0][i-2]==mat[1][i-2]:
        dp[i]=dp[i+1]=2*dp[i-1]
        dp[i]%=MOD
        dp[i+1]%=MOD
        i+=1
      else:
        dp[i]=dp[i+1]=3*dp[i-1]
        dp[i]%=MOD
        dp[i+1]%=MOD
        i+=1
  i+=1
        
#print(dp)
print(dp[-1])

Submission status: Accepted

Solution code:
n = int(input())
s1 = input()
s2 = input()
ans = 1
ch1 = 1
N = 1000000007
s1+='?'

if s1[0]==s1[1]:
    ch2=2
    ans=6
    s=3
else:
    ch2=1
    ans=3
    s=2

for i in range(s,n+1):
        if s1[i]==s1[i-1]:
            ch1+=1
        else:
            if ch1 ==1:
                if ch2==2:
                    ans*=1
                else:
                    ans=ans*2%N
            else:
                if ch2==2:
                    ans=ans*3%N
                else:
                    ans=ans*2%N
            ch2=ch1
            ch1=1

print(ans)

Submission status: Accepted

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

ans = 1
pre = None
i = 0
while i < N:
    if pre is None:
        if S1[i] == S2[i]:
            ans = ans*3 % mod
            pre = 'x'
            i += 1
        else:
            ans = ans*6 % mod
            pre = 'y'
            i += 2
    elif pre == 'x':
        if S1[i] == S2[i]:
            ans = ans*2 % mod
            pre = 'x'
            i += 1
        else:
            ans = ans*2 % mod
            pre = 'y'
            i += 2
    elif pre == 'y':
        if S1[i] == S2[i]:
            ans = ans*1 % mod
            pre = 'x'
            i += 1
        else:
            ans = ans*3 % mod
            pre = 'y'
            i += 2

print(ans % mod)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
MOD = 1000000007

n = int(input())
s1 = input()
s2 = input()

if s1[0] == s2[0]:
    ans = 3
    i = 1
    prev = 1
else:
    ans = 6
    i = 2
    prev = 2

while i<n:
    
    if s1[i] == s2[i]:
        i += 1
        if prev == 1:
            ans *= 2
        else:
            prev = 1
    else:
        i += 2
        if prev == 1:
            ans *= 2
            prev = 2
        else:
            ans *= 3
    ans %= MOD

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=input()
b=input()

ans=0
MOD= 1000000007
cnt=0
ptn=-1
while cnt<n:
    if a[cnt]==b[cnt]:
        if cnt==0:
            ans=3
            ptn=0
        else:
            if ptn==0:
                ans*=2
                ptn=0
            else:
                ans*=1
                ptn=0
        cnt+=1
    else:
        if cnt==0:
            ans=6
            ptn=1
        else:
            if ptn==0:
                ans*=2
                ptn=1
            else:
                ans*=3
                ptn=1
        cnt+=2
    ans%=MOD

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N=int(input())
    S=[list(input()) for _ in range(2)]
    ans=1
    prev=0
    i=0
    while i<N:
        if S[0][i]==S[1][i]:
            ans*=3-prev
            prev=1
            i+=1
        else:
            if prev==0:
                ans*=6
            elif prev==1:
                ans*=2
            else:
                ans*=3
            prev=2
            i+=2
        ans%=MOD
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = input()
mod = 10 ** 9 + 7
if n == 1:
    print(3)
    exit()
if s[0] == s[1]:
    i = 2
    rslt = 6
    flag = True
else:
    i = 1
    rslt = 3
    flag = False
while i < n:
    if i != n-1 and s[i] == s[i + 1]:
        if flag:
            rslt = rslt * 3 % mod
        else:
            rslt = rslt * 2 % mod
        i += 2
        flag = True
    else:
        if not flag:
            rslt = rslt * 2 % mod
        i += 1
        flag = False

print(rslt)

Submission status: Accepted

Solution code:
N = int(input())
S1 = input()
S2 = input()
ans = 1
mod = 1000000007
i = 0
while i < N:
    if i == 0:
        if S1[i] == S2[i]:
            ans *= 3
            i += 1
        else:
            ans *= 6
            i += 2
    else:
        if S1[i] == S2[i]:
            if S1[i-1] == S2[i-1]:
                ans *= 2
                ans %= mod
                i += 1
            else:
                ans *= 1
                ans %= mod
                i += 1
        else:
            if S1[i-1] == S2[i-1]:
                ans *= 2
                ans %= mod
                i += 2
            else:
                ans *= 3
                ans %= mod
                i += 2
print(ans)

Submission status: Accepted

Solution code:
# solution

data = int(input())

data1 = [input(), input()]

mod = 10**9+7

i = 1 + (data1[0][0] != data1[1][0])

ans = 3 * i

while i < data:
    p = data1[0][i-1] == data1[1][i-1]
    if data1[0][i] == data1[1][i]:
        ans *= 1 + p
        i += 1
    else:
        ans *= 3 - p
        i += 2
    ans %= mod

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s1 = input()
s2 = input()
mod = 10 ** 9 + 7
if s1[0] == s2[0]:
  ans = 3
  flag = 1
  now = 1
else:
  ans = 6
  flag = 0
  now = 2
while now < n:
  if s1[now] == s2[now]:
    if flag:
      ans *= 2
    flag = 1
    now += 1
  else:
    if flag:
      ans *= 2
    else:
      ans *= 3
    flag = 0
    now += 2
  ans %= 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:
n = int(input())
s1 = input()
s2 = input()

mod = 10**9+7

if s1[0] == s2[0]:
    ans = 3
    bfr = 'T'
    idx = 1
else:
    ans = 6
    bfr = 'Y'
    idx = 2

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

while idx <= n-1:
    if s1[idx] == s2[idx]:
        if bfr == 'T':
            ans *= 2
            ans %= mod
        idx += 1
        bfr = 'T'
    else:
        if bfr == 'T':
            ans *= 2
            ans %= mod
        else:
            ans *= 3
            ans %= mod
        idx += 2
        bfr = 'Y'
        
print(ans%mod)

Submission status: Accepted

Solution code:
mod = 10**9 + 7


n = int(input())
s1 = input(); s2 = input()

s = ""
i = 0
while i < n:
    if s1[i] == s2[i]:
        s += "A"
        i += 1
    else:
        s += "B"
        i += 2

answer = 3 if s[0] == "A" else 6
for i in range(len(s) - 1):
    if s[i] + s[i + 1] == "AA":
        answer *= 2
    elif s[i] + s[i + 1] == "AB":
        answer *= 2
    elif s[i] + s[i + 1] == "BA":
        answer *= 1
    else:
        answer *= 3
    answer %= mod

print(answer)

Submission status: Accepted

Solution code:
n= int(input())
s1=input()
s2=input()
index=0
s=''
while index<n:
    if s1[index]==s2[index]:
        index+=1
        s=s+'t'
    else:
        index+=2
        s=s+'y'
adict = {'tt':2,'ty':2,'yt':1,'yy':3}
ans = 0
if s[0]=='t':ans=3
else:ans=6
for i in range(len(s)-1):
    ans = (ans%1000000007) * (adict[s[i:i+2]]%1000000007) % 1000000007
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s1=input()
s1_=list(s1)
s2=input()
for i in range(len(s1)-1):
  if s1[i]==s1[i+1]:
    idx=s1_.index(s1[i])
    s1_[idx+1]='1'
    del s1_[idx]
mod=10**9+7
if s1_[0]=='1':
  ans=6
else:
  ans=3
#print(s1_)
for i in range(1,len(s1_)):
  if s1_[i]=='1':
    if s1_[i-1]!='1':
      ans*=2
      ans%=mod
    else:
      ans*=3
      ans%=mod
  else:
    if s1_[i-1]!='1':
      ans*=2
      ans%=mod
print(ans%mod)

Submission status: Accepted

Solution code:
from itertools import groupby
n = int(input())
list_S = input()
list_E = input()
list_MAP = []
list_C = groupby(list_S)
for num, gr in list_C:
    list_MAP.append(len(list(gr)))
ans = 1
if list_MAP[0] == 2:
    ans *= 6
else:
    ans *= 3

for i in range(1, len(list_MAP)):
    if list_MAP[i-1] == 1 and list_MAP[i] == 1:
        ans *= 2


    if list_MAP[i-1] == 1 and list_MAP[i] == 2:
        ans *= 2


    if list_MAP[i-1] == 2 and list_MAP[i] == 1:
        ans *= 1


    if list_MAP[i-1] == 2 and list_MAP[i] == 2:
        ans *=3



ans = ans % 1000000007
print(ans)

Submission status: Accepted

Solution code:
MOD = 10**9 + 7
N = int(input())
S = input()
S2 = input()
check = []
flg = False
for i in range(N-1):
    if flg:
        check.append(2)
        flg = False
        continue
    if S[i] == S[i+1]:
        flg = True
    else:
        check.append(1)
if flg:
    check.append(2)
else:
    check.append(1)
ans = 3 if check[0] == 1 else 6
for i in range(1,len(check)):
    if check[i-1] == 1:
        ans *= 2
    elif check[i] == 2 and check[i-1] == 2:
        ans *= 3
print(ans%MOD)