1.2 KiB
1.2 KiB
DONE 0242. Valid Anagram easy
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
Example 1:
Input: s = "anagram", t = "nagaram"
Output: true
Example 2:
Input: s = "rat", t = "car"
Output: false
Constraints:
1 <= s.length, t.length <= 5 * 10^{4}sandtconsist of lowercase English letters.
Follow up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
DONE Approach
Write your approach here.
DONE Python
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
DONE C++
#include <map>
#include <string>
class Solution {
public:
bool isAnagram(std::string s, std::string t) {
std::map<char, int> ctr;
for (char c: s) {
ctr[c] += c;
}
for (char c: t) {
if (ctr[c] == 0) {
return false;
}
ctr[c] -= 1;
if (ctr[c] == 0) {
ctr.erase(c);
}
}
return ctr.size() == 0;
}
};