1809A - Garland - 800
On This Page
1809A - Garland (implementation, 800)
Explanation
- If all the light bulbs have the same color, it is impossible to turn them all on, as you can’t perform the operation on the same color consecutively. In this case, print -1.
- If there are 3 light bulbs of the same color and one light bulb of a different color, it takes 6 operations to turn them all on:
- Turn on the different colored light bulb
- Turn on one of the other colored light bulbs
- Turn off the different colored light bulb
- Turn on the second light bulb of the same color
- Turn on the different colored light bulb
- Turn on the third light bulb of the same color
- In all other cases, it takes 4 operations to turn all the light bulbs on, as you can switch the light bulbs on in a sequence without violating the color restriction.
Solution
def solve(s):
if all(c == s[0] for c in s):
return -1
elif s.count(s[0]) == 3 or s.count(s[1]) == 3:
return 6
else:
return 4
t = int(input())
for _ in range(t):
s = input()
print(solve(s))