chore(Python): added is unique character algorithm (#428)
parent
b390df5318
commit
2def163eae
|
@ -12,3 +12,5 @@ Thumbs.db
|
|||
*.class
|
||||
*.idea
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -54,6 +54,7 @@
|
|||
4. [Remove Duplicates from a String](strings/remove_duplicates_from_a_string.py)
|
||||
5. [First Non Repeating Character](strings/first_non_repeating_character.py)
|
||||
6. [Longest Common Subsequence](strings/longest_common_subsequence.py)
|
||||
7. [Unique Character](strings/unique_character.py)
|
||||
|
||||
## Dynamic Programming
|
||||
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
"""
|
||||
Algorithm Type : Find the all the characters in the given strings are unique
|
||||
Time Complexity: O(n)
|
||||
"""
|
||||
s = 'abcd'
|
||||
|
||||
def is_unique(s: str) -> bool:
|
||||
"""
|
||||
>>> is_unique('ABCDE')
|
||||
True
|
||||
"""
|
||||
"""
|
||||
>>> is_unique('programmer')
|
||||
False
|
||||
"""
|
||||
|
||||
|
||||
arr = [False for _ in range(128)] #creating hashtable with False input
|
||||
for character in s: #iterate throughout the string
|
||||
char_value = ord(character)
|
||||
if arr[char_value]:
|
||||
return False
|
||||
else:
|
||||
arr[char_value] = True
|
||||
return True
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(is_unique(s))
|
||||
print(is_unique('ABCDEDD'))
|
||||
print(is_unique('programmer'))
|
Loading…
Reference in New Issue