-
Notifications
You must be signed in to change notification settings - Fork 223
/
Copy pathhappy-ladybugs.py
53 lines (36 loc) · 1.01 KB
/
happy-ladybugs.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/python3
import os
import sys
from collections import Counter
#
# Complete the happyLadybugs function below.
#
def is_happy(b):
if b[0] != b[1] or b[-1] != b[-2]:
return False
for ind in range(1, len(b)-1):
if b[ind] != b[ind-1] and b[ind] != b[ind+1]:
return False
return True
def happyLadybugs(b):
cnt = Counter(b)
print("cnt = {}".format(cnt))
singles = list(filter(lambda x: x[0] != '_' and x[1] == 1, cnt.items()))
empty = b.count('_')
print("singles = {}".format(singles))
print("empty = {}".format(empty))
if len(singles) == 0 and empty > 0:
return 'YES'
elif len(b) > 2 and is_happy(b):
return 'YES'
else:
return 'NO'
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
g = int(input())
for g_itr in range(g):
n = int(input())
b = input()
result = happyLadybugs(b)
fptr.write(result + '\n')
fptr.close()