What would be the time complexity (General/Worst case) of the following lines of code?
s1 = "any-string-of-large-size"
s2 = "anyother-string-of-larger-size"
if(any(x in s1 for x in s2)):
return "YES"
return "NO"
This code is for checking if there is any letter common to s1 and s2. I would also like to have any other approach to achieve this which may be more efficient.
I find it difficult to calculate time complexities when using such library functions. Can someone also please explain how to calculate it
setand then testany(x in set_of_s1 for ...)or convert both tosetand get the set intersection. (Actually, in this very specific code, this will be slower, as thisanyreturn positive for the very first combination of characters, but could be O(n²) in a less-than-optimal case.)return "YES" if set(s1) & set(s2) else "NO". What would be the time complexity in this case ?anywithin a_setmight be faster (still O(n) though), as this will only have to create one set and then stop early as soon as the first match is found.&O(n) or O(1) ?