-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempCodeRunnerFile.python
More file actions
44 lines (42 loc) · 1.36 KB
/
tempCodeRunnerFile.python
File metadata and controls
44 lines (42 loc) · 1.36 KB
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
ALPHABET = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
t = int(input())
for test_case in range(t):
before = input()
after = input()
becomes = {}
possible = True
for i in range(len(before)):
if before[i] in becomes and becomes[before[i]] != after[i]:
possible = False
becomes[before[i]] = after[i]
if len(set(after)) == 52 and before != after:
possible = False
answer = 0
if possible:
in_degree = {}
for r in ALPHABET:
if r in becomes and becomes[r] != r:
in_degree[becomes[r]] = in_degree.get(becomes[r], 0) + 1
answer += 1
seen = {}
for r in ALPHABET:
if r not in seen:
a = r
while a not in seen:
seen[a] = r
a = becomes.get(a, a)
if a in becomes and a != becomes[a] and seen[a] == r:
s = a
cycle = True
while True:
seen[a] = 'moo'
if in_degree.get(a, 0) > 1:
cycle = False
a = becomes[a]
if a == s:
break
if cycle:
answer += 1
print(answer)
else:
print(-1)