3 ms·
In order to hash the string you need to access all of its characters, ergo O(n). I agree with you that this sounds weird, but if you imagine arbitrarily long s
by wickedsickeune 4y ago
In order to hash the string you need to access all of its characters, ergo O(n).
I agree with you that this sounds weird, but if you imagine arbitrarily long strings as inputs, and don't want to have a lot of collisions, you need to take into account all of the input string in the hash function.
In reality they are safely assumed to take O(1), but w/e the interviewer is looking for a specific answer.