描述
Given two strings s and t , write a function to determine if t is an anagram of s.
Example 1:
Input: s = "anagram", t = "nagaram"
Output: true
Example 2:
Input: s = "rat", t = "car"
Output: false
Note:
You may assume the string contains only lowercase alphabets.
解析
根据题意,就是找出 s 和 t 两个字符串是不是同字母异序词,时间复杂度为 O(N),空间复杂度为 O(1)。
解答
class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
if len(s)!=len(t):
return False
for c in range(ord('a'),ord('z')+1):
if s.count(chr(c)) != t.count(chr(c)):
return False
return True
运行结果
Runtime: 12 ms, faster than 99.98% of Python online submissions for Valid Anagram.
Memory Usage: 12.6 MB, less than 95.98% of Python online submissions for Valid Anagram.
每日格言:逆境是达到真理的一条通路。
请作者吃小布丁 支付宝