diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/mangodm-web.py b/construct-binary-tree-from-preorder-and-inorder-traversal/mangodm-web.py new file mode 100644 index 000000000..5973bc77a --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/mangodm-web.py @@ -0,0 +1,32 @@ +from typing import List + + +class Codec: + def encode(self, strs: List[str]) -> str: + """Encodes a list of strings to a single string.""" + encoded = [] + + for s in strs: + encoded.append(s.replace("/", "//") + "/:") + + return "".join(encoded) + + def decode(self, s: str) -> List[str]: + """Decodes a single string to a list of strings.""" + decoded = [] + current_string = "" + i = 0 + + while i < len(s): + if s[i : i + 2] == "/:": + decoded.append(current_string) + current_string = "" + i += 2 + elif s[i : i + 2] == "//": + current_string += "/" + i += 2 + else: + current_string += s[i] + i += 1 + + return decoded diff --git a/counting-bits/mangodm-web.py b/counting-bits/mangodm-web.py new file mode 100644 index 000000000..d4afde6bb --- /dev/null +++ b/counting-bits/mangodm-web.py @@ -0,0 +1,11 @@ +from typing import List + + +class Solution: + def countBits(self, n: int) -> List[int]: + answer = [0] + + for i in range(1, n + 1): + answer.append(answer[i // 2] + i % 2) + + return answer diff --git a/decode-ways/mangodm-web.py b/decode-ways/mangodm-web.py new file mode 100644 index 000000000..7bde67179 --- /dev/null +++ b/decode-ways/mangodm-web.py @@ -0,0 +1,16 @@ +class Solution: + def numDecodings(self, s: str) -> int: + if s[0] == "0": + return 0 + + n = len(s) + dp = [0] * (n + 1) + dp[0], dp[1] = 1, 1 + + for i in range(2, n + 1): + if s[i - 1] != "0": + dp[i] += dp[i - 1] + if "10" <= s[i - 2 : i] <= "26": + dp[i] += dp[i - 2] + + return dp[n] diff --git a/encode-and-decode-strings/mangodm-web.py b/encode-and-decode-strings/mangodm-web.py new file mode 100644 index 000000000..5973bc77a --- /dev/null +++ b/encode-and-decode-strings/mangodm-web.py @@ -0,0 +1,32 @@ +from typing import List + + +class Codec: + def encode(self, strs: List[str]) -> str: + """Encodes a list of strings to a single string.""" + encoded = [] + + for s in strs: + encoded.append(s.replace("/", "//") + "/:") + + return "".join(encoded) + + def decode(self, s: str) -> List[str]: + """Decodes a single string to a list of strings.""" + decoded = [] + current_string = "" + i = 0 + + while i < len(s): + if s[i : i + 2] == "/:": + decoded.append(current_string) + current_string = "" + i += 2 + elif s[i : i + 2] == "//": + current_string += "/" + i += 2 + else: + current_string += s[i] + i += 1 + + return decoded diff --git a/valid-anagram/mangodm-web.py b/valid-anagram/mangodm-web.py new file mode 100644 index 000000000..d855d2084 --- /dev/null +++ b/valid-anagram/mangodm-web.py @@ -0,0 +1,9 @@ +from collections import Counter + + +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + if len(s) != len(t): + return False + + return Counter(s) == Counter(t)