I started working through the CSES problem set. And I am kind of surprised by this problem - https://cses.fi/problemset/task/1621/ It's pretty straightforward - given n numbers, as a string input (like "2 3 3 2 3") find out the count of unique numbers.
I figured the fastest way to do it would be to use a map. So I split() the input string and used the map() function to comvert each of the string numbers to int.
Here's the code:
n = int(input())
nums = map(int, input().split())
nums = set(nums)
print(len(nums))
But this code returns a TLE for an input of 100,000 numbers.
So, I changed the implementation a little bit (just as a shot in the dark) and did not map the strings into integers. Instead I just created a set() out of strings. Here's the code:
n = int(input())
nums = input().split()
nums = set(nums)
print(len(nums))
And this passes for that input of 100,000 numbers
I was under the assumption that integers are easier to hash than strings. So, it means that the map() function is the culprit. What's the time complexity of the map function?
This accepted answer in the following question suggests that the time complexity for non base 2 number strings is O(N**2) where N is the number of digits in the input string to the int() function. What is the complexity of str() function in Python3?
So, is the time complexity of the map(int, str) function O(N**2) where N is the length of the base 10 string number?