TVP

# LintCode 1510.亲密字符串

1.2.3.andconsist only of lowercase letters.

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

1.例子5，长度不相同，

2.例子4， 2个位不同，交换后得到相同结果，例子3， 2个位相同，交换后仍然相同，考虑不同记录数，如果大于2或者等于1，交换两个不同位不可能得到相同结果，或者不可能交换2个不同位，

classSolution:

"""

@param A: string A

@param B: string B

@return: bool

"""

defbuddyStrings(self,A,B):

ifnotlen(A)==len(B):

returnFalse

length=len(A)

count=

forxinrange(length):

ifnotA[x]==B[x]:

count+=1

ifcount>2orcount==1:

returnFalse

ifnotset(A)==set(B):

returnFalse

returnTrue

"ab"

"ab"

true

false

classSolution:

"""

@param A: string A

@param B: string B

@return: bool

"""

defbuddyStrings(self,A,B):

#第一次增加判断

ifA==B:

returnFalse

#

ifnotlen(A)==len(B):

returnFalse

length=len(A)

count=

forxinrange(length):

ifnotA[x]==B[x]:

count+=1

ifcount>2orcount==1:

returnFalse

ifnotset(A)==set(B):

returnFalse

returnTrue

"aa"

"aa"

false

true

classSolution:

"""

@param A: string A

@param B: string B

@return: bool

"""

defbuddyStrings(self,A,B):

#第二次增加判断

ifA==Bandlen(set(A))>1:

returnFalse

#

ifnotlen(A)==len(B):

returnFalse

length=len(A)

count=

forxinrange(length):

ifnotA[x]==B[x]:

count+=1

ifcount>2orcount==1:

returnFalse

ifnotset(A)==set(B):

returnFalse

returnTrue

"abcaa"

"abcbb"

true

false

A、Bset相同，但元素数量不同，继续增加排序结果相同的判断，

classSolution:

"""

@param A: string A

@param B: string B

@return: bool

"""

defbuddyStrings(self,A,B):

#第三次增加判断

ifnotsorted(A)==sorted(B):

returnFalse

#

ifA==Bandlen(set(A))>1:

returnFalse

ifnotlen(A)==len(B):

returnFalse

length=len(A)

count=

forxinrange(length):

ifnotA[x]==B[x]:

count+=1

ifcount>2orcount==1:

returnFalse

ifnotset(A)==set(B):

returnFalse

returnTrue

"abcabc"

"abcabc"

false

true

classSolution:

"""

@param A: string A

@param B: string B

@return: bool

"""

defbuddyStrings(self,A,B):

ifnotsorted(A)==sorted(B):

returnFalse

ifA==Bandlen(set(A))>1:

#第四次增加判断

iflen(A)>2:

returnTrue

else:

#

returnFalse

ifnotlen(A)==len(B):

returnFalse

length=len(A)

count=

forxinrange(length):

ifnotA[x]==B[x]:

count+=1

ifcount>2orcount==1:

returnFalse

#可以删掉的判断

ifnotset(A)==set(B):

returnFalse

#

returnTrue

ifnotset(A)==set(B):

returnFalse

• 发表于:
• 原文链接https://kuaibao.qq.com/s/20190216G13CTK00?refer=cp_1026
• 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号（企鹅号）传播渠道之一，根据《腾讯内容开放平台服务协议》转载发布内容。
• 如有侵权，请联系 cloudcommunity@tencent.com 删除。

2023-03-26

2023-03-26

2023-03-26

2023-03-26

2023-03-26

2023-03-26

2018-03-12

2018-06-14

2023-03-26

2023-03-26

2023-03-26

10元无门槛代金券