-
Notifications
You must be signed in to change notification settings - Fork 0
/
two-characters.py
38 lines (31 loc) · 921 Bytes
/
two-characters.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
#!/bin/python
# Solution for https://www.hackerrank.com/challenges/two-characters
import sys
import itertools
def trim_except(s, pair):
result = []
for cur in s:
if cur in pair:
result.append(cur)
return ''.join(result)
def is_alternating(s):
if len(s) <= 1:
return True
chrEven = s[0]
chrOdd = s[1]
for idx, cur in enumerate(s):
if not idx % 2 and cur != chrEven:
return False
if idx % 2 and cur != chrOdd:
return False
return True
s_len = int(raw_input().strip())
s = raw_input().strip()
maxAlternating = 0
chrSet = set(s)
chrCombinations = itertools.combinations(chrSet, 2)
for pair in chrCombinations:
sTrimmed = trim_except(s, pair)
if is_alternating(sTrimmed) and len(sTrimmed) > maxAlternating:
maxAlternating = len(sTrimmed)
print maxAlternating