Skip to content

Commit 966fb4b

Browse files
committed
new question added
1 parent 176f9bd commit 966fb4b

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ it gets converted to i-1,j-1 or we take min or max of i,j-1 and i-1,j
398398
- [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)
399399
- [Longest non-overlapping repeating sub string](/dynamic-programming/question23.c)
400400
- [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)
401+
- [Count the number of times string occured as the subsequence of the other string](/dynamic-programming/question25.c)
401402

402403
## Some important concepts to solve algos better
403404

@@ -408,7 +409,7 @@ it gets converted to i-1,j-1 or we take min or max of i,j-1 and i-1,j
408409
- 'XOR' of zero with a number is the number itself
409410
- '1s compliment is number obtained by reversing all the bits in the binary representation of a number. o to 1 and 1 to 0'
410411
- Linear hashing is (h(k) + i)modm (where m is the size of the hash table, h(k) is the hash function that takes the key k and returns a value i is the parameter that is incremented to get different values)
411-
- Subarrays of an array are always contiguous whereas subsequence may not be contiguous
412+
- Subarrays of an array are always contiguous whereas subsequence may not be contiguous but needs to be in the increasing order. Same applies to a string as well.
412413
- There are only two ways to make any data structure one is to use an array (where size is fixed and memory is contiguous) OR you can use the heap memory (structures and linked list). So Array and linked list or combo of these two are used to implement any data structure. In most cases linked list takes more time to do operations if data structure is implemented using it. But advantage is dynamic memory allocation
413414
- For INFIX to POSTFIX conversion, data structure used is stack. In stack all the operators are stored. For evaluation of POSTFIX stack is used to store operands
414415
- Evaluating and expression = convert from INFIX to postfix --> Evaluate POSTFIX. Time complexity: O(n)

dynamic-programming/question25.c

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
Count the number of times string occured as the subsequence of the other string
3+
4+
METHOD1:
5+
Naive approach, where we find all the subsequences and see whenever that subsequence is equal to the string
6+
given. This will take exponential time
7+
8+
METHOD2:
9+
We can find the LCS which will be ideally equal to the length of the string given in the second case.
10+
If the solution is also the string that is okay otherwise the string does not repeat even once.
11+
Now if the solution is the string we can go back and construct the total number of solutions which are
12+
of that length (because obviously if they are of length equal to the string, it will be the string itself)
13+
Hence we can maintain a counter for that and return its value
14+
15+
Time complexity: O(mn) //LCS
16+
Space complexity: O(mn)
17+
18+
Rest can be done in lesser time as compared to this, hence the answer
19+
20+
21+
METHOD3:
22+
Dynamic programming but not using LCS:
23+
Here we first try to write the recusive equations
24+
25+
c[i,j] = {
26+
c[i-1][j] + c[i-1][j-1] //if equal
27+
//that means we search the matched value in the remaining string as well as ignoring the matched value
28+
searching the rest in the remaining string
29+
30+
c[i-1][j] //if the value does not match
31+
}
32+
33+
We convert it into a program and we will get the answer.
34+
c[i,j] represents count of total number of times string till jth position occur in the string till ith position
35+
36+
Time complexity: O(mn)
37+
Space complexity: O(mn)
38+
39+
*/
40+
//METHOD3
41+
#include <stdio.h>
42+
#include <stdlib.h>
43+
#include <string.h>
44+
45+
int findCount(char *str1, char *str2){
46+
47+
int len1 = strlen(str1);
48+
int len2 = strlen(str2);
49+
50+
int sol[len1+1][len2+1];
51+
52+
int i,j;
53+
for(i=0;i<=len1;i++){
54+
sol[i][0] = 1;
55+
}
56+
57+
for(j=1;j<=len2;j++){
58+
sol[0][j] = 0;
59+
}
60+
61+
for(i=1;i<=len1;i++){
62+
for(j=1;j<=len2;j++){
63+
if(str1[i-1] == str2[j-1]){
64+
sol[i][j] = sol[i-1][j-1] + sol[i-1][j];
65+
}else{
66+
sol[i][j] = sol[i-1][j];
67+
}
68+
}
69+
}
70+
71+
printf("the sol array is\n");
72+
73+
for(i=0;i<=len1;i++){
74+
for(j=0;j<=len2;j++){
75+
printf("%d ", sol[i][j]);
76+
}
77+
printf("\n");
78+
}
79+
80+
return sol[len1][len2];
81+
82+
}
83+
84+
int main(){
85+
char str1[] = "GeeksforGeeks";
86+
char str2[] = "Gks";
87+
88+
int count = findCount(str1, str2);
89+
90+
printf("count is %d\n", count);
91+
92+
return 0;
93+
}
94+

nextquestions.md

+1
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,6 @@ TODO:
9090
- finding all palindromes in a given string
9191
- generalize the steps in question22 to m steps at a time
9292
- DP question23 to be done
93+
- DP solution to be constructed for LCS problem
9394

9495

0 commit comments

Comments
 (0)