Skip to content

Commit 00451cc

Browse files
committed
new question added
1 parent 9e9a7e4 commit 00451cc

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ nothing but fibonacci series.
179179
- Most problems involving strings can be taken as S(i,j), either we compare last characters then if equal
180180
it gets converted to i-1,j-1 or we take min or max of i,j-1 and i-1,j
181181
- Somtimes if the problem is already a table, you need to reverse engineer it to find the min number of ways to do something. Example create a new table and start from the very bottom and construct the solution updwards. Answer in that case will be the cell 0,0
182+
- If a string and its reversal have a common subsequence, then definately that common subsequence is going to be a palindrome
183+
- Whenever a problem is given involving string, check if LCS can be used
184+
- Whenever a generic problem is given check if it is becoming a fibonacci series or a form of it.
182185

183186
# Topic0: Programming Questions
184187

@@ -403,6 +406,7 @@ it gets converted to i-1,j-1 or we take min or max of i,j-1 and i-1,j
403406
- [Given an amount and some coints, find out in how many ways can this amount be formed](/dynamic-programming/question26.c)
404407
- [Given a 2xn board and tiles of size 2x1, count the number of ways to fill the board using 2x1 tiles](/dynamic-programming/question27.c)
405408
- [Given a cost matrix mxn having a cost at each cell. Find the min cost that it will take to reach cell (m-1,n-1) from top left corner cell (0,0) if the only allowed directions to move are right, down and diagnal down](/dynamic-programming/question28.c)
409+
- [Given a string, find out if it becomes a palindrome or not after doing atmost k deletions](/dynamic-programming/question29.c)
406410

407411
## Some important concepts to solve algos better
408412

dynamic-programming/question29.c

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
Given a string, find out if it becomes a palindrome or not after doing atmost k deletions
3+
4+
Naive approach:
5+
lets say the string is abxca
6+
and value of k=2;
7+
then answer is YES, because we can do a min of 1 deletions and max of 2 deletions
8+
9+
Therefore, number of ways to solve this will be
10+
nc1+ nc2....nck = n^k
11+
which is exponential
12+
13+
METHOD1:
14+
DP Longest common subsequence:
15+
If we reverse the given string and try to find the longest common subsequence of the given string with its
16+
reversal, definately the LCS is going to be palindrome.
17+
If the length of the main string and the LCS is subtracted we will get the min number of deletions
18+
required.
19+
If that min number is less than the max value of k given in the question answer will be YES else NO.
20+
21+
Time complexity: O(n^2) //for LCS
22+
Space complexity: O(n^2) //for LCS
23+
24+
Also, space complexity can be reduced to O(n) time if just one row is maintained at any point of time for LCS
25+
*/
26+
#include <stdio.h>
27+
#include <stdlib.h>
28+
#include <string.h>
29+
30+
void revString(char *str, char *reverse, int len){
31+
int i,j;
32+
int temp;
33+
i = len-1;
34+
j = 0;
35+
while(i>-0){
36+
reverse[j] = str[i];
37+
j++;
38+
i--;
39+
}
40+
printf("inside reverse function\n");
41+
printf("%s\n", reverse);
42+
}
43+
44+
int max(int a, int b){
45+
return a>b?a:b;
46+
}
47+
48+
int checkPal(char *str, int k){
49+
int len = strlen(str);
50+
char reverse[len];
51+
revString(str, reverse, len);
52+
53+
int lcs[len+1][len+1];
54+
int i,j;
55+
for(i=0;i<=len;i++){
56+
lcs[i][0] = 0;
57+
lcs[0][i] = 0;
58+
}
59+
60+
for(i=1;i<=len;i++){
61+
for(j=1;j<=len;j++){
62+
if(str[i-1] == reverse[i-1]){
63+
lcs[i][j] = lcs[i-1][j-1] + 1;
64+
}else{
65+
lcs[i][j] = max(lcs[i-1][j],lcs[i][j-1]);
66+
}
67+
}
68+
}
69+
70+
if(lcs[len][len] > k){
71+
return 0;
72+
}
73+
return 1;
74+
}
75+
76+
int main(){
77+
int k;
78+
printf("enter the value of k\n");
79+
scanf("%d",&k);
80+
char str[] = "acbxcda";
81+
82+
int isPalindrome = checkPal(str,k);
83+
if(isPalindrome){
84+
printf("YES\n");
85+
}else{
86+
printf("NO\n");
87+
}
88+
89+
return 0;
90+
}

nextquestions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,6 @@ TODO:
9898
- DP question26 second case
9999
- Fibonacci series indepth analysis
100100
- DP question27 variation where size of board is nxm and tile is mx1
101+
- longest bitonic sequence
101102

102103

0 commit comments

Comments
 (0)