-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuddyStrings.py
More file actions
69 lines (58 loc) · 1.28 KB
/
buddyStrings.py
File metadata and controls
69 lines (58 loc) · 1.28 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#Source : https://leetcode.com/problems/buddy-strings/
#Author : Yuan Wang
#Date : 2018-07-01
'''
**********************************************************************************
*Given two strings A and B of lowercase letters, return true if and only if we can
*swap two letters in A so that the result equals B.
*
*Example 1:
*
*Input: A = "ab", B = "ba"
*Output: true
*Example 2:**
*
*Input: A = "ab", B = "ab"
*Output: false
*Example 3:
*
*Input: A = "aa", B = "aa"
*Output: true
*Example 4:
*
*Input: A = "aaaaaaabc", B = "aaaaaaacb"
*Output: true
*Example 5:
*
*Input: A = "", B = "aa"
*Output: false
**********************************************************************************/
'''
import collections
def buddyStrings(A, B):
"""
:type A: str
:type B: str
:rtype: bool
"""
if not A or not B: return False
elif len(A) != len(B): return False
elif A==B:
dic=dict(collections.Counter(A))
for i in dic:
if dic[i] > 1:
return True
return False
for i in range(len(A)):
if A[i] != B[i]:
first=i
for j in range(len(A)-1,-1,-1):
if A[j] != B[j]:
second=j
if A[first] == B[second] and B[first] == A[second]:
return True
return False
A=["aaaaaaabc","abab","ab"]
B=["aaaaaaacb","abab","ab"]
for i in range(len(A)):
print(buddyStrings(A[i],B[i]))