Dictionary in python with strings as keys | Direct Lookup vs Finding if key exisits

46 Views Asked by At

Well I was doing a competetive programming ques, and in that i want to memoize result when we have two strings s1 and s2

I tried three methods with python dp = defaultdict(int)

First

h = hash(s1+s2)
if(dp[h] != 0 ): return 

Second :

if(dp[(s1,s2)] != 0 ): return 

3rd:

if((s1,s2) in dp ): return 

Only the 3rd case is accepted, the 1st and 2nd one's gave TLE. To my understanding this would mean that hashing strings would be slower that just iterating over keys . But i am not sure can anyone make it clear.

0

There are 0 best solutions below