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):
n=int(input())
s=list(input())

for a in [0,1]:
    for b in [0,1]:
        
        
        ass=[-1]*n
        ass[0]=a
        ass[1]=b
        
        for i in range(1,n-1):
            if ass[i]==0:
                if s[i]=='o':
                    ass[i+1]=1-ass[i-1]
                else:
                    ass[i+1]=ass[i-1]
            else:
                if s[i]=='o':
                    ass[i+1]=ass[i-1]
                else:
                    ass[i+1]=1-ass[i-1]

        if ass[0]==1:
            if (s[0]=='o' and ass[-1]!=ass[1]) or (s[0]=='x' and ass[-1]==ass[1]):
                continue
        else:
            if (s[0]=='o' and ass[-1]==ass[1]) or (s[0]=='x' and ass[-1]!=ass[1]):
                continue
        if ass[-1]==1:
            if (s[-1]=='o' and ass[n-2]!=ass[0]) or (s[-1]=='x' and ass[n-2]==ass[0]):
                continue
        else:
            if (s[-1]=='o' and ass[n-2]==ass[0]) or (s[-1]=='x' and ass[n-2]!=ass[0]):
                continue
        ass=['W' if i==0 else 'S' for i in ass]
        print(''.join(ass))
        exit()
print(-1)

Solution (Python):
import itertools

N = int(input())
S = [True if x == 'o' else False for x in input()]
S.append(S[0])
S.append(S[1])
lst = [None] * (N + 2)
for x0, x1 in itertools.product((True, False), repeat=2):
    lst[0], lst[1] = x0, x1
    for i in range(1, N + 1):
        lst[i + 1] = lst[i] ^ S[i] ^ lst[i - 1]
    if (lst[N], lst[N + 1]) == (lst[0], lst[1]):
        print(''.join(map(str, ('S' if x else 'W' for x in lst[:-2]))))
        break
else:
    print(-1)

Solution (Python):
def solve(n, S):
    for i in range(4):
        ans = list()
        t = i
        for j, s in enumerate(S):
            f0 = t // 2
            f1 = t % 2
            f2 = f0 ^ (0 if s == 'o' else 1)
            ans.append('S' if f2 == 0 else 'W')
            t = ((f1 ^ f2) << 1) | f2

        if (i == t) :
            x = ans[:-1]
            x.insert(0, ans[-1])
            return "".join(x)
    return "-1"

if __name__ == '__main__':
    print(solve(int(input()), input()))

Solution (Python):
n = int(input())
s = input()
a = "SS"
ct=1
while ct<7:    
    for i in range(n-1):
        p=a[i]
        q="A"
        if a[i]=="S":
            q="W"
        else:
            q="S"
        if a[i+1]=="S":
            if s[i+1] == "o":
                a=a+a[i]
            else:
                a=a+q
        else:
            if s[i+1] == "o":
                 a=a+q
            else:
                a=a+a[i]
    if a[0]==a[n]:
        if a[0]=="S" and s[0]=="o" and a[n-1]==a[1]:
            print(a[0:n])
            break
        elif a[0]=="S" and s[0] !="o" and a[n-1]!=a[1]:         
            print(a[0:n])
            break
        elif a[0]=="W" and s[0] =="o" and a[n-1]!=a[1]: 
            print(a[0:n])
            break
        elif a[0]=="W" and s[0] !="o" and a[n-1]==a[1]: 
            print(a[0:n])
            break
        else:
            ct=ct+1
        if ct==2:
            a="SW"
        elif ct==3:
            a="WS"
        elif ct==4:
            a="WW"
        else:
            print(-1)
            break
    else:
        ct=ct+1
        if ct==2:
            a="SW"
        elif ct==3:
            a="WS"
        elif ct==4:
            a="WW"
        else:
            print(-1)
            break

Solution (Python):
N = int(input())
S = list(input())
ans = -1
for x in ['SS', 'SW', 'WS', 'WW']:
    for i, s in enumerate(S):
        if s=='o':
            if x[-1]=='S':
                x += x[-2]
            else:
                if x[-2]=='S':
                    x += 'W'
                else:
                    x += 'S'
        else:
            if x[-1] == 'S':
                if x[-2]=='S':
                    x += 'W'
                else:
                    x += 'S'
            else:
                x += x[-2]
    if x[0]==x[-2] and x[1]==x[-1]:
        ans = x[1:-1]
        break
print(ans)

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

x = [0]*N
v = ["S","W"]

for x[0],x[1] in [["S","S"],["S","W"],["W","S"],["W","W"]]:
    for i in range(2,N):
        if (x[i-1] =="S" and s[i-1] == "o") or (x[i-1] =="W" and s[i-1] == "x"):
            x[i] = x[i-2]
        else:
            if x[i-2] == "S":
                x[i] = "W"
            else:
                x[i] = "S"
    
    det = 0
    if (x[N-1] == "S" and s[N-1] == "o" and x[N-2] != x[0]):
        det = -1
    elif (x[N-1] == "S" and s[N-1] == "x" and x[N-2] == x[0]):
        det = -1
    elif (x[N-1] == "W" and s[N-1] == "o" and x[N-2] == x[0]):
        det = -1
    elif (x[N-1] == "W" and s[N-1] == "x" and x[N-2] != x[0]):
        det = -1
    elif (x[0] == "S" and s[0] == "o" and x[N-1] != x[1]):
        det = -1
    elif (x[0] == "S" and s[0] == "x" and x[N-1] == x[1]):
        det = -1
    elif (x[0] == "W" and s[0] == "o" and x[N-1] == x[1]):
        det = -1
    elif (x[0] == "W" and s[0] == "x" and x[N-1] != x[1]):
        det = -1
    
    if det == 0:
        print("".join(x))
        break

if det == -1:
    print(-1)

Solution (Python):
def add_ans(i,ans,circle):
    if circle[i] == 'o':
        if ans[i] == 'S':
            ans += ans[i-1]

        else:
            if ans[i-1] == 'S':
                ans += 'W'
            else:
                ans += 'S'
            
        
        
    if circle[i] == 'x':
        if ans[i] == 'W':
            ans += ans[i-1]
        
        else:
            if ans[i-1] == 'S':
                ans += 'W'
            else:
                ans += 'S'
            
    return ans
def check_last(ans,circle):
    if circle[0] == 'o':
        if ans[0] == 'S':
            last  = ans[1]

        else:
            if ans[1] == 'S':
                last = 'W'
            else:
                last = 'S'



    elif circle[0] == 'x':
        if ans[0] == 'W':
            last = ans[1]

        else:
            if ans[1] == 'S':
                last = 'W'
            else:
                last = 'S'
                
                
    if circle[-1] == 'o':
        if ans[-1] == 'S':
            first  = ans[-2]

        else:
            if ans[-2] == 'S':
                first = 'W'
            else:
                first = 'S'



    elif circle[-1] == 'x':
        if ans[-1] == 'W':
            first = ans[-2]

        else:
            if ans[-2] == 'S':
                first = 'W'
            else:
                first = 'S'
                
                
    if (last == ans[-1]) & (first == ans[0]):
        return 1
    else :
        return 0


flag = True
n = int(input())
circle = input()

ans_list = ['SS' , 'SW' , 'WS' , 'WW']
for ans in ans_list:
    for i in range(1,len(circle)-1):
        ans = add_ans(i,ans,circle)


    if check_last(ans , circle) == 1:
        print(ans)
        flag = False
        break

if flag:
    print('-1')

Solution (Python):
def add_ans(i,ans,circle):
    if circle[i] == 'o':
        if ans[i] == 'S':
            ans += ans[i-1]

        else:
            if ans[i-1] == 'S':
                ans += 'W'
            else:
                ans += 'S'
            
        
        
    if circle[i] == 'x':
        if ans[i] == 'W':
            ans += ans[i-1]
        
        else:
            if ans[i-1] == 'S':
                ans += 'W'
            else:
                ans += 'S'
            
    return ans
def check_last(ans,circle):
    if circle[0] == 'o':
        if ans[0] == 'S':
            last  = ans[1]

        else:
            if ans[1] == 'S':
                last = 'W'
            else:
                last = 'S'



    if circle[0] == 'x':
        if ans[0] == 'W':
            last = ans[1]

        else:
            if ans[1] == 'S':
                last = 'W'
            else:
                last = 'S'
                
                
    if circle[-1] == 'o':
        if ans[-1] == 'S':
            first  = ans[-2]

        else:
            if ans[-2] == 'S':
                first = 'W'
            else:
                first = 'S'



    if circle[-1] == 'x':
        if ans[-1] == 'W':
            first = ans[-2]

        else:
            if ans[-2] == 'S':
                first = 'W'
            else:
                first = 'S'
                
                
    if (last == ans[-1]) & (first == ans[0]):
        return 1
    else :
        return 0


flag = True
n = int(input())
circle = input()

ans_list = ['SS' , 'SW' , 'WS' , 'WW']
for ans in ans_list:
    for i in range(1,len(circle)-1):
        ans = add_ans(i,ans,circle)


    if check_last(ans , circle) == 1:
        print(ans)
        flag = False
        break

if flag:
    print('-1')

Solution (Python):
N=int(input())
s=input()
g=0
def ki(a,b):
    global g
    l=[None]*N
    l[-2],l[-1]=a,b
    for i in range(-1,N-1):
        if l[i-1]==l[i] and s[i]=="o":
            l[i+1]=0
        elif l[i-1]!=l[i] and s[i]=="x":
            l[i+1]=0
        else:
            l[i+1]=1
    if l[-2]==a and l[-1]==b and g==0:
        g=1
        for i in l:
            print("W" if i else"S",end="")
ki(0,0)
ki(0,1)
ki(1,0)
ki(1,1)
if not g:
    print(-1)

Solution (Python):
import sys
n=int(input())
s=str(input())
l=[0]*n
flag=1

if s[0]=='x':
  flag=0
  new_s=''
  for j in range(n):
    if s[j]=='x':
      new_s+='o'
    else:
      new_s+='x'
  s=new_s
  
def check(i,l):
  sys.setrecursionlimit(10000000)
  if i==0:
    l11=[0]*n
    l12=[0]*n
    l21=[0]*n
    l22=[0]*n
    l11[0]='S'
    l11[1]=l11[n-1]='S'
    l12[0]='S'
    l12[1]=l12[n-1]='W'
    tmp1=check(i+1,l11)
    tmp2=check(i+1,l12)
    l21[0]=l21[n-1]='W'
    l21[1]='S'
    l22[0]=l22[1]='W'
    l22[n-1]='S'
    tmp3=check(i+1,l21)
    tmp4=check(i+1,l22)
    if tmp1==tmp2==tmp3==tmp4==-1:
      return -1
    else:
      if tmp1!=-1:
        return tmp1
      if tmp2!=-1:
        return tmp2
      if tmp3!=-1:
        return tmp3
      if tmp4!=-1:
        return tmp4
  elif 1<=i<=n-2:
    if s[i]=='o':
      if l[i]=='S':
        l[i+1]=l[i-1]
        return check(i+1,l)
      else:
        if l[i-1]=='W':
          l[i+1]='S'
          return check(i+1,l)
        else:
          l[i+1]='W'
          return check(i+1,l)
    else:
      if l[i]=='W':
        l[i+1]=l[i-1]
        return check(i+1,l)
      else:
        if l[i-1]=='S':
          l[i+1]='W'
          return check(i+1,l)
        else:
          l[i+1]='S'
          return check(i+1,l)
  else:
    if s[i]=='o':
      if l[i]=='S':
        if l[i-1]==l[0]:
          if (l[0]=='W' and l[i]==l[1]) or (l[0]=='S' and l[i]!=l[1]):
            return -1
          else:
            return l
        else:
          return -1
      else:
        if l[i-1]!=l[0]:
          if (l[0]=='W' and l[i]==l[1]) or (l[0]=='S' and l[i]!=l[1]):
            return -1
          else:
            return l
        else:
          return -1
    else:
      if l[i]=='S':
        if l[i-1]!=l[0]:
          if (l[0]=='W' and l[i]==l[1]) or (l[0]=='S' and l[i]!=l[1]):
            return -1
          else:
            return l
        else:
          return -1
      else:
        if l[i-1]==l[0]:
          if (l[0]=='W' and l[i]==l[1]) or (l[0]=='S' and l[i]!=l[1]):
            return -1
          else:
            return l
        else:
          return -1      
ans=check(0,l)
if ans==-1:
  print(-1)
else:
  if flag:
    print(''.join(ans))
  else:
    for i in range(n):
      if ans[i]=='S':
        ans[i]='W'
      else:
        ans[i]='S'
    print(''.join(ans))

Solution (Python):
import sys

n = int(input())
s = input()

array = [[True, True], [True, False], [False, False], [False, True]]

for i in range(1, n):
  for v in array:
    flag = ((s[i] == 'o') == v[i])
    v.append((flag == v[i-1]))
    
for v in array:
  flag1 = (v[1] == v[-2])
  flag2 = ((s[0] == 'o') == v[0])
  flag = (flag1 == flag2)
  
  if v[0] == v[-1] and flag:
    for c in v[0:len(v)-1]:
      if c:
        print('S', end='')
      else:
        print('W', end='')
        
    print()
    
    sys.exit(0)
    
print(-1)

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

def make_list(li):
    for i in range(2, n):
        if li[i-1] == 'S':
            if s[i-1] == 'o':
                li[i] = li[i-2]
            else:
                if li[i-2] == 'S':
                    li[i] = 'W'
                else:
                    li[i] = 'S'
        else:
            if s[i-1] == 'o':
                if li[i-2] == 'S':
                    li[i] = 'W'
                else:
                    li[i] = 'S'
            else:
                li[i] = li[i-2]

    hantei(li)

def hantei(li):
    flag = True
    for i in range(2):
        if li[-i] == 'S':
            if s[-i] == 'o':
                if li[-1-i] != li[1-i]:
                    flag = False
            else:
                if li[-1-i] == li[1-i]:
                    flag = False
        else:
            if s[-i] == 'o':
                if li[-1-i] == li[1-i]:
                    flag = False
            else:
                if li[-1-i] != li[1-i]:
                    flag = False

    if flag:
        print_list(li)

def print_list(li):
    for i in li:
        print(i, end = '')
    exit()

li = [[''] * n for _ in range(4)]
li[0][0] = 'S'
li[0][1] = 'S'
li[1][0] = 'S'
li[1][1] = 'W'
li[2][0] = 'W'
li[2][1] = 'S'
li[3][0] = 'W'
li[3][1] = 'W'

for i in range(4):
    make_list(li[i])

print(-1)

Solution (Python):
import sys
INF = 10**10
MOD = 10**9 + 7
sys.setrecursionlimit(100000000)
from functools import lru_cache
from copy import deepcopy 

def main():
    n = int(input())
    s = input()
    flag = False  
    
    for bit in range(4):
        ani0 = (bit>>0)&1
        ani1 = (bit>>1)&1

        if ani0 == 0:
            ans = ['S']
        else:
            ans = ['W']

        if ani1 == 0:
            ans.append('S')
        else:
            ans.append('W')
        
        before2 = ani0
        before1 = ani1
        for i in range(n - 1):
            if s[i + 1] == 'o':
                tmp = before2^before1
            else:
                tmp = before2^before1^1
            
            before1,before2 = tmp,before1

            if i != n - 2:
                if before1:
                    ans.append('W')
                else:
                    ans.append('S')
        
        if s[0] == 'o':
            tmp = before2^before1
        else:
            tmp = before2^before1^1
        
        before1,before2 = tmp,before1

        if before1 == ani1 and before2 == ani0:
            flag = True
            break
    
    if flag:
        print(''.join(ans))
    else:
        print(-1)

if __name__=='__main__':
    main()

Solution (Python):
N = int(input())
s = input()
dic = ['SS','SW','WS','WW']
for i in range(4):
  ans = dic[i]
  state = ans[-1]
  for j in range(1,N):
    if state=='S':
      if s[j]=='o':
        ans += ans[j-1]
      else:
        if ans[j-1]=='S':
          ans += 'W'
        else:
          ans += 'S'
    else:
      if s[j]=='o':
        if ans[j-1]=='S':
          ans += 'W'
        else:
          ans += 'S'
      else:
        ans += ans[j-1]
    state = ans[-1]
  if ans[0]==ans[-1]:
    ans = ans[:-1]
    if state=='S':
      if s[0]=='o':
        if ans[1]==ans[-1]:
          print(ans)
          break
      else:
        if ans[1]!=ans[-1]:
          print(ans)
          break
    else:
      if s[0]=='o':
        if ans[1]!=ans[-1]:
          print(ans)
          break
      else:
        if ans[1]==ans[-1]:
          print(ans)
          break
else:
  print(-1)

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

def check(ox, l, m, r):
    if l == r:
        if (m == 'S' and ox == 'o') or (m == 'W' and ox == 'x'):
            return True
        else:
            return False
    else:
        return not check(ox, l, m, l)

for one in 'SW':
    for two in 'SW':
        a = one + two
        for i in range(2, n):
            if check(s[i-1], a[i-2], a[i-1], 'S'):
                a += 'S'
            else:
                a += 'W'
        if check(s[n-1], a[n-2], a[n-1], a[0]) and check(s[0], a[n-1], a[0], a[1]):
            print(a)
            exit()
print(-1)

Solution (Python):




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


def simu(ret):
    for i, ss in enumerate(s[1:] + s[:1], start=1):
        if ret[i] == 'S' and ss == 'o':
            ret += ret[i - 1]
        elif ret[i] == 'S' and ss == 'x':
            ret += 'W' if ret[i - 1] == 'S' else 'S'
        elif ret[i] == 'W' and ss == 'o':
            ret += 'W' if ret[i - 1] == 'S' else 'S'
        elif ret[i] == 'W' and ss == 'x':
            ret += ret[i - 1]
    return ret


def check(ret):
    if ret[-2] != ret[0]:  
        return False
    if ret[-1] != ret[1]:
        return False
    return True
    
    
    
    
    
    
    
    
    



for ret in ['SS', 'SW', 'WS', 'WW']:
    ret1 = simu(ret)
    
    if check(ret1):
        print(ret1[:-2])
        exit()
print(-1)

Solution (Python):




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


def simu(ret):
    for i, ss in enumerate(s[1:], start=1):
        if ret[i] == 'S' and ss == 'o':
            ret += ret[i - 1]
        elif ret[i] == 'S' and ss == 'x':
            ret += 'W' if ret[i - 1] == 'S' else 'S'
        elif ret[i] == 'W' and ss == 'o':
            ret += 'W' if ret[i - 1] == 'S' else 'S'
        elif ret[i] == 'W' and ss == 'x':
            ret += ret[i - 1]
    return ret


def check(ret):
    ss = s[0]
    if ret[-1] != ret[0]:
        return False
    if ret[0] == 'S' and ss == 'o':
        return ret[-2] == ret[1]
    elif ret[0] == 'S' and ss == 'x':
        return ret[-2] != ret[1]
    elif ret[0] == 'W' and ss == 'o':
        return ret[-2] != ret[1]
    elif ret[0] == 'W' and ss == 'x':
        return ret[-2] == ret[1]



for ret in ['SS', 'SW', 'WS', 'WW']:
    ret1 = simu(ret)
    
    if check(ret1):
        print(ret1[:-1])
        exit()
print(-1)

Solution (Python):
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    s=input()
    s2=[0]*N
    for i in range(N):
        if s[i]=="x":
            s2[i]=1
        
    
    
    ans=-1
    for a in range(2):
        if ans==-1:
            for b in range(2):
                L=[0]*N
                L[0]=a
                L[1]=b
                for i in range(2,N):
                    L[i]=(L[i-2]+L[i-1]+s2[i-1])%2
                
                
                if L[0]==(L[-2]+L[-1]+s2[-1])%2 and L[1]==(L[-1]+L[0]+s2[0])%2:
                    ans=1
                    break
        
        
    if ans==-1:
        print(ans)
    else:
        ans=["S"]*N
        for i in range(N):
            if L[i]==1:
                ans[i]="W"
                
        print(''.join(map(str, ans)))
            
                
            

main()

Solution (Python):
import sys
input = sys.stdin.readline

N = int(input())
s = input().rstrip()

start = ["WS", "WW", "SS", "SW"]

for A in start:
    A = list(A)
    for i in range(1, N):
        if (A[i] == "W" and s[i] == "o") or (A[i] == "S" and s[i] == "x"):
            if A[i - 1] == "S":
                A.append("W")
            else:
                A.append("S")
        else:
            A.append(A[i - 1])
    if len(A) == N + 1 and A[0] == A[-1]:
        A = A[:N]
        if (A[0] == "W" and s[0] == "o") or (A[0] == "S" and s[0] == "x"):
            if A[1] != A[-1]:
                print("".join(A))
                sys.exit()
        else:
            if A[-1] == A[1]:
                print("".join(A))
                sys.exit()
print(-1)

Solution (Python):
from itertools import product

n = int(input())
s = input()

bl = False
for ef, el in product([1, -1], repeat=2):
    zoo = [0] * n
    zoo[0] = ef
    zoo[-1] = el
    for i, e in enumerate(s):
        if e == "o":
            if zoo[i] == 1:
                nxt = zoo[i-1]
            else:
                nxt = -zoo[i-1]

        else:
            if zoo[i] == 1:
                nxt = -zoo[i-1]
            else:
                nxt = zoo[i-1]

        if i < n - 2:
            zoo[i+1] = nxt

        elif nxt != zoo[(i+1)%n]:
            break

    else:
        bl = True
        break

if bl:
    ans = ["S" if e == 1 else "W" for e in zoo]
    print(*ans, sep="")

else:
    print(-1)

Solution (Python):
def ox_sw(ox,sw,prev):
    if ox =="o":
        if sw == 1:
            ans = prev
        else:
            ans = -1*prev
    elif ox =="x" :
        if sw ==1:
            ans = -1*prev
        else:
            ans = prev
    return ans

n = int(input())
s  = input()
a1 = [1,1]   
a2 = [1,-1]
a3 = [-1,1]
a4 = [-1,-1]
f= False

for a in [a1,a2,a3,a4]:
    for i in range(1,n):
        a.append(ox_sw(s[i],a[i],a[i-1]))
    if a[n] == a[0] and a[1] == ox_sw(s[0],a[0],a[-2]):
        f = True
        break

if f:
    ans = []
    for i in range(n):
        if a[i]==1:
            ans.append("S")
        else:
            ans.append("W")
    print("".join(ans))
else:
    print(-1)

Solution (Python):


def main():
    import sys
    
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    
    from itertools import combinations, permutations
    
    from bisect import bisect_left,bisect_right
    from math import floor, ceil
    

    

    N = int(input())
    s = list(input())
    s += s[0]

    def check(p):
        for i in range(1, N+1):
            if s[i]=='o' and p[i]=='S':
                p += p[i-1]
            elif s[i]=='o' and p[i]=='W':
                if p[i-1]=='S':
                    p += 'W'
                else:
                    p += 'S'
            elif s[i]=='x' and p[i]=='S':
                if p[i-1]=='S':
                    p += 'W'
                else:
                    p += 'S'
            else:
                p += p[i-1]
        if p[0]==p[N] and p[1]==p[N+1]:
            print(p[:-2])
            exit()

    for i in ['SS', 'SW', 'WS', 'WW']:
        check(i)
    print(-1)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import sys
    
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    
    from itertools import combinations, permutations
    
    from bisect import bisect_left,bisect_right
    from math import floor, ceil
    

    

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

    def check(p):
        for i in range(1, N):
            if s[i]=='o' and p[i]=='S':
                p += p[i-1]
            elif s[i]=='o' and p[i]=='W':
                if p[i-1]=='S':
                    p += 'W'
                else:
                    p += 'S'
            elif s[i]=='x' and p[i]=='S':
                if p[i-1]=='S':
                    p += 'W'
                else:
                    p += 'S'
            else:
                p += p[i-1]
        if p[0]==p[N]:
            if s[0]=='o' and p[0]=='S':
                if p[N-1]==p[1]:
                    print(p[:N])
                    exit()
            elif s[0]=='x' and p[0]=='S':
                if p[N-1]!=p[1]:
                    print(p[:N])
                    exit()
            elif s[0]=='o' and p[0]=='W':
                if p[N-1]!=p[1]:
                    print(p[:N])
                    exit()
            else:
                if p[N-1]==p[1]:
                    print(p[:N])
                    exit()

    for i in ['SS', 'SW', 'WS', 'WW']:
        check(i)
    print(-1)
    


if __name__ == '__main__':
    main()

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


def check(a, b):
    v = [0]*N
    v[0] = a
    v[1] = b
    for i in range(1, N-1):
        if s[i] == 'o':
            if v[i]:
                v[i+1] = v[i-1]
            else:
                v[i+1] = not v[i-1]
        else:
            if v[i]:
                v[i+1] = not v[i-1]
            else:
                v[i+1] = v[i-1]

    ok = True
    for i in range(N):
        same = v[(i-1) % N] == v[(i+1) % N]
        if s[i] == 'o':
            if v[i] != same:
                ok = False
        else:
            if v[i] == same:
                ok = False

    return v if ok else None


p = [
    [True, True],
    [False, False],
    [True, False],
    [False, True]
]
ans = "-1"
for a, b in p:
    l = check(a, b)
    if l != None:
        ans = "".join(["S" if v else "W" for v in l])
        break
print(ans)

Solution (Python):
from itertools import product

def next_sw(ox, sw, prev_sw):
    if (ox == 'o' and sw == 'S') or (ox == 'x' and sw == 'W'):
        return prev_sw 
    else:
        return 'W' if prev_sw =='S' else 'S'

N = int(input())
S = input()
ans = [None] * (N+1)
ok = False
for ans[0], ans[1] in product('SW', 'SW'):
    for i in range(1, N):
        ans[i+1] = next_sw(S[i], ans[i], ans[i-1])
    if ans[0] == ans[-1] and ans[1] == next_sw(S[0], ans[0], ans[-2]):
        ok = True
        break
if ok:
    print(''.join(ans[:N]))
else:
    print(-1)

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



def check(li):
    for i in range(1,n-1):
        if li[i] == 'S':
            if li[i-1] == 'S':
                if s[i] == 'o':
                    li += ['S']
                else:
                    li += ['W']
            else:
                if s[i] == 'o':
                    li += ['W']
                else:
                    li += ['S']
        else:
            if li[i-1] == 'S':
                if s[i] == 'o':
                    li += ['W']
                else:
                    li += ['S']
            else:
                if s[i] == 'o':
                    li += ['S']
                else:
                    li += ['W']
    if li[-1] == 'S':
        if s[-1] == 'o':
            if li[-2] != li[0]:
                return False
        else:
            if li[-2] == li[0]:
                return False
    else:
        if s[-1] == 'o':
            if li[-2] == li[0]:
                return False
        else:
            if li[-2] != li[0]:
                return False

    if li[0] == 'S':
        if s[0] == 'o':
            if li[-1] != li[1]:
                return False
        else:
            if li[-1] == li[1]:
                return False
    else:
        if s[0] == 'o':
            if li[-1] == li[1]:
                return False
        else:
            if li[-1] != li[1]:
                return False
    return li
for i in range(2):
    for j in range(2):
        s1 = 'S' if i == 1 else 'W'
        s2 = 'S' if j == 1 else 'W'
        k = check([s1,s2])
        if k:
            print("".join(k))
            exit()
print(-1)

Solution (Python):
import sys


def calc(s, x, i):
    if s[i] == "S":
        if x == "o":
            return s[i-1]
        elif s[i-1] == "S":
            return "W"
        else:
            return "S"
    else:
        if x == "x":
            return s[i-1]
        elif s[i-1] == "S":
            return "W"
        else:
            return "S"


n = int(input())
s = input()
for s1 in ["S", "W"]:
    for sn in ["S", "W"]:
        ans = [s1] + [""] * (n-2) + [sn]
        for i in range(n-1):
            ans[i+1] = calc(ans, s[i], i)
        zn = calc(ans, s[-2], n-2)
        z1 = calc(ans, s[-1], n-1)
        if zn == sn and z1 == s1:
            print("".join(ans))
            sys.exit()

print(-1)