From 81033d7da1c4f9488663013c84931e6455af4d21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Le=20Lay?= Date: Thu, 9 Jun 2022 08:32:10 -0400 Subject: [PATCH] Add clarifying comment --- 242-Valid-Anagrams.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/242-Valid-Anagrams.py b/242-Valid-Anagrams.py index ffb09d661..15cc18147 100644 --- a/242-Valid-Anagrams.py +++ b/242-Valid-Anagrams.py @@ -9,6 +9,13 @@ def isAnagram(self, s: str, t: str) -> bool: countS[s[i]] = 1 + countS.get(s[i], 0) countT[t[i]] = 1 + countT.get(t[i], 0) for c in countS: + # Note how we do not need to compare the keys of both hashmaps: + # because we know s and t have same length, any difference in + # the letters used will be reflected by a difference in counts. + # ex: "aabb" vs "aabc" -> introducing letter c necessarily impacts + # the count for letter b. + # In other words: checking for a diff in counts is sufficient to detect + # an invalid anagram. if countS[c] != countT.get(c, 0): return False return True