|
| 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 | + |
0 commit comments