forked from kamyu104/GoogleKickStart-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindrome_free_strings.py3
35 lines (31 loc) · 1007 Bytes
/
palindrome_free_strings.py3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Copyright (c) 2022 kamyu. All rights reserved.
#
# Google Kick Start 2022 Round A - Problem C. Palindrome Free Strings
# https://codingcompetitions.withgoogle.com/kickstart/round/00000000008cb33e/00000000009e762e
#
# Time: O(N)
# Space: O(N)
#
def check(s):
return len(s) <= 4 or s != s[::-1]
def iter_backtracking(s): # backtrack only the valid states, of which number is O(N)
lookup = {i for i, x in enumerate(s) if x == '?'}
stk = []
i = 0
while i < len(s):
if i in lookup:
s[i] = '0'
stk.append(i)
while not (check(s[i-4:i+1]) and check(s[i-5:i+1])):
if not stk:
return False
i = stk.pop()
s[i] = '1'
i += 1
return True
def palindrome_free_strings():
N = int(input().strip())
S = list(input().strip())
return "POSSIBLE" if iter_backtracking(S) else "IMPOSSIBLE"
for case in range(int(input())):
print('Case #%d: %s' % (case+1, palindrome_free_strings()))