2

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

6
  • 1
    To make this faster, you can convert one of the strings to set and then test any(x in set_of_s1 for ...) or convert both to set and get the set intersection. (Actually, in this very specific code, this will be slower, as this any return positive for the very first combination of characters, but could be O(n²) in a less-than-optimal case.) Commented Sep 21, 2020 at 15:16
  • Ohh thanks..thinking in your lines, I think it can be made more faster by using this: return "YES" if set(s1) & set(s2) else "NO". What would be the time complexity in this case ? Commented Sep 21, 2020 at 15:19
  • 1
    Exactly. Time complexity would then be O(n) for creating the two sets and the intersection. Using any with in a_set might 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. Commented Sep 21, 2020 at 15:19
  • So is the time complexity for intersection & O(n) or O(1) ? Commented Sep 21, 2020 at 15:21
  • Time complexity for intersection is O(n), since you have to make a O(1) lookup for all the n elements in the set. Commented Sep 21, 2020 at 15:22

2 Answers 2

3

The best and worst case are O(1) and O(|s1|*|s2|), respectively, where |s1| and |s2| denote the length of the two strings.

Indeed, your code could be rewritten as

for c2 in s2:
   for c1 in s1:
      if c1==c2:
          return "YES"
return "NO"

If you just want to check if the two string share a common char you could write it as

if set(s1) & set(s2):
   return "YES"
return "NO"

This would have the same worst case time complexity O(|s1|*|s2|), but the average case would be O(min(|s1|,|s2|).

Sign up to request clarification or add additional context in comments.

4 Comments

It's the worst case considering collisions, unluckily to happen, but still wiki.python.org/moin/TimeComplexity
Okay, makes sense. Of course, the worst case in the double-loop-scenario is much more likely.
@abc Could you please explain what you mean by collisions?
@Dev5 I think this thread cover your question stackoverflow.com/questions/3949310/how-is-set-implemented
0

The time complexity is O(N) in general for (in) keyword. So x in s2 is straight forward O(N). Which concludes overall complexity to be O(N^2).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.