from collections import defaultdict groups = [l.strip().split("\n") for l in open("6.dat").read().strip().split("\n\n")] gca, gcb = [], [] for g in groups: gl = len(g) seen = set() cca = 0 ccb = defaultdict(int) for c in "".join(g): ccb[c] += 1 if c not in seen: cca += 1 seen.add(c) gca.append(cca) gcb.append(sum([v == gl for k, v in ccb.items()])) print("a", sum(gca), "b", sum(gcb))