Skip to content

Commit

Permalink
Merge pull request #366 from mangodm-web/main
Browse files Browse the repository at this point in the history
[mangodm-web] Week 02 Solutions
  • Loading branch information
DaleSeo authored Aug 25, 2024
2 parents fac6215 + cf50f90 commit 8605a7d
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
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)

0 comments on commit 8605a7d

Please sign in to comment.