您的位置:首页 > 编程语言 > Python开发

【LintCode 简单】158. 两个字符串是变位词

2018-01-25 22:36 477 查看
1.问题描述:

写出一个函数 
anagram(s,
t)
 判断两个字符串是否可以通过改变字母的顺序变成一样的字符串。

2.样例:

给出 s = 
"abcd"
,t=
"dcab"
,返回 
true
.

给出 s = 
"ab"
, t = 
"ab"
,
返回 
true
.

给出 s = 
"ab"
, t = 
"ac"
,
返回 
false
.

3.代码:
class Solution:
"""
@param s: The first string
@param b: The second string
@return true or false
"""
def anagram(self, s, t):
# write your code here
l1=list(s)
l2=list(t)
length1=len(l1)
length2=len(l2)
if length1!=length2:
return False
else:
for i in range(length1):
for j in range(length2):
fg=False
if l1[i]==l2[j]:
l1[i]=0
l2[j]=0
fg=True
break
if fg==False:
return False
return True
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  LintCode Python 字符串