力扣题-11.29
[力扣刷题攻略] Re:从零开始的力扣刷题生活
力扣题1:032. 有效的字母异位词
解题思想:直接遍历即可
class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
if s==t:
return False
if len(s)!=len(t):
return False
char_count = {}
for i in range(len(s)):
if s[i] in char_count:
char_count[s[i]] += 1
else:
char_count[s[i]] = 1
for i in range(len(t)):
if t[i] in char_count:
char_count[t[i]] -=1
if char_count[t[i]]<0:
return False
else:
return False
return True
class Solution {
public:
bool isAnagram(string s, string t) {
if (s == t) {
return false;
}
if (s.length() != t.length()) {
return false;
}
std::unordered_map<char, int> char_count;
for (int i = 0; i < s.length(); ++i) {
if (char_count.find(s[i]) != char_count.end()) {
char_count[s[i]] += 1;
} else {
char_count[s[i]] = 1;
}
}
for (int i = 0; i < t.length(); ++i) {
if (char_count.find(t[i]) != char_count.end()) {
char_count[t[i]] -= 1;
if (char_count[t[i]] < 0) {
return false;
}
} else {
return false;
}
}
return true;
}
};