Leetcode - Determine if Two Strings Are Close (Python)

preview_player
Показать описание
January 2021 Leetcode Challenge
Leetcode - Determine if Two Strings Are Close #1657
Рекомендации по теме
Комментарии
Автор

Amazing idea and solution, pretty easy-understood

ShawnLee-rgsz
Автор

How do you come up with such a great ideas?! I appreciate it! Thank you Tim!

muzaffartursunov
Автор

Wanted to share refactored version:
def closeStrings(self, word1: str, word2: str) -> bool:
hist1 = Counter(word1)
hist2 = Counter(word2)
hist3 = Counter(hist1.values())
hist4 = Counter(hist2.values())
return hist3 == hist4 and set(word1) == set(word2)

TimBrownYoutube
Автор

great explanation! thanks a lot for this!

Alpha
Автор

He accidentally added the c1 == c2 in the return at the bottom : [Here is the code code corrected and cleaned up]
def closeStrings(self, word1: str, word2: str) -> bool:
m1 = Counter(word1)
m2 = Counter(word2)
freq1 = Counter(m1.values())
freq2 = Counter(m2.values())
return freq1 == freq2 and set(word1) == set(word2)

blank
Автор

The first thing that comes to your mind is recursion. Every single time lol

waleeddib
Автор

I think you can remove the c1==c2 condition as it should already be included in the second condition. if c1 equals c2 means that both strings share the same set of chars and the same distribution of counts

Thanks a lot for the videos, watched a couple and they were short but very clear and easy to understand

patrikm
Автор

Why did you do a list comprehension [v for v in c1.values()]? Is this not just the same as simply c1.values()?

chickaberga