From 8ef882a4f37a1b9d1f4ee89e5b8fab196ff52967 Mon Sep 17 00:00:00 2001 From: bgz30 <44124521+BGZ30@users.noreply.github.com> Date: Tue, 18 Aug 2020 18:40:49 +0200 Subject: [PATCH] Add unique chars check --- data_structures/strings/unique_chars.py | 26 +++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 data_structures/strings/unique_chars.py diff --git a/data_structures/strings/unique_chars.py b/data_structures/strings/unique_chars.py new file mode 100644 index 00000000..8c74a8be --- /dev/null +++ b/data_structures/strings/unique_chars.py @@ -0,0 +1,26 @@ +""" +Check if a string has unique chars + + How it works: + - The total number of characters that a string can be built of is 128 chars, + so if a string is longer than 128 chars, then it must have repeated chars. + - if it's 128 or less, then check using two nested for loops. +""" +def unique_chars(s): + lenS = len(s) + if lenS > 128: + return False + for i in range(lenS-1): + for j in range(i+1,lenS): + if s[j]==s[i]: + return False + return True + + +s1 = "goToSchool" +s2 = "getAjob" +s3 = "A" + +print("%s is: " %s1, unique_chars(s1), '\n') +print("%s is: " %s2, unique_chars(s2), '\n') +print("%s is: " %s3, unique_chars(s3), '\n') \ No newline at end of file