Problem:
 Score : 500 points    Problem Statement Snuke, who loves animals, built a zoo.  There are N animals in this zoo. They are conveniently numbered 1 through N , and arranged in a circle.
The animal numbered i (2≤i≤N-1) is adjacent to the animals numbered i-1 and i+1 . Also, the animal numbered 1 is adjacent to the animals numbered 2 and N , and the animal numbered N is adjacent to the animals numbered N-1 and 1 .  There are two kinds of animals in this zoo: honest sheep that only speak the truth, and lying wolves that only tell lies.  Snuke cannot tell the difference between these two species, and asked each animal the following question: "Are your neighbors of the same species?" The animal numbered i answered s_i . Here, if s_i is o , the animal said that the two neighboring animals are of the same species, and if s_i is x , the animal said that the two neighboring animals are of different species.  More formally, a sheep answered o if the two neighboring animals are both sheep or both wolves, and answered x otherwise.
Similarly, a wolf answered x if the two neighboring animals are both sheep or both wolves, and answered o otherwise.  Snuke is wondering whether there is a valid assignment of species to the animals that is consistent with these responses. If there is such an assignment, show one such assignment. Otherwise, print -1 .      Constraints  3 ≤ N ≤ 10^{5}  s is a string of length N consisting of o and x .         Input The input is given from Standard Input in the following format:  N  s       Output If there does not exist an valid assignment that is consistent with s , print -1 .
Otherwise, print an string t in the following format. The output is considered correct if the assignment described by t is consistent with s .   t is a string of length N consisting of S and W .  If t_i is S , it indicates that the animal numbered i is a sheep. If t_i is W , it indicates that the animal numbered i is a wolf.         Sample Input 1 6
ooxoox      Sample Output 1 SSSWWS  For example, if the animals numbered 1 , 2 , 3 , 4 , 5 and 6 are respectively a sheep, sheep, sheep, wolf, wolf, and sheep, it is consistent with their responses. Besides, there is another valid assignment of species: a wolf, sheep, wolf, sheep, wolf and wolf.  Let us remind you: if the neiboring animals are of the same species, a sheep answers o and a wolf answers x . If the neiboring animals are of different species, a sheep answers x and a wolf answers o .          Sample Input 2 3
oox      Sample Output 2 -1  Print -1 if there is no valid assignment of species.       Sample Input 3 10
oxooxoxoox      Sample Output 3 SSWWSSSWWS

Solution (Python):
from itertools import product

N = int(input())
s = input()

answer = [''] * N

for i, j in product(['S', 'W'], repeat=2):
    answer[0] = i
    answer[1] = j
    for k in range(2, N):
        if answer[k - 1] == 'S':
            if s[k - 1] == 'o':
                answer[k] = answer[k - 2]
            else:
                if answer[k - 2] == 'W':
                    answer[k] = 'S'
                else:
                    answer[k] = 'W'
        else:
            if s[k - 1] == 'x':
                answer[k] = answer[k - 2]
            else:
                if answer[k - 2] == 'W':
                    answer[k] = 'S'
                else:
                    answer[k] = 'W'

    for k in [-1, 0]:
        if answer[k] == 'S':
            if s[k] == 'o':
                if answer[k - 1] != answer[k + 1]:
                    break
            else:
                if answer[k - 1] == answer[k + 1]:
                    break
        else:
            if s[k] == 'x':
                if answer[k - 1] != answer[k + 1]:
                    break
            else:
                if answer[k - 1] == answer[k + 1]:
                    break
    else:
        print(''.join(answer))
        exit()

print(-1)

Solution (Python):
N = int(input())
S = [1 if a == "x" else 0 for a in input()]
for a, b in ((0, 0), (0, 1), (1, 0), (1, 1)):
    X = [a, b]
    for i in range(N):
        X.append(X[-1]^X[-2]^S[i])
    if X[-2:] == X[:2]:
        print("".join(["W" if x else "S" for x in X[1:-1]]))
        break
else:
    print(-1)

Solution (Python):
def f(n, s, t):
    for i in range(2, n):
        if (t[i-2] == t[i-1] and s[i-1] == 'o') or (t[i-2] != t[i-1] and s[i-1] == 'x'):
            t += 'S'
        else:
            t += 'W'
    for i in range(0, 2):
        if (t[(i+n-2)%n] == t[(i+n-1)%n] and s[(i+n-1)%n] == 'o') or (t[(i+n-2)%n] != t[(i+n-1)%n] and s[(i+n-1)%n] == 'x'):
            if t[i] != 'S': return ":("
        else:
            if t[i] != 'W': return ":("
    return t
n = int(input())
s = input()
u = ["SS", "SW", "WS", "WW"]
for i in range(4):
    t = f(n, s, u[i])
    if t != ":(":
        print(t)
        exit(0)
print(-1)

Solution (Python):
N = int(input())
s = input()
def solve(ans):
    for i in range(2,N):
        if ans[i-2]=='S' and ans[i-1]=='S':
            if s[i-1]=='o':
                ans += 'S'
            else:
                ans += 'W'
        elif ans[i-2]=='S' and ans[i-1]=='W':
            if s[i-1]=='o':
                ans += 'W'
            else:
                ans += 'S'
        elif ans[i-2]=='W' and ans[i-1]=='S':
            if s[i-1]=='o':
                ans += 'W'
            else:
                ans += 'S'
        else:
            if s[i-1]=='o':
                ans += 'S'
            else:
                ans += 'W'
    if (ans[0]=='S' and s[0]=='o') or (ans[0]=='W' and s[0]=='x'):
        if ans[-1]!=ans[1]:
            return
    elif (ans[0]=='S' and s[0]=='x') or (ans[0]=='W' and s[0]=='o'):
        if ans[-1]==ans[1]:
            return
    if (ans[-1]=='S' and s[-1]=='o') or (ans[-1]=='W' and s[-1]=='x'):
        if ans[-2]!=ans[0]:
            return
    elif (ans[-1]=='S' and s[-1]=='x') or (ans[-1]=='W' and s[-1]=='o'):
        if ans[-2]==ans[0]:
            return
    return ans

C = ['SS','SW','WS','WW']

for c in C:
    if solve(c):
        print(solve(c))
        break
else:
    print(-1)

Solution (Python):
n=int(input())
s=input()

l = [0] * n
l2 = [0 if s[i]=='o' else 1 for i in range(n)]
def f(x,y):
    l[0]=x
    l[1]=y
    for i in range(2,n):
        l[i]=l[i-2]^l[i-1]^l2[i-1]
    if l[0] == l[n-2]^l[n-1]^l2[n-1] and l[1]==l[n-1]^l[0]^l2[0]:
        return True
    else: return False

if f(0,0):
    print(''.join(['S' if not l[i] else 'W' for i in range(n)]))
elif f(0,1):
    print(''.join(['S' if not l[i] else 'W' for i in range(n)]))
elif f(1,0):
    print(''.join(['S' if not l[i] else 'W' for i in range(n)]))
elif f(1,1):
    print(''.join(['S' if not l[i] else 'W' for i in range(n)]))
else:print(-1)

Solution (Python):
N=int(input())
S=input()
L1,L2,L3,L4='SS','SW','WS','WW'

def make_seq(L):
    for i in range(1,N-1):
        if S[i]+L[i-1]+L[i] in ['oSS','oWW','xSW','xWS']:
            L+='S'
        else:
            L+='W'
    return L

T=['oSSS','oWSW','oSWW','oWWS','xWSS','xSSW','xSWS','xWWW']
def check(L):
    if S[0]+L[-1]+L[0]+L[1] in T and S[-1]+L[-2]+L[-1]+L[0] in T:
        return L
    else:
        return -1

L1=make_seq(L1)
L2=make_seq(L2)
L3=make_seq(L3)
L4=make_seq(L4)
C=[check(L1),check(L2),check(L3),check(L4)]

import sys
for i in C:
    if i!=-1:
        print(i)
        sys.exit()
print(-1)

Solution (Python):
N = int(input())
s = input()

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.root = [-1]*(n+1)
        self.rnk = [0]*(n+1)
        
    def find_root(self, x):
        if self.root[x] < 0:
            return x
        else:
            self.root[x] = self.find_root(self.root[x])
            return self.root[x]
        
    def unite(self, x, y):
        x = self.find_root(x)
        y = self.find_root(y)
        if x == y:
            return
        elif self.rnk[x] > self.rnk[y]:
            self.root[x] += self.root[y]
            self.root[y] = x
        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if self.rnk[x] == self.rnk[y]:
                self.rnk[y] += 1
                
    def isSameGroup(self, x, y):
        return self.find_root(x) == self.find_root(y)
    
    def count(self, x):
        return -self.root[self.find_root(x)]
    
uf0 = UnionFind(N+1) 
uf1 = UnionFind(N+1) 
uf2 = UnionFind(N+1) 
uf3 = UnionFind(N+1) 


uf0.unite(1, 0)
uf0.unite(2, 0)
uf1.unite(1, 0)
uf1.unite(2, N+1)
uf2.unite(1, N+1)
uf2.unite(2, 0)
uf3.unite(1, N+1)
uf3.unite(1, N+1)

if s[0] == "o":
    uf0.unite(N, 0)
    uf1.unite(N, N+1)
    uf2.unite(N, N+1)
    uf3.unite(N, 0)
else:
    uf0.unite(N, N+1)
    uf1.unite(N, 0)
    uf2.unite(N, 0)
    uf3.unite(N, N+1)
    
for i in range(2, N+1):
    if i == N:
        a = 1
    else:
        a = i+1
        
    if s[i-1] == "o":
        if uf0.isSameGroup(i, 0):
            uf0.unite(i-1, a)
        elif uf0.isSameGroup(i-1, 0):
            uf0.unite(a, N+1)
        else:
            uf0.unite(a, 0)
        
        if uf1.isSameGroup(i, 0):
            uf1.unite(i-1, a)
        elif uf1.isSameGroup(i-1, 0):
            uf1.unite(a, N+1)
        else:
            uf1.unite(a, 0)
            
        if uf2.isSameGroup(i, 0):
            uf2.unite(i-1, a)
        elif uf2.isSameGroup(i-1, 0):
            uf2.unite(a, N+1)
        else:
            uf2.unite(a, 0)
            
        if uf3.isSameGroup(i, 0):
            uf3.unite(i-1, a)
        elif uf3.isSameGroup(i-1, 0):
            uf3.unite(a, N+1)
        else:
            uf3.unite(a, 0)
            
    else:
        if not uf0.isSameGroup(i, 0):
            uf0.unite(i-1, a)
        elif uf0.isSameGroup(i-1, 0):
            uf0.unite(a, N+1)
        else:
            uf0.unite(a, 0)
            
        if not uf1.isSameGroup(i, 0):
            uf1.unite(i-1, a)
        elif uf1.isSameGroup(i-1, 0):
            uf1.unite(a, N+1)
        else:
            uf1.unite(a, 0)
            
        if not uf2.isSameGroup(i, 0):
            uf2.unite(i-1, a)
        elif uf2.isSameGroup(i-1, 0):
            uf2.unite(a, N+1)
        else:
            uf2.unite(a, 0)
            
        if not uf3.isSameGroup(i, 0):
            uf3.unite(i-1, a)
        elif uf3.isSameGroup(i-1, 0):
            uf3.unite(a, N+1)
        else:
            uf3.unite(a, 0)
            
ans = -1
if not uf0.isSameGroup(0, N+1):
    ans = uf0
elif not uf1.isSameGroup(0, N+1):
    ans = uf1
elif not uf2.isSameGroup(0, N+1):
    ans = uf2
elif not uf3.isSameGroup(0, N+1):
    ans = uf3
    
if ans == -1:
    print(ans)
else:
    t = []
    for i in range(1, N+1):
        if ans.isSameGroup(i, 0):
            t.append("S")
        else:
            t.append("W")
    print("".join(t))

Solution (Python):


n=int(input())
sw=[0 for i in range(n)]
ox=list(input())

def who(x):
    if sw[x-1]=="S":
        a=1
    else:
        a=-1
        
    if sw[x]=="S":
        b=1
    else:
        b=-1
    
    if ox[x]=="o":
        c=1
    else:
        c=-1
    
    if a*b*c==1:
        return "S"
    else:
        return "W"
   
        
start=[["S","S"],["S","W"],["W","S"],["W","W"]]
ans=-1

for i in range(4):
    sw[0],sw[-1]=start[i]
    tmp1=sw[-1]
    tmp2=sw[0]

    for j in range(n):
        sw[(j+1)%n]=who(j)

    if tmp1==sw[-1] and tmp2==sw[0]:
        ans="".join(sw)
    
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    S=list(map(lambda x:(x=='o')-0,input()))

    
    from itertools import product
    for a,b in product(range(2),repeat=2):
        ans=[None]*n
        ans[0]=a; ans[1]=b

        for i in range(2,n):
            if(S[i-1]==ans[i-1]): ans[i]=ans[i-2]
            else: ans[i]=1-ans[i-2]

        if((S[0]==ans[0])==(ans[1]==ans[-1]) and (S[-1]==ans[-1])==(ans[0]==ans[-2])):
            for s in ans: print('S' if(s) else 'W',end='')
            print()
            return
    print(-1)
resolve()

Solution (Python):
n=int(input())
s=input()

def sw(animal,maeanimal,flag):
    if animal == "S":
        if maeanimal == "S":
            if flag == "o":
                return "S"
            else:
                return "W"
        else:
            if flag == "o":
                return "W"
            else:
                return "S"
    else:
        if maeanimal == "S":
            if flag == "o":
                return "W"
            else:
                return "S"
        else:
            if flag == "o":
                return "S"
            else:
                return "W"
def main():
    r = ["" for _ in range(n)]
    r[0] = "S"
    if s[0] == "o":
        r[1] = "S"
        r[-1]= "S"
    else:
        r[1] = "S"
        r[-1] = "W"

    for i in range(1,n-2):
        r[i+1] = sw(r[i],r[i-1],s[i])
    if r[n-1] == sw(r[n-2],r[n-3],s[n-2]) and r[0] == sw(r[n-1],r[n-2],s[n-1]):
        return "".join(map(str, r))

    r = ["" for _ in range(n)]
    r[0] = "S"
    if s[0] == "o":
        r[1] = "W"
        r[-1]= "W"
    else:
        r[1] = "W"
        r[-1] = "S"

    for i in range(1,n-2):
        r[i+1] = sw(r[i],r[i-1],s[i])
    if r[n-1] == sw(r[n-2],r[n-3],s[n-2]) and r[0] == sw(r[n-1],r[n-2],s[n-1]):
        return "".join(map(str, r))

    r = ["" for _ in range(n)]
    r[0] = "W"
    if s[0] == "o":
        r[1] = "S"
        r[-1]= "W"
    else:
        r[1] = "S"
        r[-1] = "S"

    for i in range(1,n-2):
        r[i+1] = sw(r[i],r[i-1],s[i])
    if r[n-1] == sw(r[n-2],r[n-3],s[n-2]) and r[0] == sw(r[n-1],r[n-2],s[n-1]):
        return "".join(map(str, r))

    r = ["" for _ in range(n)]
    r[0] = "W"
    if s[0] == "o":
        r[1] = "W"
        r[-1]= "S"
    else:
        r[1] = "W"
        r[-1] = "W"

    for i in range(1,n-2):
        r[i+1] = sw(r[i],r[i-1],s[i])
    if r[n-1] == sw(r[n-2],r[n-3],s[n-2]) and r[0] == sw(r[n-1],r[n-2],s[n-1]):
        return "".join(map(str, r))

    return "-1"

print(main())

Solution (Python):
import sys


def main():
    N = int(input())
    s = list(input())
    s.append(s[0])
    s.insert(0, s[-2])

    init = ((0, 0), (0, 1), (1, 0), (1, 1))
    for i in init:
        p = [0] * (N+2)
        p[0] = i[0]
        p[1] = i[1]
        for j in range(1, N+1):
            if s[j] == "o":
                if p[j] == 0:
                    p[j+1] = p[j-1] ^ 0
                else:
                    p[j+1] = p[j-1] ^ 1
            else:
                if p[j] == 0:
                    p[j+1] = p[j-1] ^ 1
                else:
                    p[j+1] = p[j-1] ^ 0

        if p[0] == p[-2] and p[1] == p[-1]:
            ss = list(map(lambda x: "W" if x == 1 else "S", p[1:N+1]))
            print(''.join(ss))
            sys.exit()
    else:
        print(-1)


main()

Solution (Python):
n=int(input())
s=input()
animals=["

def oppositeAnimal(animal):
  if animal=="S":
    return "W"
  else:
    return "S"

def getNextAnimal(animal,answer,pastanimal):
  if animal=="S":
    if answer=="o":
      return pastanimal
    else:
      return oppositeAnimal(pastanimal)
  else:
    if answer=="o":
      return oppositeAnimal(pastanimal)
    else:
      return pastanimal

for a0 in ("S","W"):
  for a1 in ("S","W"):
    animals=["
    animals[0]=a0
    animals[1]=a1
    for i in range(2,len(s)):
      animals[i]=getNextAnimal(animals[i-1],s[i-1],animals[i-2])
    if getNextAnimal(animals[len(s)-1],s[len(s)-1],animals[len(s)-2])==animals[0]:
      if getNextAnimal(animals[0],s[0],animals[len(s)-1])==animals[1]:
        print("".join(animals))
        exit(0)
print(-1)

Solution (Python):
def match(index_num):
    if answer[index_num] == "S":
        if s[index_num] == "o":
            if answer[(index_num+1)%N] == answer[index_num-1]:
                return True
        else:
            if answer[(index_num+1)%N] != answer[index_num-1]:
                return True
    else:
        if s[index_num] == "o":
            if answer[(index_num+1)%N] != answer[index_num-1]:
                return True
        else:
            if answer[(index_num+1)%N] == answer[index_num-1]:
                return True
    return False
 
N = int(input())
s = list(input())
SorW = ["S","W"]
 
for i in SorW:
    for j in SorW:
        answer = []
        append = answer.append
        append(i)
        append(j)
 
        for k in range(2,N):
            if answer[k-1] == "S":
                if s[k-1] == "o":
                    append(answer[k-2])
                else:
                    if answer[k-2] == "S":
                        append("W")
                    else:
                        append("S")
            else:
                if s[k-1] == "o":
                    if answer[k-2] == "W":
                        append("S")
                    else:
                        append("W")
                else:
                    append(answer[k-2])
        
        for k in [0,N-1]:
            if not match(k):
                break
            if k == N-1:
                print("".join(answer))
                exit()
print(-1)

Solution (Python):
N = int(input())
s = list(input())
one_two_pair = (["S", "S"], ["S", "W"], ["W", "S"], ["W", "W"])
for one_two in one_two_pair:
    ans = one_two
    for i in range(1, N - 1):
        left = ans[i - 1]
        now = ans[i]
        ox = s[i]
        if now == "S":
            if ox == "o":
                if left == "S":
                    ans.append("S")
                else:
                    ans.append("W")
            else:
                if left == "S":
                    ans.append("W")
                else:
                    ans.append("S")
        elif now == "W":
            if ox == "o":
                if left == "S":
                    ans.append("W")
                else:
                    ans.append("S")
            else:
                if left == "S":
                    ans.append("S")
                else:
                    ans.append("W")
    first, last = False, False
    left = ans[-1]
    now = ans[0]
    right = ans[1]
    if now == "S":
        if s[0] == "o":
            first = (left == right)
        else:
            first = (left != right)
    else:
        if s[0] == "o":
            first = (left != right)
        else:
            first = (left == right)

    left = ans[-2]
    now = ans[-1]
    right = ans[0]
    if now == "S":
        if s[-1] == "o":
            last = (left == right)
        else:
            last = (left != right)
    else:
        if s[-1] == "o":
            last = (left != right)
        else:
            last = (left == right)

    if first and last:
        print("".join(ans))
        break

else:
    print(-1)

Solution (Python):
import copy

def sol(animals):
  for i, rep in enumerate(reps[:N-2]):
    if i == 0: continue
    if animals[i] == 1:animals[i+1] = rep*animals[i-1]
    else:animals[i+1] = -rep*animals[i-1]

  if animals[-2] == 1 and animals[-1] != reps[-2]*animals[-3]: return False
  elif  animals[-2] == -1 and animals[-1] != -reps[-2]*animals[-3]: return False
      
  if animals[-1] == 1 and animals[0] != reps[-1]*animals[-2]:return False
  elif animals[-1] == -1 and animals[0] != -reps[-1]*animals[-2]: return False

  return animals

N = int(input())
reps = [1 if s=='o' else -1 for s in input()]
animals = [0 for _ in range(N)]

if reps[0] == 1:
  cases = [[1,1,1], [-1,1,-1],[1,-1,-1],[-1,-1,1]]
else:
  cases = [[-1,1,1],[1,1,-1],[1,-1,1],[-1,-1,-1]]
  
for case in cases:
  animals1 = copy.deepcopy(animals)
  animals1[-1],animals1[0],animals1[1] = case 
  res = sol(animals1)
  if res is not False:
    print(''.join(['S' if s==1 else 'W' for s in res]))
    exit()
print(-1)

Solution (Python):
import sys
n=int(input())
s=list(input())
for i in range(n):
    if s[i]=="o":
        s[i]=0
    else:
        s[i]=1
s.append(s[0])

data=["11","10","01","00"]
anser=""
for ans in data:
    qqq=ans
        
    for i in range(1,n+1):
        if ans[i]=="1":
            ans+=str((int(ans[i-1])+s[i])%2)
        else:
            ans+=str((int(ans[i-1])+s[i]+1)%2)
    if ans[n:]==qqq:
        anser=ans[:n]
        break
else:
    print(-1)
    sys.exit()
anser=list(anser)
for i in range(n):
    if anser[i]=="1":
        anser[i]="S"
    else:
        anser[i]="W"
print("".join(anser))

Solution (Python):
def examD():
    N = I()
    s = S()
    ans = ""
    animal = [None, "S", "W"]
    ansC = ""
    for i in range(4):
        cur = ""
        if i==0:
            ansC = [1,1]
        elif i==1:
            ansC = [1,-1]
        elif i==2:
            ansC = [-1,1]
        elif i==3:
            ansC = [-1,-1]
        if (s[0]=="o" and ansC[0]==1) or (s[0]=="x" and ansC[0]==(-1)):
            cur = ansC[1]
        else:
            cur = ansC[1]*(-1)

        for j in range(1,N):
            if (s[j]=="o" and ansC[j]==1) or (s[j]=="x" and ansC[j]==(-1)):
                ansC.append(ansC[j-1])
            else:
                ansC.append(ansC[j-1]*(-1))
        if ansC[N-1]==cur and ansC[N]==ansC[0]:
            for i in ansC[:N]:
                ans = ans + animal[i]
            break
    if not ans:
        ans = int(-1)
    print(ans)


import sys
import copy
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examD()

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 18
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007

n = I()
s = S()
L = [0] * n



def main():
    for i, j in [(1, 0), (0, 1), (0, 0), (1, 1)]:
        L[0] = i
        L[1] = j
        for k in range(1, n - 1):
            if (s[k] == 'o' and L[k]) or (s[k] == 'x' and not L[k]):
                L[k + 1] = L[k - 1]
            else:
                L[k + 1] = L[k - 1] ^ 1

        if (s[n - 1] == 'o' and L[n - 1]) or (s[n - 1] == 'x' and not L[n - 1]):
            first = L[n - 2]
        else:
            first = L[n - 2] ^ 1
        if (s[0] == 'o' and L[0]) or (s[0] == 'x' and not L[0]):
            second = L[n - 1]
        else:
            second = L[n - 1] ^ 1
        if first == L[0] and second == L[1]:
            return L


L = main()
if L:
    print(''.join(['S' if i else 'W' for i in L]))
else:
    print(-1)

Solution (Python):
import sys


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n = int(input().rstrip('\n'))
    s = [True if s == "o" else False for s in list(str(input().rstrip('\n')))]
    ls = [[0] * n for _ in range(4)]
    ls[0][0] = False
    ls[0][1] = False
    ls[1][0] = False
    ls[1][1] = True
    ls[2][0] = True
    ls[2][1] = False
    ls[3][0] = True
    ls[3][1] = True
    for i in range(1, n-1):
        ls[0][i+1] = (s[i] == ls[0][i]) == ls[0][i-1]
        ls[1][i+1] = (s[i] == ls[1][i]) == ls[1][i-1]
        ls[2][i+1] = (s[i] == ls[2][i]) == ls[2][i-1]
        ls[3][i+1] = (s[i] == ls[3][i]) == ls[3][i-1]
    for i in range(4):
        if (s[-1] == ls[i][-1] and ls[i][0] == ls[i][-2]) or (s[-1] != ls[i][-1] and ls[i][0] != ls[i][-2]):
            if (s[0] == ls[i][0] and ls[i][1] == ls[i][-1]) or (s[0] != ls[i][0] and ls[i][1] != ls[i][-1]):
                print("".join(["S" if ls[i][j] else "W" for j in range(n)]))
                exit()
    print(-1)


if __name__ == '__main__':
    solve()

Solution (Python):
def make(l,ai): 
    global N
    for i in range(2,N):
        ai.append(ai[i-2]==(ai[i-1]==l[i-1]))
    return ai

def check(l,ai):
    return ai[0]==(ai[-2]==(ai[-1]==l[-1])) and ai[1]==(ai[-1]==(ai[0]==l[0]))

N=int(input())
l=[c=='o' for c in input()]

f=False
a=[[True,True],[True,False],[False,True],[False,False]]
for ai in a:
    ai=make(l,ai)

f=False
for ai in a:
    if check(l,ai):
        for aij in ai:
            print('S' if aij else 'W',end='')
        f=True
        break
if not f:
    print(-1)

Solution (Python):
N = int(input())
S = input()

opposite = {"S": "W",
            "W": "S"}

for ans in ["SS","SW","WS","WW"]:
  for i in range(1,N-1):
    if (ans[i],S[i]) in [("S","o"),("W","x")]:
      ans += ans[i-1]
    elif (ans[i],S[i]) in [("S","x"),("W","o")]:
      ans += opposite[ans[i-1]]
  
  for i in [-1,0]:
    if ans[i] == "S":
      if (S[i] == "o" and ans[i-1] != ans[i+1]) or (S[i] == "x" and ans[i-1] == ans[i+1]):
        break
    else:
      if (S[i] == "o" and ans[i-1] == ans[i+1]) or (S[i] == "x" and ans[i-1] != ans[i+1]):
        break
  else:
    print(ans)
    break
else:
  print(-1)

Solution (Python):
N = int(input())
S = input()

opposite = {"S": "W",
            "W": "S"}

for ans in ["SS","SW","WS","WW"]:
  for i in range(1,N-1):
    if ans[i] == "S" and S[i] == "o":
      ans += ans[i-1]
    elif ans[i] == "S" and S[i] == "x":
      ans += opposite[ans[i-1]]
    elif ans[i] == "W" and S[i] == "o":
      ans += opposite[ans[i-1]]
    elif ans[i] == "W" and S[i] == "x":
      ans += ans[i-1]
  
  for i in [-1,0]:
    if ans[i] == "S":
      if (S[i] == "o" and ans[i-1] != ans[i+1]) or (S[i] == "x" and ans[i-1] == ans[i+1]):
        break
    else:
      if (S[i] == "o" and ans[i-1] == ans[i+1]) or (S[i] == "x" and ans[i-1] != ans[i+1]):
        break
  else:
    print(ans)
    break
else:
  print(-1)

Solution (Python):
global S,ans

def check_at(i):
  global S,ans

  if ans[i] == "S":
    if (S[i] == "o" and ans[i-1] != ans[i+1]) or (S[i] == "x" and ans[i-1] == ans[i+1]):
      return True
  else:
    if (S[i] == "o" and ans[i-1] == ans[i+1]) or (S[i] == "x" and ans[i-1] != ans[i+1]):
      return True

N = int(input())
S = input()

opposite = {"S": "W",
            "W": "S"}

for ans in ["SS","SW","WS","WW"]:
  for i in range(1,N-1):
    if ans[i] == "S" and S[i] == "o":
      ans += ans[i-1]
    elif ans[i] == "S" and S[i] == "x":
      ans += opposite[ans[i-1]]
    elif ans[i] == "W" and S[i] == "o":
      ans += opposite[ans[i-1]]
    elif ans[i] == "W" and S[i] == "x":
      ans += ans[i-1]
  
  if (not check_at(-1)) and (not check_at(0)):
    print(ans)
    break
else:
  print(-1)

Solution (Python):
def check_at(i,ans,s):
  if ans[i] == "S":
    if (s[i] == "o" and ans[i-1] != ans[i+1]) or (s[i] == "x" and ans[i-1] == ans[i+1]):
      return True
  else:
    if (s[i] == "o" and ans[i-1] == ans[i+1]) or (s[i] == "x" and ans[i-1] != ans[i+1]):
      return True

N = int(input())
S = input()

opposite = {"S": "W",
            "W": "S"}

for ans in ["SS","SW","WS","WW"]:
  for i in range(1,N-1):
    if ans[i] == "S" and S[i] == "o":
      ans += ans[i-1]
    elif ans[i] == "S" and S[i] == "x":
      ans += opposite[ans[i-1]]
    elif ans[i] == "W" and S[i] == "o":
      ans += opposite[ans[i-1]]
    elif ans[i] == "W" and S[i] == "x":
      ans += ans[i-1]
  
  if (not check_at(-1,ans,S)) and (not check_at(0,ans,S)):
    print(ans)
    break
else:
  print(-1)

Solution (Python):
global opposite
opposite = {"S": "W",
            "W": "S"}

def generate(ans,s,N):
  global opposite
  for i in range(1,N-1):
    if ans[i] == "S" and s[i] == "o":
      ans += ans[i-1]
    elif ans[i] == "S" and s[i] == "x":
      ans += opposite[ans[i-1]]
    elif ans[i] == "W" and s[i] == "o":
      ans += opposite[ans[i-1]]
    elif ans[i] == "W" and s[i] == "x":
      ans += ans[i-1]
  
  if ans[-1] == "S":
    if (s[-1] == "o" and ans[-2] != ans[0]) or (s[-1] == "x" and ans[-2] == ans[0]):
      return True
  if ans[-1] == "W":
    if (s[-1] == "o" and ans[-2] == ans[0]) or (s[-1] == "x" and ans[-2] != ans[0]):
      return True
  if ans[0] == "S":
    if (s[0] == "o" and ans[-1] != ans[1]) or (s[0] == "x" and ans[-1] == ans[1]):
      return True
  if ans[0] == "W":
    if (s[0] == "o" and ans[-1] == ans[1]) or (s[0] == "x" and ans[-1] != ans[1]):
      return True
  print(ans)

N = int(input())
S = input()
if generate("SS",S,N):
  if generate("SW",S,N):
    if generate("WS",S,N):
      if generate("WW",S,N):
        print(-1)

Solution (Python):
n = int(input())
s = input()

def loop():
    for i in range(1, n-1):
        if ans[i] == 'S':
            if s[i] == 'x':
                if ans[i-1] == 'S':         
                    ans[i+1] = 'W'
                else:
                    ans[i+1] = 'S'
            else:
                if ans[i-1] == 'W':
                    ans[i+1] = 'W'
                else:
                    ans[i+1] = 'S'
        else:
            if s[i] == 'o':
                if ans[i-1] == 'S':
                    ans[i+1] = 'W'
                else:
                    ans[i+1] = 'S'
            else:
                if ans[i-1] == 'W':
                    ans[i+1] = 'W'
                else:
                    ans[i+1] = 'S'

ans = ['S'] * n
if s[0]  == 'x':
    ans[1] = 'W'
else:
    ans[1] = 'S'
loop()
if ans[n-1] == 'S':
    if (s[n-1] == 'o' and ans[n-2] == 'S') or (s[n-1] == 'x' and ans[n-2] == 'W'):
        print(''.join(ans))
        exit()

ans = ['W'] * (n-1) + ['S']
if s[0]  == 'x':
    ans[1] = 'S'
else:
    ans[1] = 'W'
loop()
if ans[n-1] == 'S':
    if (s[n-1] == 'x' and ans[n-2] == 'S') or (s[n-1] == 'o' and ans[n-2] == 'W'):
        print(''.join(ans))
        exit()

ans = ['S'] * (n-1) + ['W']
if s[0]  == 'x':
    ans[1] = 'S'
else:
    ans[1] = 'W'
loop()
if ans[n-1] == 'W':
    if (s[n-1] == 'x' and ans[n-2] == 'S') or (s[n-1] == 'o' and ans[n-2] == 'W'):
        print(''.join(ans))
        exit()

ans = ['W'] * n
if s[0]  == 'x':
    ans[1] = 'W'
else:
    ans[1] = 'S'
loop()
if ans[n-1] == 'W':
    if (s[n-1] == 'x' and ans[n-2] == 'W') or (s[n-1] == 'o' and ans[n-2] == 'S'):
        print(''.join(ans))
        exit()        

print(-1)