Similar Strings

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
# Cloudin 云英 面试题
def is_similar(A, B):
dict_a = {}
dict_b = {}
for i in A:
if i in dict_a:
dict_a[i] += 1
else:
dict_a[i] = 1
for j in B:
if j in dict_b:
dict_b[j] += 1
else:
dict_b[j] = 1

if dict_a == dict_b:
return True
else:
return False


if __name__ == "__main__":
A = "aac"
B = "caa"
print(is_similar(A, B))
A = "aac"
B = "caab"
print(is_similar(A, B))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Cloudin 云英 面试题
from collections import Counter


def is_similar_collections(A, B):
cnt_a = Counter(A)
cnt_b = Counter(B)
if cnt_a == cnt_b:
return True
else:
return False


if __name__ == "__main__":
A = "aac"
B = "caa"
print(is_similar_collections(A, B))
A = "aac"
B = "caab"
print(is_similar_collections(A, B))