-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhappy_ladybugs.py
62 lines (50 loc) · 1.37 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
54
55
56
57
58
59
60
61
62
#https://www.hackerrank.com/challenges/happy-ladybugs/problem?isFullScreen=true
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'happyLadybugs' function below.
#
# The function is expected to return a STRING.
# The function accepts STRING b as parameter.
#
def happyLadybugs(b):
# Write your code here
# Initially it feels like
# if there is at least one empty cell
# and count of all colors is greater than 1
# then all the ladybugs can be rearranged to stay happy
# If there is an unhappy ladybug
# then there must be at least one empty cell
unhappy = False
m = dict()
e = 0
for i, item in enumerate(b):
if i > 0 and i < len(b) - 1 and item != "_" and not unhappy:
unhappy = item != b[i - 1] and item != b[i + 1]
if item == "_":
e += 1
elif item not in m:
m[item] = 1
else:
m[item] += 1
if not m:
return 'YES'
for item in m:
if m[item] == 1:
return 'NO'
if e > 0 or not unhappy:
return 'YES'
return 'NO'
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
g = int(input().strip())
for g_itr in range(g):
n = int(input().strip())
b = input()
result = happyLadybugs(b)
fptr.write(result + '\n')
fptr.close()