Skip to content
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
42 changes: 42 additions & 0 deletions leetcode/decode_ways/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Decode Ways

**Difficulty:** Medium
**Topics:** String, Dynamic Programming
**Tags:** blind-75

**LeetCode:** [Problem 91](https://leetcode.com/problems/decode-ways/description/)

## Problem Description

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.

## Examples

### Example 1:

```
Input: s = "12"
Output: 2
Explanation: "12" could be decoded as "AB" (1 2) or "L" (12).
```

### Example 2:

```
Input: s = "226"
Output: 3
Explanation: "226" could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
```

### Example 3:

```
Input: s = "06"
Output: 0
Explanation: leading zero makes it invalid.
```

## Constraints

- 1 <= s.length <= 100
- s contains only digits and may contain leading zero(s)
Empty file.
8 changes: 8 additions & 0 deletions leetcode/decode_ways/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def run_num_decodings(solution_class: type, s: str):
implementation = solution_class()
return implementation.num_decodings(s)


def assert_num_decodings(result: int, expected: int) -> bool:
assert result == expected
return True
29 changes: 29 additions & 0 deletions leetcode/decode_ways/playground.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# ---
# jupyter:
# jupytext:
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.17.3
# kernelspec:
# display_name: leetcode-py-py3.13
# language: python
# name: python3
# ---

# %%
from helpers import assert_num_decodings, run_num_decodings
from solution import Solution

# %%
# Example test case
s = "226"
expected = 3

# %%
result = run_num_decodings(Solution, s)
result

# %%
assert_num_decodings(result, expected)
30 changes: 30 additions & 0 deletions leetcode/decode_ways/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution:

# Time: O(n)
# Space: O(1)
def num_decodings(self, s: str) -> int:
if not s:
return 0

num_ways_two_steps_behind: int = 1
num_ways_one_step_behind: int = 0 if s[0] == "0" else 1

for index in range(1, len(s)):
current_char: str = s[index]
previous_char: str = s[index - 1]

current_num_ways: int = 0

if current_char != "0":
current_num_ways += num_ways_one_step_behind

two_digit_value: int = int(previous_char + current_char)
if previous_char != "0" and 10 <= two_digit_value <= 26:
current_num_ways += num_ways_two_steps_behind

num_ways_two_steps_behind, num_ways_one_step_behind = (
num_ways_one_step_behind,
current_num_ways,
)

return num_ways_one_step_behind
34 changes: 34 additions & 0 deletions leetcode/decode_ways/test_solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pytest

from leetcode_py import logged_test

from .helpers import assert_num_decodings, run_num_decodings
from .solution import Solution


class TestDecodeWays:
def setup_method(self):
self.solution = Solution()

@logged_test
@pytest.mark.parametrize(
"s, expected",
[
("12", 2),
("226", 3),
("06", 0),
("0", 0),
("10", 1),
("27", 1),
("101", 1),
("100", 0),
("110", 1),
("2101", 1),
("2611055971756562", 4),
("1", 1),
("30", 0),
],
)
def test_num_decodings(self, s: str, expected: int):
result = run_num_decodings(Solution, s)
assert_num_decodings(result, expected)
40 changes: 40 additions & 0 deletions leetcode/set_matrix_zeroes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Set Matrix Zeroes

**Difficulty:** Medium
**Topics:** Array, Hash Table, Matrix
**Tags:** blind-75

**LeetCode:** [Problem 73](https://leetcode.com/problems/set-matrix-zeroes/description/)

## Problem Description

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.

## Examples

### Example 1:

![Example 1](https://assets.leetcode.com/uploads/2020/08/17/mat1.jpg)

```
Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
Output: [[1,0,1],[0,0,0],[1,0,1]]
```

### Example 2:

![Example 2](https://assets.leetcode.com/uploads/2020/08/17/mat2.jpg)

```
Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
```

## Constraints

- m == matrix.length
- n == matrix[i].length
- 1 <= m, n <= 200
- -2^31 <= matrix[i][j] <= 2^31 - 1

- Follow up: Could you devise a constant space solution?
Empty file.
12 changes: 12 additions & 0 deletions leetcode/set_matrix_zeroes/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def run_set_zeroes(solution_class: type, matrix: list[list[int]]):
import copy

matrix_copy = copy.deepcopy(matrix)
implementation = solution_class()
implementation.set_zeroes(matrix_copy)
return matrix_copy


def assert_set_zeroes(result: list[list[int]], expected: list[list[int]]) -> bool:
assert result == expected
return True
29 changes: 29 additions & 0 deletions leetcode/set_matrix_zeroes/playground.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# ---
# jupyter:
# jupytext:
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.17.3
# kernelspec:
# display_name: leetcode-py-py3.13
# language: python
# name: python3
# ---

# %%
from helpers import assert_set_zeroes, run_set_zeroes
from solution import Solution

# %%
# Example test case
matrix = [[1, 1, 1], [1, 0, 1], [1, 1, 1]]
expected = [[1, 0, 1], [0, 0, 0], [1, 0, 1]]

# %%
result = run_set_zeroes(Solution, matrix)
result

# %%
assert_set_zeroes(result, expected)
29 changes: 29 additions & 0 deletions leetcode/set_matrix_zeroes/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution:

# Time: O(m * n)
# Space: O(1)
def set_zeroes(self, matrix: list[list[int]]) -> None:
row_count: int = len(matrix)
col_count: int = len(matrix[0]) if row_count > 0 else 0

first_row_has_zero: bool = any(matrix[0][col_index] == 0 for col_index in range(col_count))
first_col_has_zero: bool = any(matrix[row_index][0] == 0 for row_index in range(row_count))

for row_index in range(1, row_count):
for col_index in range(1, col_count):
if matrix[row_index][col_index] == 0:
matrix[row_index][0] = 0
matrix[0][col_index] = 0

for row_index in range(1, row_count):
for col_index in range(1, col_count):
if matrix[row_index][0] == 0 or matrix[0][col_index] == 0:
matrix[row_index][col_index] = 0

if first_row_has_zero:
for col_index in range(col_count):
matrix[0][col_index] = 0

if first_col_has_zero:
for row_index in range(row_count):
matrix[row_index][0] = 0
42 changes: 42 additions & 0 deletions leetcode/set_matrix_zeroes/test_solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import pytest

from leetcode_py import logged_test

from .helpers import assert_set_zeroes, run_set_zeroes
from .solution import Solution


class TestSetMatrixZeroes:
def setup_method(self):
self.solution = Solution()

@logged_test
@pytest.mark.parametrize(
"matrix, expected",
[
([[1, 1, 1], [1, 0, 1], [1, 1, 1]], [[1, 0, 1], [0, 0, 0], [1, 0, 1]]),
(
[[0, 1, 2, 0], [3, 4, 5, 2], [1, 3, 1, 5]],
[[0, 0, 0, 0], [0, 4, 5, 0], [0, 3, 1, 0]],
),
([[1]], [[1]]),
([[0]], [[0]]),
([[1, 0]], [[0, 0]]),
([[0, 1]], [[0, 0]]),
([[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
([[1, 2, 0], [4, 5, 6], [7, 8, 9]], [[0, 0, 0], [4, 5, 0], [7, 8, 0]]),
([[0, 2, 3], [4, 5, 6], [7, 8, 9]], [[0, 0, 0], [0, 5, 6], [0, 8, 9]]),
([[1, 2, 3], [4, 0, 6], [7, 8, 0]], [[1, 0, 0], [0, 0, 0], [0, 0, 0]]),
([[1, 2], [0, 4], [5, 6]], [[0, 2], [0, 0], [0, 6]]),
([[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]),
([[0, 2, 0], [4, 5, 6]], [[0, 0, 0], [0, 5, 0]]),
([[-1, 2, 3], [4, 0, -6]], [[-1, 0, 3], [0, 0, 0]]),
(
[[1, 2, 3, 4], [0, 5, 0, 7], [8, 9, 10, 11]],
[[0, 2, 0, 4], [0, 0, 0, 0], [0, 9, 0, 11]],
),
],
)
def test_set_zeroes(self, matrix: list[list[int]], expected: list[list[int]]):
result = run_set_zeroes(Solution, matrix)
assert_set_zeroes(result, expected)
67 changes: 67 additions & 0 deletions leetcode_py/cli/resources/leetcode/json/problems/decode_ways.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"problem_name": "decode_ways",
"solution_class_name": "Solution",
"problem_number": "91",
"problem_title": "Decode Ways",
"difficulty": "Medium",
"topics": "String, Dynamic Programming",
"_tags": { "list": ["blind-75"] },
"readme_description": "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.",
"_readme_examples": {
"list": [
{
"content": "```\nInput: s = \"12\"\nOutput: 2\nExplanation: \"12\" could be decoded as \"AB\" (1 2) or \"L\" (12).\n```"
},
{
"content": "```\nInput: s = \"226\"\nOutput: 3\nExplanation: \"226\" could be decoded as \"BZ\" (2 26), \"VF\" (22 6), or \"BBF\" (2 2 6).\n```"
},
{
"content": "```\nInput: s = \"06\"\nOutput: 0\nExplanation: leading zero makes it invalid.\n```"
}
]
},
"readme_constraints": "- 1 <= s.length <= 100\n- s contains only digits and may contain leading zero(s)",
"readme_additional": "",
"helpers_imports": "",
"helpers_content": "",
"helpers_run_name": "num_decodings",
"helpers_run_signature": "(solution_class: type, s: str)",
"helpers_run_body": " implementation = solution_class()\n return implementation.num_decodings(s)",
"helpers_assert_name": "num_decodings",
"helpers_assert_signature": "(result: int, expected: int) -> bool",
"helpers_assert_body": " assert result == expected\n return True",
"solution_imports": "",
"solution_contents": "",
"solution_class_content": "",
"test_imports": "import pytest\nfrom leetcode_py import logged_test\nfrom .helpers import assert_num_decodings, run_num_decodings\nfrom .solution import Solution",
"test_content": "",
"test_class_name": "DecodeWays",
"test_class_content": " def setup_method(self):\n self.solution = Solution()",
"_solution_methods": {
"list": [
{
"name": "num_decodings",
"signature": "(self, s: str) -> int",
"body": " # TODO: Implement num_decodings\n return 0"
}
]
},
"_test_helper_methods": {
"list": [{ "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" }]
},
"_test_methods": {
"list": [
{
"name": "test_num_decodings",
"signature": "(self, s: str, expected: int)",
"parametrize": "s, expected",
"test_cases": "[('12', 2), ('226', 3), ('06', 0), ('0', 0), ('10', 1), ('27', 1), ('101', 1), ('100', 0), ('110', 1), ('2101', 1), ('2611055971756562', 4), ('1', 1), ('30', 0)]",
"body": " result = run_num_decodings(Solution, s)\n assert_num_decodings(result, expected)"
}
]
},
"playground_imports": "from helpers import run_num_decodings, assert_num_decodings\nfrom solution import Solution",
"playground_setup": "# Example test case\ns = '226'\nexpected = 3",
"playground_run": "result = run_num_decodings(Solution, s)\nresult",
"playground_assert": "assert_num_decodings(result, expected)"
}
Loading