Skip to content

Commit d099945

Browse files
committed
new question added
1 parent ee87380 commit d099945

File tree

4 files changed

+122
-1
lines changed

4 files changed

+122
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ total ways to reach the nth step f(n) = f(n-1) + f(n-2)
176176
i.e from n-1 it can take you 1 step and from n-2 it can take you only 1 step of size 2. Therefore, this is
177177
nothing but fibonacci series.
178178
- In some algos involving DP you can start from n and in that case answer to n is dependent on n-1 and so on.
179-
179+
- Most problems involving strings can be taken as S(i,j), either we compare last characters then if equal
180+
it gets converted to i-1,j-1 or we take min or max of i,j-1 and i-1,j
180181

181182
# Topic0: Programming Questions
182183

@@ -395,6 +396,8 @@ nothing but fibonacci series.
395396
- [Partition problem](/dynamic-programming/question20.c)
396397
- [Find the longest palindromic subsequence](/dynamic-programming/question21.c)
397398
- [Given n-stairs, how many number of ways a person can climb to top from bottom using 1 step or 2 steps](/dynamic-programming/question22.c)
399+
- [Longest non-overlapping repeating sub string](/dynamic-programming/question23.c)
400+
- [Given two strings X and Y, find the minimum cost to make two strings equal using only delete operations. Cost to delete character in X is S and in Y is P](/dynamic-programming/question24.c)
398401

399402
## Some important concepts to solve algos better
400403

dynamic-programming/question23.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
Longest non-overlapping repeating sub string
3+
4+
Total number of sub strings of a given string are n^2.
5+
Now, once we have a given string, we need to see whether that substring is a part of the remaining string or
6+
not. This can be done using KMP algorithm in O(n) time
7+
Therefore total time complexity: O(n^3)
8+
9+
Better method:
10+
11+
12+
*/

dynamic-programming/question24.c

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
Given two strings X and Y, find the minimum cost to make two strings equal using only delete operations.
3+
Cost to delete character in X is S and in Y is P
4+
5+
METHOD1:
6+
DP using LCS
7+
Here we need to minimize the number of delete operations, so that the cost can be minimized.
8+
Hence we need to find as many matching characters in both the strings as possible.
9+
One way is to find the longest common subsequence in both the strings and the remaining characters
10+
apart from those should be deleted.
11+
If the length of one strings is n and other is m
12+
13+
Time complexity: O(mn) //to find LCS
14+
Space complexity: O(mn)
15+
16+
If length of LCS is l
17+
Cost of deletion: (m-l)S + (n-l)P
18+
19+
METHOD2:
20+
Here we use DP:
21+
C(i,j) = {
22+
23+
C(i-1,j-1) if s[i] == s[j],
24+
min{ C(i-1,j),(j-1,i) } , if not equal
25+
26+
27+
}
28+
29+
Therefore we need to minimize cost. Assume that the length of one string is n and other i m. In worst case
30+
C(i,j) can have m*n unique subproblems. A binary tree can be made to see the repeating sub problems as per the
31+
given recurive equation. Hence using bottom-up dynamic programming algo, we can solve this by making a 2d array
32+
with length 1 greater than the total length of the string.
33+
Here 0,1 means that we are comparing string with no letter with string with 1st letter and trying to find
34+
the answer (0,2): string with no letter with string with two letters and so on
35+
36+
Time complexity: O(mn)
37+
Space complexity: O(mn)
38+
*/
39+
40+
//METHOD2
41+
#include <stdio.h>
42+
#include <stdlib.h>
43+
#include <string.h>
44+
45+
int findMin(int a,int b){
46+
return (a<b)?a:b;
47+
}
48+
49+
int findCost(char *str1, char *str2, int cost1, int cost2){
50+
int len1 = strlen(str1);
51+
int len2 = strlen(str2);
52+
53+
int sol[len1+1][len2+1];
54+
55+
int i,j;
56+
57+
sol[0][0] = 0;
58+
59+
for(i=1;i<len1;i++){
60+
sol[i][0] = cost1*i;
61+
}
62+
63+
for(j=1;j<len1;j++){
64+
sol[0][j] = cost2*j;
65+
}
66+
67+
for(i=1;i<len1+1;i++){
68+
for(j=1;j<len2+1;j++){
69+
if(str1[i-1] == str2[j-1]){
70+
sol[i][j] = sol[i-1][j-1];
71+
}else{
72+
sol[i][j] = findMin(sol[i-1][j]+1,sol[i][j-1] + 2);
73+
}
74+
}
75+
}
76+
77+
printf("priting the sol array\n");
78+
79+
for(i=0;i<len1+1;i++){
80+
for(j=0;j<len2+1;j++){
81+
printf("%d ", sol[i][j]);
82+
}
83+
printf("\n");
84+
}
85+
86+
return sol[len1][len2];
87+
}
88+
89+
int main(){
90+
91+
char str1[] = "RAVI";
92+
int cost1 = 1;
93+
int cost2 = 2;
94+
char str2[] = "AIBD";
95+
96+
int cost = findCost(str1,str2, cost1, cost2);
97+
98+
printf("min cost for making two strings equal is: %d\n", cost);
99+
100+
return 0;
101+
}
102+
103+

nextquestions.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,7 @@ TODO:
8888
- program to find fibonacci number in logn time (geeks for geeks)
8989
- DP question 19 to be done
9090
- finding all palindromes in a given string
91+
- generalize the steps in question22 to m steps at a time
92+
- DP question23 to be done
93+
9194

0 commit comments

Comments
 (0)