You are viewing a single comment's thread. Return to all comments →
Python hack with itertools combinations:
def is_alternating(s): if len(s) <= 1 or len(set(s)) != 2: return False for i in range(len(s)-1): if s[i] == s[i+1]: return False return True def alternate(s): max_len = 0 chars_set = set(s) if len(chars_set) < 2: return 0 if len(chars_set) == 2: return len(s) if is_alternating(s) else 0 for combo in itertools.combinations(chars_set, 2): allowed = set(combo) new_string = ''.join(c for c in s if c in allowed) res = len(new_string) if is_alternating(new_string) and res > max_len: max_len = res return max_len
Seems like cookies are disabled on this browser, please enable them to open this website
Two Characters
You are viewing a single comment's thread. Return to all comments →
Python hack with itertools combinations: