Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 02c81b2

Browse files
committed
Adding Dynamic Programming Module to this repo.
1 parent 07c84a6 commit 02c81b2

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

DynamicProgramming/__init__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
Dynamic programming is a method for solving complex problems by breaking them down into simpler subproblems. It is applicable when the problem can be divided into overlapping subproblems that can be solved independently. The main idea is to store the results of subproblems to avoid redundant computations, which can significantly improve the efficiency of the algorithm.
3+
4+
Dynamic programming is particularly useful in optimization problems where the goal is to find the best solution among many possible ones. It is commonly used in various fields such as operations research, economics, and computer science.
5+
6+
Example:
7+
A classic example of dynamic programming is the Fibonacci sequence, where each number is the sum of the two preceding ones. Using dynamic programming, we can store the results of previously computed Fibonacci numbers to avoid redundant calculations.
8+
9+
Here is a simple implementation of the Fibonacci sequence using dynamic programming in Python:
10+
11+
def fibonacci(n):
12+
# Create an array to store Fibonacci numbers
13+
fib = [0] * (n + 1)
14+
# Base cases
15+
fib[0] = 0
16+
fib[1] = 1
17+
# Compute the Fibonacci numbers in a bottom-up manner
18+
for i in range(2, n + 1):
19+
fib[i] = fib[i - 1] + fib[i - 2]
20+
return fib[n]
21+
22+
print(fibonacci(10)) # Output: 55
23+
24+
In this example, the function `fibonacci` computes the nth Fibonacci number by storing the results of previous computations in an array, thus avoiding redundant calculations and improving efficiency.
25+
"""

DynamicProgramming/abbrevation.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Problem Link : https://www.hackerrank.com/challenges/abbr/problem
3+
You can perform the following operation on some string, :
4+
5+
1. Capitalize zero or more of 's lowercase letters at some index i
6+
(i.e., make them uppercase).
7+
2. Delete all of the remaining lowercase letters in .
8+
9+
Example:
10+
a=daBcd and b="ABC"
11+
daBcd -> capitalize a and c(dABCd) -> remove d (ABC)
12+
"""
13+
14+
def abbrevation(first_input: str, second_input: str) -> bool:
15+
"""
16+
Determines if string a can be transformed into string b by capitalizing
17+
zero or more lowercase letters and deleting all other lowercase letters.
18+
19+
Args:
20+
a (str): The original string.
21+
b (str): The target string.
22+
23+
Returns:
24+
bool: True if a can be transformed into b, False otherwise.
25+
"""
26+
27+
dp = [[False] * (len(first_input) + 1) for _ in range(len(second_input) + 1)]
28+
dp[0][0] = True
29+
30+
for i in range(len(second_input)):
31+
for j in range(len(first_input) + 1):
32+
if dp[i][j]:
33+
if j < len(first_input) and first_input[i].upper() == second_input[j]:
34+
dp[i + 1][j + 1] = True
35+
if first_input[i].islower():
36+
dp[i + 1][j] = True
37+
38+
return dp[len(second_input)][len(first_input)]
39+
40+
print(abbrevation("daBcd", "ABC"))

0 commit comments

Comments
 (0)