{"id":13993,"date":"2021-10-01T10:00:58","date_gmt":"2021-10-01T04:30:58","guid":{"rendered":"https:\/\/python-programs.com\/?p=13993"},"modified":"2021-11-22T18:33:29","modified_gmt":"2021-11-22T13:03:29","slug":"python-program-to-check-whether-string-contains-unique-characters","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-whether-string-contains-unique-characters\/","title":{"rendered":"Python Program to Check whether String Contains Unique Characters"},"content":{"rendered":"
Given a string, the task is to check whether the given string contains all the unique characters in Python.<\/p>\n
Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to check whether the given string contains unique characters in python.<\/p>\n Explore complete java concepts from the Java programming examples<\/a> and get ready to become a good programmer and crack the java software developer interview with ease.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation: Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n 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 …<\/p>\nGiven string =btech<\/pre>\n
The given string [ btech ] contains unique characters<\/pre>\n
Given string =bteceeh<\/pre>\n
The given string [ bteceeh ] contains duplicate characters<\/pre>\n
Program to Check whether String Contains Unique Characters in Python<\/h2>\n
\n
Method #1: Using Counter() (Hashing , Static Input)<\/h3>\n
\n
# Import the Counter() function from collections using the import keyword.\r\nfrom collections import Counter\r\n# Give the string as static input and store it in a variable.\r\ngivenstrng = 'btech'\r\n# Calculate the frequency of all the given string elements\r\n# using the Counter() function which returns the element\r\n# and its frequency as key-value pair and\r\n# store this dictionary in a variable(say strngfreqelements)\r\nstrngfreqelements = Counter(givenstrng)\r\n# Calculate the length of this frequency dictionary\r\n# using the len() function and store it in a variable.\r\nlengthfreq = len(strngfreqelements)\r\n# Calculate the length of the given string using\r\n# the len() function and store it in another variable.\r\nlengthstrng = len(givenstrng)\r\n# Check if both lengths are equal or not using the If conditional statement.\r\n# If both lengths are equal then the given string contains all the unique characters.\r\nif(lengthfreq == lengthstrng):\r\n print('The given string [', givenstrng, '] contains unique characters')\r\n# Else the given string contains duplicate characters.\r\nelse:\r\n print('The given string [', givenstrng, '] contains duplicate characters')\r\n<\/pre>\n
The given string [ btech ] contains unique characters<\/pre>\n
Method #2: Using Counter() (Hashing , User Input)<\/h3>\n
\n
\n<\/strong><\/p>\n# Import the Counter() function from collections using the import keyword.\r\nfrom collections import Counter\r\n# Give the string as user input using the input() function and store it in a variable.\r\ngivenstrng = input('Enter some random string = ')\r\n# Calculate the frequency of all the given string elements\r\n# using the Counter() function which returns the element\r\n# and its frequency as key-value pair and\r\n# store this dictionary in a variable(say strngfreqelements)\r\nstrngfreqelements = Counter(givenstrng)\r\n# Calculate the length of this frequency dictionary\r\n# using the len() function and store it in a variable.\r\nlengthfreq = len(strngfreqelements)\r\n# Calculate the length of the given string using\r\n# the len() function and store it in another variable.\r\nlengthstrng = len(givenstrng)\r\n# Check if both lengths are equal or not using the If conditional statement.\r\n# If both lengths are equal then the given string contains all the unique characters.\r\nif(lengthfreq == lengthstrng):\r\n print('The given string [', givenstrng, '] contains unique characters')\r\n# Else the given string contains duplicate characters.\r\nelse:\r\n print('The given string [', givenstrng, '] contains duplicate characters')\r\n<\/pre>\n
Enter some random string = oneplus\r\nThe given string [ oneplus ] contains unique characters<\/pre>\n
Method #3: Using set() method (Static Input)<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\ngivenstrng = 'bteceeh'\r\n# Convert this string to set using set() function and store it in a variable.\r\nstrngset = set(givenstrng)\r\n# Calculate the length of this set using the len() function and store it in a variable.\r\nlengthfreq = len(strngset)\r\n# Calculate the length of the given string using\r\n# the len() function and store it in another variable.\r\nlengthstrng = len(givenstrng)\r\n# Check if both lengths are equal or not using the If conditional statement.\r\n# If both lengths are equal then the given string contains all the unique characters.\r\nif(lengthfreq == lengthstrng):\r\n print('The given string [', givenstrng, '] contains unique characters')\r\n# Else the given string contains duplicate characters.\r\nelse:\r\n print('The given string [', givenstrng, '] contains duplicate characters')\r\n<\/pre>\n
The given string [ bteceeh ] contains duplicate characters<\/pre>\n
Method #4: Using set() method (User Input)<\/h3>\n
\n
# Give the string as user input using the input() function and store it in a variable.\r\ngivenstrng = input('Enter some random string = ')\r\n# Convert this string to set using set() function and store it in a variable.\r\nstrngset = set(givenstrng)\r\n# Calculate the length of this set using the len() function and store it in a variable.\r\nlengthfreq = len(strngset)\r\n# Calculate the length of the given string using\r\n# the len() function and store it in another variable.\r\nlengthstrng = len(givenstrng)\r\n# Check if both lengths are equal or not using the If conditional statement.\r\n# If both lengths are equal then the given string contains all the unique characters.\r\nif(lengthfreq == lengthstrng):\r\n print('The given string [', givenstrng, '] contains unique characters')\r\n# Else the given string contains duplicate characters.\r\nelse:\r\n print('The given string [', givenstrng, '] contains duplicate characters')\r\n<\/pre>\n
Enter some random string = xiaomi\r\nThe given string [ xiaomi ] contains duplicate characters<\/pre>\n
\n