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: Runtime Error

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

Submission status: Runtime Error

Solution code:
MOD = 1000000007

N = int(input())
S = []
for i in range(2):
    S.append(input())
    

# make XYline
xy = ""
for i in range(len(S[0])):
    if S[0][i] == S[1][i]:
        xy += "X"
    elif S[0][i] == S[0][i+1]:
        xy += "Y"

# proceed
if xy[0] == "X":
    ans = 3
else:
    ans = 6

if len(xy)==1:
    pass
else:
    for i in range(1,len(xy)):
        if xy[i-1] == "X" and xy[i] == "X":
            ans *= 2
        elif xy[i-1] == "X" and xy[i] == "Y":
            ans *= 2
        elif xy[i-1] == "Y" and xy[i] == "X":
            ans *= 1
        elif xy[i-1] == "Y" and xy[i] == "Y":
            ans *= 3

        ans = ans%MOD

    
print(ans)

Submission status: Runtime Error

Solution code:
def solve():
    n = int(input())
    pattern = [[a, b] for a, b in zip(list(input()), list(input()))]
    pointer, ans = 0, 0
    if pattern[0][0] == pattern[0][1]:
        ans = 3
    else:
        ans = 6
    while pointer < n - 1:
        current = pattern[pointer][0]
        if current == pattern[pointer][1]:
            if pattern[pointer+1][0] == pattern[pointer+1][1]:
                ans *= 2
                # print("X->X")
            else:
                ans *= 2
                # print("X->Y")
            pointer += 1
        else:
            if pattern[pointer+2][0] == pattern[pointer+2][1]:
                ans *= 1
                # print("Y->X")
            else:
                ans *= 3
                # print("Y->Y")
            pointer += 2
    print(ans % 1000000007)

if __name__=="__main__":
    solve()

Submission status: Runtime Error

Solution code:
N = int(input())
l1 = input()
l2 = input()
result_f=[3,6]
result_g=[3,6]

def f(N, l1, l2):
    if N==1:
        return 3
    if N==2:
        return 6
    else:
        if l1[len(l1)-1]==l2[len(l2)-1]:
            if l1[len(l1)-2]==l2[len(l2)-2]:
                l1 = l1[:len(l1)-3]
                l2 = l2[:len(l2)-3]
                return f(N-1, l1, l2)*2
            else:
                l1 = l1[:len(l1)-2]
                l2 = l2[:len(l2)-2]
                return g(N-1, l1, l2)
        else:
            return g(N, l1, l2)

def g(N, l1, l2):
    if N==1:
        return 3
    if N==2:
        return 6
    if N==3:
        return 6
    else:
        if l1[len(l1)-1]!=l2[len(l2)-1]:
            if l1[len(l1)-3]==l2[len(l2)-3]:
                l1 = l1[:len(l1)-3]
                l2 = l2[:len(l2)-3]
                return f(N-2, l1, l2)*2
            else:
                l1 = l1[:len(l1)-3]
                l2 = l2[:len(l2)-3]
                return g(N-2, l1, l2)*3
        else:
            return f(N, l1, l2)

print(f(N, l1, l2)%1000000007)

Submission status: Runtime Error

Solution code:
N = int(input())
S1 = list(input())
S2 = list(input())
i = 0


if S1[i] != S2[i]:
    res = 6
    i+=2
    rear = 1
else :
    res = 3
    i+=1
    rear = 0

while i != (N-1):
    if S1[i] != S2[i]:
        if rear == 1:
            res *= 3
        else:
            res *= 2
        rear = 1
        i += 2

    else:
        if rear == 0:
            res *= 2
        i += 1
        rear = 0


print(res%1000000007)

Submission status: Runtime Error

Solution code:
N = int(input())
S1 = list(input())
S2 = list(input())
i = 0


if S1[i] != S2[i]:
    res = 6
    i+=2
    rear = 1
else :
    res = 3
    i+=1
    rear = 0

while i != (N-1):
    if S1[i] != S2[i]:
        if rear == 1:
            res *= 3
        else:
            res *= 2
        rear = 1
        i += 2

    else:
        if rear == 0:
            res *= 2
        i += 1
        rear = 0
    print(i)

print(res)

Submission status: Runtime Error

Solution code:
INF = 1000000007

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

tmp = []

flag = 0
for n in range(N):
  if flag==1:
    flag=0
    continue
  elif S1[n]==S2[n]:
  	tmp.append("type1")
  elif S1[n]==S1[n+1]:
    tmp.append("type2")
    flag=1

if tmp[0]=="type1":
  S = 3
elif tmp[1]=="type2":
  S = 6

for s in range(1,len(tmp)):
  if tmp[s-1]=="type1" and tmp[s]=="type1":
    S *= 2
    S %= INF
  elif tmp[s-1]=="type1" and tmp[s]=="type2":
    S *= 2
    S %= INF
  elif tmp[s-1]=="type2" and tmp[s]=="type2":
    S *= 3
    S % INF
print(S)

Submission status: Runtime Error

Solution code:
INF = 1000000007

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

tmp = []

flag = 0
for n in range(N):
  if flag==1:
    flag=0
    continue
  elif S1[n]==S2[n]:
  	tmp.append("type1")
  elif S1[n]==S1[n+1]:
    tmp.append("type2")
    flag=1
print(tmp)

if tmp[0]=="type1":
  S = 3
elif tmp[1]=="type2":
  S = 6

for s in range(1,len(tmp)):
  if tmp[s-1]=="type1" and tmp[s]=="type1":
    S *= 2
    S %= INF
  elif tmp[s-1]=="type1" and tmp[s]=="type2":
    S *= 2
    S %= INF
  elif tmp[s-1]=="type2" and tmp[s]=="type2":
    S *= 3
    S % INF
print(S)