From dad857b3c930574a0e1b63ea5954478bd7a2e161 Mon Sep 17 00:00:00 2001 From: icat11 Date: Fri, 15 Dec 2023 22:21:05 -0800 Subject: [PATCH 1/6] a new solution of 0 338 --- python/0338-counting-bits.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/python/0338-counting-bits.py b/python/0338-counting-bits.py index 42f0a4662..ca751db47 100644 --- a/python/0338-counting-bits.py +++ b/python/0338-counting-bits.py @@ -8,3 +8,17 @@ def countBits(self, n: int) -> List[int]: offset = i dp[i] = 1 + dp[i - offset] return dp + +# Another dp solution +class Solution2: + def countBits(self, n: int) -> List[int]: + res = [0] * (n + 1) + for i in range(1, n + 1): + if i % 2 == 1: + res[i] = res[i - 1] + 1 + else: + res[i] = res[i // 2] + return res +# This solution is based on the division of odd and even numbers. +# I think it's easier to understand. +# This is my full solution, covering the details: https://leetcode.com/problems/counting-bits/solutions/4411054/odd-and-even-numbers-a-easier-to-understanding-way-of-dp/ From 0bcd8f956e247e772f5985ef18314d933201201b Mon Sep 17 00:00:00 2001 From: icat11 Date: Tue, 16 Jan 2024 11:41:06 -0800 Subject: [PATCH 2/6] python 1397 added --- python/1397-find-all-good-strings.py | 48 ++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 python/1397-find-all-good-strings.py diff --git a/python/1397-find-all-good-strings.py b/python/1397-find-all-good-strings.py new file mode 100644 index 000000000..cbcc75b2c --- /dev/null +++ b/python/1397-find-all-good-strings.py @@ -0,0 +1,48 @@ +class Solution: + def findGoodStrings(self, n: int, s1: str, s2: str, evil: str) -> int: + a = ord('a') + z = ord('z') + + arr_e = list(map(ord, evil)) + len_e = len(evil) + next = [0] * len_e + + for i in range(1, len_e): + j = next[i - 1] + while j > 0 and evil[i] != evil[j]: + j = next[j - 1] + if evil[i] == evil[j]: + next[i] = j + 1 + + def good(s): + arr = list(map(ord, s)) + len_a = len(arr) + + @cache + def f(i, skip, reach, e): + if e == len_e: + return 0 + if i == len_a: + return 0 if skip else 1 + + limit = arr[i] if reach else z + ans = 0 + + if skip: + ans += f(i + 1, True, False, 0) + + for c in range(a, limit + 1): + ee = e + while ee > 0 and arr_e[ee] != c: + ee = next[ee - 1] + + if arr_e[ee] == c: + ee += 1 + + ans += f(i + 1, False, reach and c == limit, ee) + + return ans % int(1e9 + 7) + + return f(0, True, True, 0) + + return (good(s2) - good(s1) + int(evil not in s1)) % int(1e9 + 7) From 818d4f1d17f19f189dbf5a0176b22d408511f9fb Mon Sep 17 00:00:00 2001 From: "giggling.ginger" Date: Tue, 16 Jan 2024 11:45:16 -0800 Subject: [PATCH 3/6] python 1397 added --- python/1397-find-all-good-strings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/1397-find-all-good-strings.py b/python/1397-find-all-good-strings.py index cbcc75b2c..8e628b96f 100644 --- a/python/1397-find-all-good-strings.py +++ b/python/1397-find-all-good-strings.py @@ -34,7 +34,7 @@ def f(i, skip, reach, e): for c in range(a, limit + 1): ee = e while ee > 0 and arr_e[ee] != c: - ee = next[ee - 1] + ee = next[ee - 1] if arr_e[ee] == c: ee += 1 From 5c28166bf012158797ac2893e82105d879097d1d Mon Sep 17 00:00:00 2001 From: "giggling.ginger" Date: Tue, 16 Jan 2024 11:49:25 -0800 Subject: [PATCH 4/6] python 1397 added --- python/1397-find-all-good-strings.py | 48 ---------------------------- 1 file changed, 48 deletions(-) delete mode 100644 python/1397-find-all-good-strings.py diff --git a/python/1397-find-all-good-strings.py b/python/1397-find-all-good-strings.py deleted file mode 100644 index 8e628b96f..000000000 --- a/python/1397-find-all-good-strings.py +++ /dev/null @@ -1,48 +0,0 @@ -class Solution: - def findGoodStrings(self, n: int, s1: str, s2: str, evil: str) -> int: - a = ord('a') - z = ord('z') - - arr_e = list(map(ord, evil)) - len_e = len(evil) - next = [0] * len_e - - for i in range(1, len_e): - j = next[i - 1] - while j > 0 and evil[i] != evil[j]: - j = next[j - 1] - if evil[i] == evil[j]: - next[i] = j + 1 - - def good(s): - arr = list(map(ord, s)) - len_a = len(arr) - - @cache - def f(i, skip, reach, e): - if e == len_e: - return 0 - if i == len_a: - return 0 if skip else 1 - - limit = arr[i] if reach else z - ans = 0 - - if skip: - ans += f(i + 1, True, False, 0) - - for c in range(a, limit + 1): - ee = e - while ee > 0 and arr_e[ee] != c: - ee = next[ee - 1] - - if arr_e[ee] == c: - ee += 1 - - ans += f(i + 1, False, reach and c == limit, ee) - - return ans % int(1e9 + 7) - - return f(0, True, True, 0) - - return (good(s2) - good(s1) + int(evil not in s1)) % int(1e9 + 7) From 2e9798dfecbac75ce41cd3db39573edc54921839 Mon Sep 17 00:00:00 2001 From: "giggling.ginger" Date: Tue, 16 Jan 2024 11:52:07 -0800 Subject: [PATCH 5/6] python 1397 added --- python/1397-find-all-good-strings-1.py | 48 ++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 python/1397-find-all-good-strings-1.py diff --git a/python/1397-find-all-good-strings-1.py b/python/1397-find-all-good-strings-1.py new file mode 100644 index 000000000..8e628b96f --- /dev/null +++ b/python/1397-find-all-good-strings-1.py @@ -0,0 +1,48 @@ +class Solution: + def findGoodStrings(self, n: int, s1: str, s2: str, evil: str) -> int: + a = ord('a') + z = ord('z') + + arr_e = list(map(ord, evil)) + len_e = len(evil) + next = [0] * len_e + + for i in range(1, len_e): + j = next[i - 1] + while j > 0 and evil[i] != evil[j]: + j = next[j - 1] + if evil[i] == evil[j]: + next[i] = j + 1 + + def good(s): + arr = list(map(ord, s)) + len_a = len(arr) + + @cache + def f(i, skip, reach, e): + if e == len_e: + return 0 + if i == len_a: + return 0 if skip else 1 + + limit = arr[i] if reach else z + ans = 0 + + if skip: + ans += f(i + 1, True, False, 0) + + for c in range(a, limit + 1): + ee = e + while ee > 0 and arr_e[ee] != c: + ee = next[ee - 1] + + if arr_e[ee] == c: + ee += 1 + + ans += f(i + 1, False, reach and c == limit, ee) + + return ans % int(1e9 + 7) + + return f(0, True, True, 0) + + return (good(s2) - good(s1) + int(evil not in s1)) % int(1e9 + 7) From c2f608558d157da09412e9b8ee9e5e4b87b457ff Mon Sep 17 00:00:00 2001 From: Yaseen Khan Date: Sun, 10 Nov 2024 07:29:06 -0700 Subject: [PATCH 6/6] Fixed code file name --- ...find-all-good-strings-1.py => 1397-find-all-good-strings-i.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename python/{1397-find-all-good-strings-1.py => 1397-find-all-good-strings-i.py} (100%) diff --git a/python/1397-find-all-good-strings-1.py b/python/1397-find-all-good-strings-i.py similarity index 100% rename from python/1397-find-all-good-strings-1.py rename to python/1397-find-all-good-strings-i.py