Skip to content

Commit 8605a7d

Browse files
authored
Merge pull request #366 from mangodm-web/main
[mangodm-web] Week 02 Solutions
2 parents fac6215 + cf50f90 commit 8605a7d

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from typing import List
2+
3+
4+
class Codec:
5+
def encode(self, strs: List[str]) -> str:
6+
"""Encodes a list of strings to a single string."""
7+
encoded = []
8+
9+
for s in strs:
10+
encoded.append(s.replace("/", "//") + "/:")
11+
12+
return "".join(encoded)
13+
14+
def decode(self, s: str) -> List[str]:
15+
"""Decodes a single string to a list of strings."""
16+
decoded = []
17+
current_string = ""
18+
i = 0
19+
20+
while i < len(s):
21+
if s[i : i + 2] == "/:":
22+
decoded.append(current_string)
23+
current_string = ""
24+
i += 2
25+
elif s[i : i + 2] == "//":
26+
current_string += "/"
27+
i += 2
28+
else:
29+
current_string += s[i]
30+
i += 1
31+
32+
return decoded

counting-bits/mangodm-web.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def countBits(self, n: int) -> List[int]:
6+
answer = [0]
7+
8+
for i in range(1, n + 1):
9+
answer.append(answer[i // 2] + i % 2)
10+
11+
return answer

decode-ways/mangodm-web.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def numDecodings(self, s: str) -> int:
3+
if s[0] == "0":
4+
return 0
5+
6+
n = len(s)
7+
dp = [0] * (n + 1)
8+
dp[0], dp[1] = 1, 1
9+
10+
for i in range(2, n + 1):
11+
if s[i - 1] != "0":
12+
dp[i] += dp[i - 1]
13+
if "10" <= s[i - 2 : i] <= "26":
14+
dp[i] += dp[i - 2]
15+
16+
return dp[n]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from typing import List
2+
3+
4+
class Codec:
5+
def encode(self, strs: List[str]) -> str:
6+
"""Encodes a list of strings to a single string."""
7+
encoded = []
8+
9+
for s in strs:
10+
encoded.append(s.replace("/", "//") + "/:")
11+
12+
return "".join(encoded)
13+
14+
def decode(self, s: str) -> List[str]:
15+
"""Decodes a single string to a list of strings."""
16+
decoded = []
17+
current_string = ""
18+
i = 0
19+
20+
while i < len(s):
21+
if s[i : i + 2] == "/:":
22+
decoded.append(current_string)
23+
current_string = ""
24+
i += 2
25+
elif s[i : i + 2] == "//":
26+
current_string += "/"
27+
i += 2
28+
else:
29+
current_string += s[i]
30+
i += 1
31+
32+
return decoded

valid-anagram/mangodm-web.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from collections import Counter
2+
3+
4+
class Solution:
5+
def isAnagram(self, s: str, t: str) -> bool:
6+
if len(s) != len(t):
7+
return False
8+
9+
return Counter(s) == Counter(t)

0 commit comments

Comments
 (0)