This commit is contained in:
2026-06-01 18:12:40 +08:00
parent c9fe2fab76
commit 8c9f58fdfb
5 changed files with 168 additions and 10 deletions
@@ -1,5 +1,5 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0242. Valid Anagram :easy:
* DONE 0242. Valid Anagram :easy:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0242. Valid Anagram][0242. Valid Anagram]]
:END:
@@ -26,21 +26,36 @@ Given two strings ~s~ and ~t~, return ~true~ if ~t~ is an anagram of ~s~, and ~f
*Follow up:* What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
** TODO Approach
** DONE Approach
Write your approach here.
** TODO Python
** DONE Python
#+begin_src python
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
#+end_src
** TODO C++
** DONE C++
#+begin_src cpp
#include <map>
#include <string>
class Solution {
public:
bool isAnagram(string s, string t) {
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;
}
};
#+end_src