leetcode 242 有效的字母异位词
2018-03-17 11:17
411 查看
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词。例如,
s = "anagram",t = "nagaram",返回 true
s = "rat",t = "car",返回 false注意:
假定字符串只包含小写字母。提升难度:
输入的字符串包含 unicode 字符怎么办?你能能否调整你的解法来适应这种情况?class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
lens = len(s)
lent = len(t)
if lens != lent:
return False
flag = {}
for p in s:
if p in flag:
flag[p] += 1
else:
flag[p] = 1
print flag
for p in t:
if p not in flag:
return False
elif flag[p] == 0:
return False
flag[p] -= 1
return True
s = "anagram",t = "nagaram",返回 true
s = "rat",t = "car",返回 false注意:
假定字符串只包含小写字母。提升难度:
输入的字符串包含 unicode 字符怎么办?你能能否调整你的解法来适应这种情况?class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
lens = len(s)
lent = len(t)
if lens != lent:
return False
flag = {}
for p in s:
if p in flag:
flag[p] += 1
else:
flag[p] = 1
print flag
for p in t:
if p not in flag:
return False
elif flag[p] == 0:
return False
flag[p] -= 1
return True
相关文章推荐
- LeetCode242有效的字母异位词
- Leetcode 49. 字母异位词分组
- LeetCode 242 Valid Anagram(有效字谜)(*)
- 【初级算法】15. 有效的字母异位词
- Leetcode 242 Valid Anagram 字符串处理
- 有效的字母异位词
- leetCode 65.Valid Number (有效数字)
- LeetCode 32. Longest Valid Parentheses(最长有效括号)
- [leetcode]20. Valid Parentheses ,有效的括号符串python实现【easy】
- LeetCode-242 Valid Anagram
- LeetCode242 Valid Anagram
- LeetCode242——Valid Anagram
- LeetCode 242 Valid Anagram
- 【LeetCode】242 - Valid Anagram
- Leetcode:Generate Parentheses 生成有效括号对
- [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母
- [Leetcode] valid parentheses 有效括号对
- leetcode--242-- Valid Anagram
- LeetCode 242:Valid Anagram
- 【LeetCode】(242)Valid Anagram(Easy)