Solution Review: Is Unique
This lesson contains the solution review to determine whether a string contains all unique characters or not.
We'll cover the following...
In this lesson, we will consider how to determine if a string has all unique characters. One approach to this problem may be to use an additional data structure, like a hash table. We will also consider how one may solve this problem without the use of such a data structure.
So, we’ll present a number of solutions to the problem posed in the previous challenge and give a rough time and space complexity analysis of each approach.
Let’s get started!
Solution 1
Now let’s discuss the actual implementation. Solution 1 uses a Python dictionary to solve the problem in linear time complexity, but because of the additional data structure, the space complexity is also linear.
Implementation
Below is the implementation of Solution 1 in Python:
def is_unique_1(input_str):d = dict()for i in input_str:if i in d:return Falseelse:d[i] = 1return True
Explanation
d
is initialized ...