chore(Python): added is unique character algorithm (#428)

pull/472/head
Dheerendra Panwar 2021-09-20 04:42:43 -07:00 committed by GitHub
parent b390df5318
commit 2def163eae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 0 deletions

2
.gitignore vendored
View File

@ -12,3 +12,5 @@ Thumbs.db
*.class
*.idea

View File

@ -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

View File

@ -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'))