
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to check if a string contains all unique characters
In this article, we will learn about the solution and approach to solve the given problem statement.
Problem statement
Given a sring input, we need to find whether a string contains all unique characters or not.
Approach
We will create an array of boolean values, where the variable flag at the index i indicates that whether character i in the alphabet is contained in the string or not.
The second time we encounter this character we can immediately return false as string characters is no longer unique.
We can also return false if the string length exceeds the value of number of unique characters presnt in the alphabet.
Herw we have fixed the size of string to 256 maximum
Now let’s see the implementation −
Example
def isUniqueChars(st): if len(st) > 256: return False # Initialization char_set = [False] * 128 # in char_set for i in range(0, len(st)): # ASCII value val = ord(st[i]) if char_set[val]: return False char_set[val] = True return True # main st = "tutorialspoint" print(isUniqueChars(st))
Output
False
All the variables are declared in the global frame as shown in the figure given below −
Conclusion
In this article, we learned about the approach to check if a string contains all unique characters
- Related Articles
- Python program to check if a string contains any unique character
- Checking if a string contains all unique characters using JavaScript
- Python Program to check if String contains only Defined Characters using Regex
- C# program to determine if a string has all unique characters
- Check if list contains all unique elements in Python
- How to check if a string only contains certain characters in Python?
- Java Program to check if the String contains only certain characters
- How to check if a unicode string contains only numeric characters in Python?
- How to check if a string contains only decimal characters?
- Check if string contains special characters in Swift
- Python - Check If All the Characters in a String Are Alphanumeric?
- Program to check if a string contains any special character in Python
- Python - Check if frequencies of all characters of a string are different
- Java Program to Check if a string contains a substring
- Golang program to check if a string contains a substring
