diff --git a/longest-common-subsequence/ppxyn1.py b/longest-common-subsequence/ppxyn1.py new file mode 100644 index 0000000000..73fda73ec3 --- /dev/null +++ b/longest-common-subsequence/ppxyn1.py @@ -0,0 +1,18 @@ +# idea : recursive +# Time complexity : O(len(text1)*len(text2)) + +class Solution: + def longestCommonSubsequence(self, text1: str, text2: str) -> int: + @cache + def dfs(i, j): + if i == len(text1) or j == len(text2): + return 0 + if text1[i] == text2[j]: + match_len = 1 + dfs(i+1, j+1) + return match_len + else: + return max(dfs(i + 1, j), dfs(i, j + 1)) + return dfs(0, 0) + + + diff --git a/palindromic-substrings/ppxyn1.py b/palindromic-substrings/ppxyn1.py new file mode 100644 index 0000000000..0e25379a21 --- /dev/null +++ b/palindromic-substrings/ppxyn1.py @@ -0,0 +1,28 @@ +# idea : palindrome +# Time Complexity: O(n^2) + +class Solution: + def countSubstrings(self, s: str) -> int: + result = 0 + + for i in range(len(s)): + # odd + left = i + right = i + while left >= 0 and right < len(s) and s[left] == s[right]: + result += 1 + left -= 1 + right += 1 + + # even + left = i + right = i + 1 + while left >= 0 and right < len(s) and s[left] == s[right]: + result += 1 + left -= 1 + right += 1 + + return result + + + diff --git a/reverse-bits/ppxyn1.py b/reverse-bits/ppxyn1.py new file mode 100644 index 0000000000..dc1915028d --- /dev/null +++ b/reverse-bits/ppxyn1.py @@ -0,0 +1,10 @@ +# idea: - +# Time Complexity : O(1) since just fixed 32bit + +class Solution: + def reverseBits(self, n: int) -> int: + bits = bin(n)[2:] + bits = bits.zfill(32) # 32bits + reversed_bits = bits[::-1] + return int(reversed_bits, 2) # binary to int +