Skip to content
Open
Show file tree
Hide file tree
Changes from 53 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
35c6c7e
Add: Matrix Prefix Sum
invincible04 Oct 23, 2023
a8603ea
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
b21b10d
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
322e45a
Merge branch 'master' of https://github.com/aayushsoni4/Python
invincible04 Oct 23, 2023
89d32bd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
2cee13c
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
4e2e56e
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
de886cf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
336adf2
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
28d9cfd
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
b43f475
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
37718c7
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
e5ee235
Merge branch 'master' of https://github.com/aayushsoni4/Python
invincible04 Oct 23, 2023
e9403b2
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
7d7e2e7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
f987823
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
c0e18be
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
4ed2d72
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
157c744
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
6e5c84c
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
5c3a01f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
6b718c5
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
2c78030
Merge branch 'master' of https://github.com/aayushsoni4/Python
invincible04 Oct 23, 2023
11aa213
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
3ab1ea6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
97d1414
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
14000b3
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
0853c35
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
b01f637
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
13d806e
Merge branch 'master' of https://github.com/aayushsoni4/Python
invincible04 Oct 23, 2023
778df87
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
b568f9d
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
b03426e
Merge branch 'master' of https://github.com/aayushsoni4/Python
invincible04 Oct 23, 2023
aba2ef2
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
6280be4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
896f3a9
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
abe5fb0
Merge branch 'master' of https://github.com/aayushsoni4/Python
invincible04 Oct 23, 2023
01e6e1c
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
27022eb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
0d8007d
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
bc5276d
Merge branch 'master' of https://github.com/aayushsoni4/Python
invincible04 Oct 23, 2023
fbc4fa7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
b9311e9
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
14e51db
Merge branch 'master' of https://github.com/aayushsoni4/Python
invincible04 Oct 23, 2023
9da4957
Add: Distinct Subsequences
invincible04 Oct 23, 2023
b2f0756
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
61a2824
Changes made in Distinct Subsequences
invincible04 Oct 23, 2023
ff23cb8
Changes made in Distinct Subsequences
invincible04 Oct 23, 2023
3329261
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
fb935b2
Changes made in Distinct Subsequences
invincible04 Oct 23, 2023
b7e51c9
Merge branch 'master' of https://github.com/aayushsoni4/Python
invincible04 Oct 23, 2023
aaf1c3b
Changes made in Distinct Subsequences
invincible04 Oct 23, 2023
e268227
Changes made in Distinct Subsequences
invincible04 Oct 23, 2023
6ce845a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
7cb9ca2
Removed Distinct Subsequences
invincible04 Oct 23, 2023
2eda0a9
Removed Distinct Subsequences
invincible04 Oct 23, 2023
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions dynamic_programming/distinct_sequences.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"""
author: Aayush Soni
Given two strings s and t, return the number of distinct subsequences
of s which equals t.
Input: s = "babgbag", t = "bag"
Output: 5
Explanation: As shown below, there are 5 ways you can generate "bag" from s.
>>> "babgbag"
>>> "babgbag"
>>> "babgbag"
>>> "babgbag"
>>> "babgbag"
Leetcode link: https://leetcode.com/problems/distinct-subsequences/description/
"""

def count_distinct_subsequences(s: str, t: str) -> int:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: s

Please provide descriptive name for the parameter: t

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: s

Please provide descriptive name for the parameter: t

"""
This function calculates the number of distinct
subsequences of s that equal t.

:param s: The input string s.
:param t: The target string t.
:return: The number of distinct subsequences of s that equal t.

>>> countDistinctSubsequences("babgbag", "bag")
5
>>> countDistinctSubsequences("rabbbit", "rabbit")
3
"""

m = len(t) # Length of t
n = len(s) # Length of s

# If t is longer than s, it can't be a subsequence.
if m > n:
return 0

# Create a matrix to store counts of subsequences.
# dp[i][j] stores the count of occurrences of t(1..i) in s(1..j).
dp = [[0 for _ in range(n + 1)] for __ in range(m + 1)]

# Initialize the first column with all 0s.
# An empty string can't have another string as a subsequence.
for i in range(1, m + 1):
dp[i][0] = 0

# Initialize the first row with all 1s.
# An empty string is a subsequence of all strings.
for j in range(n + 1):
dp[0][j] = 1

# Fill dp[][] in a bottom-up manner
for i in range(1, m + 1):
for j in range(1, n + 1):
# If the last characters don't match,
# the value is the same as the value without the last character in s.
if t[i - 1] != s[j - 1]:
dp[i][j] = dp[i][j - 1]

# If the last characters match,
# the value is obtained by considering two cases:
# a) All subsequences without the last character in s.
# b) All subsequences without the last characters in both s and t.
else:
dp[i][j] = dp[i][j - 1] + dp[i - 1][j - 1]

return dp[m][n]


if __name__ == "__main__":
s = "babgbag"
t = "bag"
result = count_distinct_subsequences(s, t)
print(f"The number of distinct subsequences of '{t}' in '{s}' is {result}.")
86 changes: 86 additions & 0 deletions matrix/matrix_prefix_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
"""
Python Program to find prefix sum of a 2D array
"""


def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]:
"""
Calculate the prefix sum of a 2D matrix.
Prefix Sum Formula:
prefix_sum[i][j] = prefix_sum[i - 1][j] + prefix_sum[i][j - 1]
- prefix_sum[i - 1][j - 1] + matrix[i][j]

:param matrix: A 2D matrix.
:return: A matrix containing the prefix sums.

>>> calculate_prefix_sum([[1, 1, 1], [1, 1, 1], [1, 1, 1]])
[[1, 2, 3], [2, 4, 6], [3, 6, 9]]

>>> calculate_prefix_sum([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
[[1, 3, 6], [5, 12, 21], [12, 27, 45]]
"""
rows = len(matrix)
cols = len(matrix[0])

# Initialize the prefix sum matrix with zeros, with the
# same dimensions as the original matrix.
prefix_sum = [[0 for _ in range(cols)] for _ in range(rows)]

# Calculate the prefix sum for the top-left cell.
prefix_sum[0][0] = matrix[0][0]

# Calculate cumulative sums for the first row.
for i in range(1, cols):
prefix_sum[0][i] = prefix_sum[0][i - 1] + matrix[0][i]

# Calculate cumulative sums for the first column.
for i in range(1, rows):
prefix_sum[i][0] = prefix_sum[i - 1][0] + matrix[i][0]

# Update the values in the cells using the general formula.
for i in range(1, rows):
for j in range(1, cols):
# The value in each cell is the sum of:
# - The cell above it
# - The cell to the left of it
# - Subtracting the overlapping cell
# - Adding the value from the original matrix
prefix_sum[i][j] = (
prefix_sum[i - 1][j]
+ prefix_sum[i][j - 1]
- prefix_sum[i - 1][j - 1]
+ matrix[i][j]
)

return prefix_sum


def display_matrix(matrix: list[list[int]]) -> None:
"""
Display a 2D matrix.

:param matrix: A 2D matrix.

Display a 2D matrix by printing each row's elements separated by spaces.

>>> display_matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
1 2 3
4 5 6
7 8 9

>>> display_matrix([[10, 20, 30], [40, 50, 60]])
10 20 30
40 50 60
"""
for row in matrix:
# Join the elements of each row with spaces and print the result.
print(" ".join(map(str, row)))


if __name__ == "__main__":
matrix = [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
# Calculate the prefix sum of the 2D matrix
prefix_sum_matrix = calculate_prefix_sum(matrix)

# Display the prefix sum matrix
display_matrix(prefix_sum_matrix)