[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

Interesting in any computer science.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

GKE Autopilot Kubernetes Cluster with Pulumi Infrastructure as Code

Developing as a Software Developer: 7 Methods for Faster Code Mastery

What they publish: This women’s site is primarily, but

Open source container security scanner

Launch EC2 Instance using AWS CLI

Dynamic Reports — A How-to Guide

The SOLID Principles of Object-Oriented Programming and NoSQL

Code Runs the World — But No One Understands It

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
Fatboy Slim

Fatboy Slim

Interesting in any computer science.

More from Medium

[LeetCode]#2089. Find Target Indices After Sorting Array

LeetCode Patterns Adventure 12 — Palindrome Linked List

Remove nth node from end of list Leetcode Python

4-in-1 Priority First Search in Python: BFS, DFS, Prim’s, and Dijkstra’s algorithms