From 9c6453edb1fe443a25d7f772ebf3cf0d4f47b2b7 Mon Sep 17 00:00:00 2001 From: Peshal Agarwal Date: Sun, 25 Oct 2020 15:14:41 +0100 Subject: [PATCH 1/6] feat: added counting bits algorithm --- dynamic_programming/counting_bits.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 dynamic_programming/counting_bits.py diff --git a/dynamic_programming/counting_bits.py b/dynamic_programming/counting_bits.py new file mode 100644 index 000000000000..e9544e374d5d --- /dev/null +++ b/dynamic_programming/counting_bits.py @@ -0,0 +1,21 @@ +def countBits(num: int): + ''' + >> + ''' + ones = [0, 1] + anchor = 2 + + if num < 2: + return ones[:num+1] + + while(True): + j = 0 + while(j Date: Sun, 25 Oct 2020 15:43:12 +0100 Subject: [PATCH 2/6] Add decode remove bits --- dynamic_programming/counting_bits.py | 21 ------------ dynamic_programming/decode_ways.py | 50 ++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 21 deletions(-) delete mode 100644 dynamic_programming/counting_bits.py create mode 100644 dynamic_programming/decode_ways.py diff --git a/dynamic_programming/counting_bits.py b/dynamic_programming/counting_bits.py deleted file mode 100644 index e9544e374d5d..000000000000 --- a/dynamic_programming/counting_bits.py +++ /dev/null @@ -1,21 +0,0 @@ -def countBits(num: int): - ''' - >> - ''' - ones = [0, 1] - anchor = 2 - - if num < 2: - return ones[:num+1] - - while(True): - j = 0 - while(j 1 +'B' -> 2 +... +'Z' -> 26 +Given a non-empty string containing only digits, +determine the total number of ways to decode it. +''' + +def num_decodings(s: str): + ''' + >> num_decodings("12") + 2 + >> num_decodings("226") + 3 + ''' + if not s or int(s[0]) == 0: + return 0 + + last = 1 + second_last = 1 + + for i in range(1, len(s)): + + # 0 is a special digit since it does not + # correspond to any alphabet but can be + # meaningful if preceeded by 1 or 2 + + if s[i] == "0": + if s[i-1] in {"1", "2"}: + curr = second_last + else: + return 0 + + elif 11 <= int(s[i-1:i+1]) <= 26: + curr = second_last + last + else: + curr = last + + last, second_last = curr, last + + return last + + +if __name__ == "__main__": + import doctest + + doctest.testmod() \ No newline at end of file From 66f11072205b376bc8ed670cfb0147a10d014a90 Mon Sep 17 00:00:00 2001 From: Peshal Agarwal Date: Sun, 25 Oct 2020 16:03:17 +0100 Subject: [PATCH 3/6] fix indentation issues --- dynamic_programming/decode_ways.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/dynamic_programming/decode_ways.py b/dynamic_programming/decode_ways.py index 0b2979e643e6..4e29f24bffb5 100644 --- a/dynamic_programming/decode_ways.py +++ b/dynamic_programming/decode_ways.py @@ -10,24 +10,23 @@ ''' def num_decodings(s: str): - ''' + """ >> num_decodings("12") 2 >> num_decodings("226") 3 - ''' + """ if not s or int(s[0]) == 0: return 0 last = 1 second_last = 1 - + for i in range(1, len(s)): - # 0 is a special digit since it does not # correspond to any alphabet but can be # meaningful if preceeded by 1 or 2 - + if s[i] == "0": if s[i-1] in {"1", "2"}: curr = second_last @@ -47,4 +46,4 @@ def num_decodings(s: str): if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + doctest.testmod() From 3d1d616b3a91f7776f6ec84fff95138da2bfeb6d Mon Sep 17 00:00:00 2001 From: Peshal Agarwal Date: Sun, 25 Oct 2020 16:10:48 +0100 Subject: [PATCH 4/6] indent to spaces --- dynamic_programming/decode_ways.py | 1 - 1 file changed, 1 deletion(-) diff --git a/dynamic_programming/decode_ways.py b/dynamic_programming/decode_ways.py index 4e29f24bffb5..f52a84f52b88 100644 --- a/dynamic_programming/decode_ways.py +++ b/dynamic_programming/decode_ways.py @@ -32,7 +32,6 @@ def num_decodings(s: str): curr = second_last else: return 0 - elif 11 <= int(s[i-1:i+1]) <= 26: curr = second_last + last else: From 00f7b19f901227d2dbbb80271a7a8c8e250d0143 Mon Sep 17 00:00:00 2001 From: Peshal Agarwal <32962474+agpeshal@users.noreply.github.com> Date: Sun, 25 Oct 2020 16:44:49 +0100 Subject: [PATCH 5/6] Update decode_ways.py --- dynamic_programming/decode_ways.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/dynamic_programming/decode_ways.py b/dynamic_programming/decode_ways.py index f52a84f52b88..fcd80eb6ade0 100644 --- a/dynamic_programming/decode_ways.py +++ b/dynamic_programming/decode_ways.py @@ -1,4 +1,4 @@ -''' +""" A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 @@ -7,7 +7,7 @@ 'Z' -> 26 Given a non-empty string containing only digits, determine the total number of ways to decode it. -''' +""" def num_decodings(s: str): """ @@ -38,7 +38,6 @@ def num_decodings(s: str): curr = last last, second_last = curr, last - return last From 319710bc22b4536f943d81f87af0c0c5fb99939b Mon Sep 17 00:00:00 2001 From: Peshal Agarwal Date: Sun, 25 Oct 2020 17:58:52 +0100 Subject: [PATCH 6/6] Add num of ways to split a string --- strings/num_splits.py | 44 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 strings/num_splits.py diff --git a/strings/num_splits.py b/strings/num_splits.py new file mode 100644 index 000000000000..15ae482018ed --- /dev/null +++ b/strings/num_splits.py @@ -0,0 +1,44 @@ +""" +You are given a string s, a split is called good if you can split s into +2 non-empty strings p and q where its concatenation is equal to s and +the number of distinct letters in p and q are the same. + +Return the number of good splits you can make in s. +""" +from collections import Counter, defaultdict + +def num_splits(self, s: str): + """ + >> num_splits("aacaba") + 2 + >> num_splits("abcd") + 1 + >> num_splits("aaaaa") + 4 + """ + ans = 0 + rightSplit = Counter(s) + leftSplit = defaultdict(int) + + leftCount = 0 + rightCount = len(rightSplit) + + for ch in s: + rightSplit[ch] -= 1 + leftSplit[ch] += 1 + + if rightSplit[ch] == 0: + rightCount -= 1 + + leftCount = len(leftSplit) + + if leftCount == rightCount: + ans += 1 + + return ans + + +if __name__ == "__main__": + import doctest + + doctest.testmod()