Given a string S consisting of lowercase English alphabets, the task is to find the number of unique characters present in the string.
Examples:
Input: S = “geeksforgeeks”
Output: 7
Explanation: The given string “geeksforgeeks” contains 6 unique characters {‘g’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’, ‘r’}.Input: S = “madam”
Output: 1
Approach: The idea to solve the given problem is to initialize a Set() in python to store all distinct characters of the given string and then, print the size of the set as the required answer.
Below is the implementation of the above approach:
Python3
# Python program for the above approach # Function to count the number of distinct # characters present in the string str def countDis(str): # Stores all distinct characters s = set(str) # Return the size of the set return len(s) # Driver Code if __name__ == "__main__": # Given string S S = "geeksforgeeks" print(countDis(S)) |
7
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

