Skip to content

Commit e111fbc

Browse files
authored
feat: add 2 more leetcode problems (#66)
1 parent 61178ed commit e111fbc

File tree

16 files changed

+433
-5
lines changed

16 files changed

+433
-5
lines changed

leetcode/decode_ways/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Decode Ways
2+
3+
**Difficulty:** Medium
4+
**Topics:** String, Dynamic Programming
5+
**Tags:** blind-75
6+
7+
**LeetCode:** [Problem 91](https://leetcode.com/problems/decode-ways/description/)
8+
9+
## Problem Description
10+
11+
You have intercepted a secret message encoded as a string of numbers. The message is decoded via the mapping: `"1" -> 'A', "2" -> 'B', ..., "26" -> 'Z'`. Given a string `s` containing only digits, return the number of ways to decode it. Return `0` if it cannot be decoded.
12+
13+
## Examples
14+
15+
### Example 1:
16+
17+
```
18+
Input: s = "12"
19+
Output: 2
20+
Explanation: "12" could be decoded as "AB" (1 2) or "L" (12).
21+
```
22+
23+
### Example 2:
24+
25+
```
26+
Input: s = "226"
27+
Output: 3
28+
Explanation: "226" could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
29+
```
30+
31+
### Example 3:
32+
33+
```
34+
Input: s = "06"
35+
Output: 0
36+
Explanation: leading zero makes it invalid.
37+
```
38+
39+
## Constraints
40+
41+
- 1 <= s.length <= 100
42+
- s contains only digits and may contain leading zero(s)

leetcode/decode_ways/__init__.py

Whitespace-only changes.

leetcode/decode_ways/helpers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def run_num_decodings(solution_class: type, s: str):
2+
implementation = solution_class()
3+
return implementation.num_decodings(s)
4+
5+
6+
def assert_num_decodings(result: int, expected: int) -> bool:
7+
assert result == expected
8+
return True

leetcode/decode_ways/playground.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# ---
2+
# jupyter:
3+
# jupytext:
4+
# text_representation:
5+
# extension: .py
6+
# format_name: percent
7+
# format_version: '1.3'
8+
# jupytext_version: 1.17.3
9+
# kernelspec:
10+
# display_name: leetcode-py-py3.13
11+
# language: python
12+
# name: python3
13+
# ---
14+
15+
# %%
16+
from helpers import assert_num_decodings, run_num_decodings
17+
from solution import Solution
18+
19+
# %%
20+
# Example test case
21+
s = "226"
22+
expected = 3
23+
24+
# %%
25+
result = run_num_decodings(Solution, s)
26+
result
27+
28+
# %%
29+
assert_num_decodings(result, expected)

leetcode/decode_ways/solution.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution:
2+
3+
# Time: O(n)
4+
# Space: O(1)
5+
def num_decodings(self, s: str) -> int:
6+
if not s:
7+
return 0
8+
9+
num_ways_two_steps_behind: int = 1
10+
num_ways_one_step_behind: int = 0 if s[0] == "0" else 1
11+
12+
for index in range(1, len(s)):
13+
current_char: str = s[index]
14+
previous_char: str = s[index - 1]
15+
16+
current_num_ways: int = 0
17+
18+
if current_char != "0":
19+
current_num_ways += num_ways_one_step_behind
20+
21+
two_digit_value: int = int(previous_char + current_char)
22+
if previous_char != "0" and 10 <= two_digit_value <= 26:
23+
current_num_ways += num_ways_two_steps_behind
24+
25+
num_ways_two_steps_behind, num_ways_one_step_behind = (
26+
num_ways_one_step_behind,
27+
current_num_ways,
28+
)
29+
30+
return num_ways_one_step_behind
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import pytest
2+
3+
from leetcode_py import logged_test
4+
5+
from .helpers import assert_num_decodings, run_num_decodings
6+
from .solution import Solution
7+
8+
9+
class TestDecodeWays:
10+
def setup_method(self):
11+
self.solution = Solution()
12+
13+
@logged_test
14+
@pytest.mark.parametrize(
15+
"s, expected",
16+
[
17+
("12", 2),
18+
("226", 3),
19+
("06", 0),
20+
("0", 0),
21+
("10", 1),
22+
("27", 1),
23+
("101", 1),
24+
("100", 0),
25+
("110", 1),
26+
("2101", 1),
27+
("2611055971756562", 4),
28+
("1", 1),
29+
("30", 0),
30+
],
31+
)
32+
def test_num_decodings(self, s: str, expected: int):
33+
result = run_num_decodings(Solution, s)
34+
assert_num_decodings(result, expected)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Set Matrix Zeroes
2+
3+
**Difficulty:** Medium
4+
**Topics:** Array, Hash Table, Matrix
5+
**Tags:** blind-75
6+
7+
**LeetCode:** [Problem 73](https://leetcode.com/problems/set-matrix-zeroes/description/)
8+
9+
## Problem Description
10+
11+
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s. You must do it in place.
12+
13+
## Examples
14+
15+
### Example 1:
16+
17+
![Example 1](https://assets.leetcode.com/uploads/2020/08/17/mat1.jpg)
18+
19+
```
20+
Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
21+
Output: [[1,0,1],[0,0,0],[1,0,1]]
22+
```
23+
24+
### Example 2:
25+
26+
![Example 2](https://assets.leetcode.com/uploads/2020/08/17/mat2.jpg)
27+
28+
```
29+
Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
30+
Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
31+
```
32+
33+
## Constraints
34+
35+
- m == matrix.length
36+
- n == matrix[i].length
37+
- 1 <= m, n <= 200
38+
- -2^31 <= matrix[i][j] <= 2^31 - 1
39+
40+
- Follow up: Could you devise a constant space solution?

leetcode/set_matrix_zeroes/__init__.py

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def run_set_zeroes(solution_class: type, matrix: list[list[int]]):
2+
import copy
3+
4+
matrix_copy = copy.deepcopy(matrix)
5+
implementation = solution_class()
6+
implementation.set_zeroes(matrix_copy)
7+
return matrix_copy
8+
9+
10+
def assert_set_zeroes(result: list[list[int]], expected: list[list[int]]) -> bool:
11+
assert result == expected
12+
return True
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# ---
2+
# jupyter:
3+
# jupytext:
4+
# text_representation:
5+
# extension: .py
6+
# format_name: percent
7+
# format_version: '1.3'
8+
# jupytext_version: 1.17.3
9+
# kernelspec:
10+
# display_name: leetcode-py-py3.13
11+
# language: python
12+
# name: python3
13+
# ---
14+
15+
# %%
16+
from helpers import assert_set_zeroes, run_set_zeroes
17+
from solution import Solution
18+
19+
# %%
20+
# Example test case
21+
matrix = [[1, 1, 1], [1, 0, 1], [1, 1, 1]]
22+
expected = [[1, 0, 1], [0, 0, 0], [1, 0, 1]]
23+
24+
# %%
25+
result = run_set_zeroes(Solution, matrix)
26+
result
27+
28+
# %%
29+
assert_set_zeroes(result, expected)

0 commit comments

Comments
 (0)