-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathbuddy-strings.py
More file actions
36 lines (35 loc) · 926 Bytes
/
buddy-strings.py
File metadata and controls
36 lines (35 loc) · 926 Bytes
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
class Solution:
"""
@param A: string A
@param B: string B
@return: bool
"""
def buddyStrings(self, A, B):
# Write your code here
if len(A) != len(B):
return False
di = {}
for i in range(len(A)):
if not A[i] in di:
di[A[i]] = 1
else:
di[A[i]] += 1
if A == B:
for k, v in di.items():
if v >= 2:
return True
return False
for i in range(len(A)):
if not B[i] in di:
di[B[i]] = -1
else:
di[B[i]] -= 1
for k, v in di.items():
if v != 0:
return False
cnt = 0
for i in range(len(A)):
if A[i] != B[i]:
cnt += 1
return cnt == 2
# easy: https://www.lintcode.com/problem/buddy-strings/