标签:通过 case input 复杂度 空间 python3 c++ 时间 turn map
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. 意思:
判断t 是否为 s的变型词,即由相同的字母组成,但字母的顺序可能不同
class Solution { public: bool isAnagram(string s, string t) { if(s.size()!=t.size()) { return false; } vector<int> map1(26,0),map2(26,0); for(int i=0;i<s.size();i++) { map1[s[i]-‘a‘]+=1; map2[t[i]-‘a‘]+=1; } return map1==map2; } };
class Solution: def isAnagram(self, s: ‘str‘, t: ‘str‘) -> ‘bool‘: if len(s)!=len(t): return False dict1,dict2={},{} for i in s: dict1[i]=dict1.get(i,0)+1 for i in t: dict2[i]=dict2.get(i,0)+1 return dict1==dict2
标签:通过 case input 复杂度 空间 python3 c++ 时间 turn map
原文地址:https://www.cnblogs.com/zydxx/p/10420607.html