27 lines
1.2 KiB
Python
27 lines
1.2 KiB
Python
'''
|
|
Bill has found a set. The set consists of small English letters. Bill carefully wrote out all the letters from the set in one line, separated by a comma. He also added an opening curved bracket at the beginning of the line and a closing curved bracket at the end of the line. Unfortunately, from time to time Bill would forget writing some letter and write it again. Help Bill to count the total number of distinct letters in his set.
|
|
|
|
Given a string of set format set_str, return an integer of total number of distinct letters from set.
|
|
|
|
Example 1
|
|
Input:
|
|
{b, a, b, a}
|
|
Output:
|
|
2
|
|
'''
|
|
|
|
class Solution(object):
|
|
def solve(self, set_str:str) -> int:
|
|
print(f"set_str: {set_str}")
|
|
# Remove the curly braces and split the string by comma
|
|
elements = set_str[1:-1].split(", ")
|
|
# Use a set to find distinct elements
|
|
distinct_elements = set(elements)
|
|
return len(distinct_elements)
|
|
|
|
s = Solution()
|
|
print(s.solve("{b, a, b, a}")) # Output: 2
|
|
print(s.solve("{c, d, e, c, d}")) # Output: 3
|
|
print(s.solve("{x, y, z}")) # Output: 3
|
|
print (s.solve("{a, b, c, d, e, f, g, h, i, j}")) # Output: 10
|
|
print(s.solve("{a, a, a, a}")) # Output: 1 |