Sobes.tech
Senior

How long does it take to insert N different strings of length K into a set?

sobes.tech AI

Answer from AI

Inserting N different strings of length K into a set in Python on average takes about O(N * K) time, where:

  • N is the number of strings,
  • K is the length of each string.

Explanation:

  • Hashing each string takes approximately O(K), since computing the hash requires processing all characters of the string.
  • Inserting into a set is amortized O(1) per element.

Total: for all N strings — O(N * K).

Example:

s = set()
for i in range(N):
    string = generate_string_of_length_K()
    s.add(string)  # hashing the string + insertion
How long does it take to insert N different strings… - sobes.tech