Leetcode - Find and Replace Pattern (Python)

preview_player
Показать описание
May 2021 Leetcode Challenge
Leetcode - Find and Replace Pattern #890
Difficulty: Medium
Рекомендации по теме
Комментарии
Автор

My approach was exactly what you did initially, but instead of creating a list, I decided to make an integer. for example, let's say our pattern was "aqq", I would subtract 122-(pattern of each word) if it's 0 then they match. I didn't create create "122" as a string then convert it to int. Instead, I just did :

def patternFinder(word):
Map=defaultdict(int)
newPattern=0
for i in range(len(word)):
if word[i] not in Map:
Map[word[i]]=i

return newPattern


ans=[]
for word in words:
w=patternFinder(word)
if w-pattern==0:
ans.append(word)
return ans

This way we are not using another hashmap. I think subtraction and multiplication are constant time. Please correct me if I'm wrong.

CEOofTheHood
Автор

I keep forgetting you can compare lists like that in immediately thought of two maps for this one

janmichaelaustria
Автор

Sir can you mention your socials in description?...only if you can help us with some medium difficulty python algo problems...much appreciated

magniboy
join shbcf.ru