Re: [LeetCode] 刷到面試 Grind169 C++
242. Valid Anagram easy題
哈希表
第一次遇到這個
但還滿好懂得
class Solution {
public:
bool isAnagram(string s, string t) {
int hash[26] = {0};
if(s.length()!=t.length()) return false;
for(int i=0;i<s.length();i++){
hash[s[i]-'a']++;
hash[t[i]-'a']--;
}
for(int j=0;j<26;j++){
if(hash[j]!=0) return false;
}
return true;
}
};
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.113.136.220 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1678870847.A.2E6.html
討論串 (同標題文章)
完整討論串 (本文為第 7 之 14 篇):