-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jewel Game
54 lines (41 loc) · 1.04 KB
/
Jewel Game
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
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'getMaxScore' function below.
#
# The function is expected to return an INTEGER.
# The function accepts STRING jewels as parameter.
#
from collections import deque
def getMaxScore(jewels):
score = 0
if len(jewels) > 1:
d_q = deque()
end = 0
while end < len(jewels):
if not d_q:
d_q.append(jewels[end])
end += 1
else:
if d_q[-1] == jewels[end]:
score += 1
end += 1
d_q.pop()
else:
d_q.append(jewels[end])
end += 1
return score
# print(getMaxScore("abcddcbd")) # 3
# print(getMaxScore("abcd")) # 0
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
t = int(input().strip())
for t_itr in range(t):
jewels = input()
ans = getMaxScore(jewels)
fptr.write(str(ans) + '\n')
fptr.close()