Given a string, the task is to check whether the given string contains all the unique characters in Python.
Examples:
Example1:
Input:
Given string =btech
Output:
The given string [ btech ] contains unique characters
Example2:
Input:
Given string =bteceeh
Output:
The given string [ bteceeh ] contains duplicate characters
Program to Check whether String Contains Unique Characters in Python
Below are the ways to check whether the given string contains unique characters in python.
- Using Counter() (Hashing , Static Input)
- Using Counter() (Hashing , User Input)
- Using set() method (Static Input)
- Using set() method (User Input)
Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.
Method #1: Using Counter() (Hashing , Static Input)
Approach:
- Import the Counter() function from collections using the import keyword.
- Give the string as static input and store it in a variable.
- Calculate the frequency of all the given string elements using the Counter() function which returns the element and its frequency as key-value pair and store this dictionary in a variable(say strngfreqelements)
- Calculate the length of this frequency dictionary using the len() function and store it in a variable.
- Calculate the length of the given string using the len() function and store it in another variable.
- Check if both lengths are equal or not using the If conditional statement.
- If both lengths are equal then the given string contains all the unique characters.
- Else the given string contains duplicate characters.
- The Exit of the program.
Below is the implementation:
# Import the Counter() function from collections using the import keyword.
from collections import Counter
# Give the string as static input and store it in a variable.
givenstrng = 'btech'
# Calculate the frequency of all the given string elements
# using the Counter() function which returns the element
# and its frequency as key-value pair and
# store this dictionary in a variable(say strngfreqelements)
strngfreqelements = Counter(givenstrng)
# Calculate the length of this frequency dictionary
# using the len() function and store it in a variable.
lengthfreq = len(strngfreqelements)
# Calculate the length of the given string using
# the len() function and store it in another variable.
lengthstrng = len(givenstrng)
# Check if both lengths are equal or not using the If conditional statement.
# If both lengths are equal then the given string contains all the unique characters.
if(lengthfreq == lengthstrng):
print('The given string [', givenstrng, '] contains unique characters')
# Else the given string contains duplicate characters.
else:
print('The given string [', givenstrng, '] contains duplicate characters')
Output:
The given string [ btech ] contains unique characters
Method #2: Using Counter() (Hashing , User Input)
Approach:
- Import the Counter() function from collections using the import keyword.
- Give the string as user input using the input() function and store it in a variable.
- Calculate the frequency of all the given string elements using the Counter() function which returns the element and its frequency as key-value pair and store this dictionary in a variable(say strngfreqelements)
- Calculate the length of this frequency dictionary using the len() function and store it in a variable.
- Calculate the length of the given string using the len() function and store it in another variable.
- Check if both lengths are equal or not using the If conditional statement.
- If both lengths are equal then the given string contains all the unique characters.
- Else the given string contains duplicate characters.
- The Exit of the program.
Below is the implementation:
# Import the Counter() function from collections using the import keyword.
from collections import Counter
# Give the string as user input using the input() function and store it in a variable.
givenstrng = input('Enter some random string = ')
# Calculate the frequency of all the given string elements
# using the Counter() function which returns the element
# and its frequency as key-value pair and
# store this dictionary in a variable(say strngfreqelements)
strngfreqelements = Counter(givenstrng)
# Calculate the length of this frequency dictionary
# using the len() function and store it in a variable.
lengthfreq = len(strngfreqelements)
# Calculate the length of the given string using
# the len() function and store it in another variable.
lengthstrng = len(givenstrng)
# Check if both lengths are equal or not using the If conditional statement.
# If both lengths are equal then the given string contains all the unique characters.
if(lengthfreq == lengthstrng):
print('The given string [', givenstrng, '] contains unique characters')
# Else the given string contains duplicate characters.
else:
print('The given string [', givenstrng, '] contains duplicate characters')
Output:
Enter some random string = oneplus The given string [ oneplus ] contains unique characters
Method #3: Using set() method (Static Input)
Approach:
- Give the string as static input and store it in a variable.
- Convert this string to set using set() function and store it in a variable.
- Calculate the length of this set using the len() function and store it in a variable.
- Calculate the length of the given string using the len() function and store it in another variable.
- Check if both lengths are equal or not using the If conditional statement.
- If both lengths are equal then the given string contains all the unique characters.
- Else the given string contains duplicate characters.
- The Exit of the program.
Below is the implementation:
# Give the string as static input and store it in a variable.
givenstrng = 'bteceeh'
# Convert this string to set using set() function and store it in a variable.
strngset = set(givenstrng)
# Calculate the length of this set using the len() function and store it in a variable.
lengthfreq = len(strngset)
# Calculate the length of the given string using
# the len() function and store it in another variable.
lengthstrng = len(givenstrng)
# Check if both lengths are equal or not using the If conditional statement.
# If both lengths are equal then the given string contains all the unique characters.
if(lengthfreq == lengthstrng):
print('The given string [', givenstrng, '] contains unique characters')
# Else the given string contains duplicate characters.
else:
print('The given string [', givenstrng, '] contains duplicate characters')
Output:
The given string [ bteceeh ] contains duplicate characters
Method #4: Using set() method (User Input)
Approach:
- Give the string as static input and store it in a variable.
- Give the string as user input using the input() function and store it in a variable.
- Calculate the length of this set using the len() function and store it in a variable.
- Calculate the length of the given string using the len() function and store it in another variable.
- Check if both lengths are equal or not using the If conditional statement.
- If both lengths are equal then the given string contains all the unique characters.
- Else the given string contains duplicate characters.
- The Exit of the program.
Below is the implementation:
# Give the string as user input using the input() function and store it in a variable.
givenstrng = input('Enter some random string = ')
# Convert this string to set using set() function and store it in a variable.
strngset = set(givenstrng)
# Calculate the length of this set using the len() function and store it in a variable.
lengthfreq = len(strngset)
# Calculate the length of the given string using
# the len() function and store it in another variable.
lengthstrng = len(givenstrng)
# Check if both lengths are equal or not using the If conditional statement.
# If both lengths are equal then the given string contains all the unique characters.
if(lengthfreq == lengthstrng):
print('The given string [', givenstrng, '] contains unique characters')
# Else the given string contains duplicate characters.
else:
print('The given string [', givenstrng, '] contains duplicate characters')
Output:
Enter some random string = xiaomi The given string [ xiaomi ] contains duplicate characters
Related Programs:
- python program to check whether a string is a palindrome or not using recursion
- python program to check if a string is palindrome or not
- python program to check if a string is a pangram or not
- python program to check whether the given number is strong number or not
- python program to check whether the given number is perfect number or not
- python program to check whether the given two numbers are amicable numbers or not
- python program to calculate the number of words and the number of characters present in a string
