Open In App

Python program to check if a string contains all unique characters

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

To implement an algorithm to determine if a string contains all unique characters. 

Examples: 

Input : s = “abcd” 
Output: True 
“abcd” doesn’t contain any duplicates. Hence the output is True.

Input : s = “abbd” 
Output: False 
“abbd” contains duplicates. Hence the output is False.

One solution is to create an array of boolean values, where the flag at the index i indicates whether character i in the alphabet is contained in the string. The second time you see this character you can immediately return false. 

You can also return false if the string length exceeds the number of unique characters in the alphabet.  

Implementation:

Python




def isUniqueChars(st):
 
    # String length cannot be more than
    # 256.
    if len(st) > 256:
        return False
 
    # Initialize occurrences of all characters
    char_set = [False] * 128
 
    # For every character, check if it exists
    # in char_set
    for i in range(0, len(st)):
 
        # Find ASCII value and check if it
        # exists in set.
        val = ord(st[i])
        if char_set[val]:
            return False
 
        char_set[val] = True
 
    return True
 
# driver code
st = "abcd"
print(isUniqueChars(st))


Output

True

Complexity Analysis:

  • Time Complexity: O(N), where N is the length of the string.
  • Auxiliary Space: O(1), no extra space required so it is a constant.

Method #2:Using Built-in Python Functions:

  • Count the frequencies of characters using Counter() function
  • If the keys in the frequency dictionary(which gives the count of distinct characters) is equal to the length of string then print True else False

Implementation:

Python3




from collections import Counter
 
 
def isUniqueChars(string):
 
    # Counting frequency
    freq = Counter(string)
 
    if(len(freq) == len(string)):
        return True
    else:
        return False
 
# driver code
st = "abcd"
print(isUniqueChars(st))
# This code is contributed by vikkycirus


Output

True

Complexity Analysis:

  • Time Complexity: O(N), where N is the length of the string.
  • Auxiliary Space: O(26), in total there are 26 letters in alphabet and no extra space required so it is a constant.

Method #3 : Using list() and set() methods

Python3




st = "abcbd"
x=list(set(st))
y=list(st)
x.sort()
y.sort()
if(x==y):
    print(True)
else:
    print(False)


Output

False

Time Complexity: O(logn)
Auxiliary Space: O(1)

Method #4: Using for loop and membership operators

Python3




st = "abcbd"
a=""
for i in st:
    if i not in a:
        a+=i
if(a==st):
    print(True)
else:
    print(False)


Output

False

Time Complexity: O(N)
Auxiliary Space: O(N)

Method #5 : Using Operator.countOf() function

Python3




import operator as op
 
 
def isUniqueChars(string):
 
    for i in string:
        if op.countOf(string, i) > 1:
            return False
    return True
 
 
# driver code
st = "abcd"
print(isUniqueChars(st))


Output

True

Complexity Analysis:

Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1), no extra space required so it is a constant.

Method #6 : Using count() function

Python3




def isUniqueChars(string):
 
    for i in string:
        if string.count(i) > 1:
            return False
    return True
 
 
# driver code
st = "abcd"
print(isUniqueChars(st))


Output

True

Time Complexity: O(N)
Auxiliary Space: O(1)



Last Updated : 28 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads