From 2def163eae9be1e2370b8cdd36a011c33670ffba Mon Sep 17 00:00:00 2001 From: Dheerendra Panwar Date: Mon, 20 Sep 2021 04:42:43 -0700 Subject: [PATCH] chore(Python): added is unique character algorithm (#428) --- .gitignore | 2 ++ algorithms/Python/README.md | 1 + algorithms/Python/strings/unique_character.py | 30 +++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 algorithms/Python/strings/unique_character.py diff --git a/.gitignore b/.gitignore index 21b5bc82..d2c52858 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,5 @@ Thumbs.db *.class *.idea + + diff --git a/algorithms/Python/README.md b/algorithms/Python/README.md index 4e5a699e..dc36f3f4 100644 --- a/algorithms/Python/README.md +++ b/algorithms/Python/README.md @@ -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 diff --git a/algorithms/Python/strings/unique_character.py b/algorithms/Python/strings/unique_character.py new file mode 100644 index 00000000..40b77d72 --- /dev/null +++ b/algorithms/Python/strings/unique_character.py @@ -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')) \ No newline at end of file