n = int(input())
words = [input() for _ in range(n)]
fst = [w[0] for w in words]
hsh = {f: set([word for word in words if word.startswith(f)]) for f in fst }
while any(len(v) > 1 for v in hsh.values()):
_hsh = {}
for k, v in hsh.items():
if len(v) > 1:
fs = set([w[len(k)] for w in v if len(k) < len(w)])
for f in fs:
pref = k + f
_hsh[pref] = set([word for word in words if word.startswith(pref)])
gs = set([w for w in v if len(k) == len(w)])
if len(gs) > 0:
_hsh[k] = gs
else:
_hsh[k] = v
hsh= _hsh
print(hsh)
To embed this program on your website, copy the following code and paste it into your website's HTML: