Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mangodm-web] Week 02 Solutions #366

Merged
merged 5 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions counting-bits/mangodm-web.py
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions decode-ways/mangodm-web.py
Original file line number Diff line number Diff line change
@@ -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]
32 changes: 32 additions & 0 deletions encode-and-decode-strings/mangodm-web.py
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions valid-anagram/mangodm-web.py
Original file line number Diff line number Diff line change
@@ -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)
Loading