[LeetCode]#2068. Check Whether Two Strings are Almost Equivalent

Input: word1 = "aaaa", word2 = "bccb"
Output: false
Explanation: There are 4 'a's in "aaaa" but 0 'a's in "bccb".
The difference is 4, which is more than the allowed 3.
  1. use Counter to word1 and word2
  2. Find their different
  3. Find their union
  4. If value ≤3, return True
class Solution:
def checkAlmostEquivalent(self, word1, word2):
c1, c2 = Counter(word1), Counter(word2)
values = ((c1-c2)|(c2-c1)).values()
return max(values, default=0) < 4

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store