|
| 1 | +The key idea here is that it is not only strings of the form - AGC, ACG, GAC that we need to watch out for ... But also strings of length 4 like - A_GC, AG_C, AGC_ and _AGC |
| 2 | + |
| 3 | +So, we will maintain f(i, xyz) which represents the number of strings of length i which ends with xyz. |
| 4 | + |
| 5 | +Now, to establish the transition, we will iterate over all possibilities w, such that 'wxyz' is a valid four-letter string and add f(i - 1, wxy) to f(i, xyz). |
| 6 | + |
| 7 | +---- |
| 8 | + |
| 9 | +Here is how we check if a 4-length string is good or not. |
| 10 | + |
| 11 | +long long no_of_strings[N][NO_OF_LETTERS][NO_OF_LETTERS][NO_OF_LETTERS]; |
| 12 | +char letter[NO_OF_LETTERS] = {'A', 'C', 'G', 'T'}; |
| 13 | + |
| 14 | +int ok(int last_1, int last_2, int last_3, int last_4) |
| 15 | +{ |
| 16 | + char l1 = letter[last_1], l2 = letter[last_2], l3 = letter[last_3], l4 = letter[last_4]; |
| 17 | + |
| 18 | + if(l1 == 'A' && l2 == 'G' && l4 == 'C') |
| 19 | + return false; |
| 20 | + if(l1 == 'A' && l3 == 'G' && l4 == 'C') |
| 21 | + return false; |
| 22 | + if(l1 == 'A' && l2 == 'G' && l3 == 'C') |
| 23 | + return false; |
| 24 | + if(l2 == 'A' && l3 == 'G' && l4 == 'C') |
| 25 | + return false; |
| 26 | + if(l1 == 'G' && l2 == 'A' && l3 == 'C') |
| 27 | + return false; |
| 28 | + if(l2 == 'G' && l3 == 'A' && l4 == 'C') |
| 29 | + return false; |
| 30 | + if(l1 == 'A' && l2 == 'C' && l3 == 'G') |
| 31 | + return false; |
| 32 | + if(l2 == 'A' && l3 == 'C' && l4 == 'G') |
| 33 | + return false; |
| 34 | + |
| 35 | + return true; |
| 36 | +} |
| 37 | + |
| 38 | +----- |
| 39 | + |
| 40 | +We have established the recurrence, but keep in mind that the case for i = 1, 2 are special cases because these strings do not have suffixes of length 3 ! |
| 41 | + |
| 42 | +So for convenience, we can assume that the character at position 0, and -1 is 'T'. |
| 43 | + |
| 44 | +Here is how I handled it - |
| 45 | + |
| 46 | +if(i == 1) |
| 47 | +{ |
| 48 | + no_of_strings[i][last_1][T_INDEX][T_INDEX] = 1; |
| 49 | +} |
| 50 | +else if(i == 2) |
| 51 | +{ |
| 52 | + no_of_strings[i][last_1][last_2][T_INDEX] = 4; |
| 53 | +} |
| 54 | + |
| 55 | +Suppose for i = 1 case, I always update f(1, xTT) only. The reason is that I don't want to over count. |
| 56 | + |
| 57 | +Suppose we set every combination of f(1, xyz) = 1, then when we are counting the answer for i = 1, we will be counting the answer as 64 ! When it is actually just 1. So we ensure that for every possible last digit, we are updating only one state rather than 64 states. |
| 58 | + |
| 59 | +Similarly for the i = 2 case. We hard-code the third index. This ensures that for every unique pair, we only only update one state, rather than 4 states. |
| 60 | + |
| 61 | +This helps us in counting correctly. |
| 62 | + |
| 63 | +--- |
| 64 | + |
| 65 | +The i = 3 case is also a special case ! Although i = 3 does have a substring of length 3 in the end, it cannot transition to i - 1, with a suffix of length 3. |
| 66 | + |
| 67 | +So, we must treat it differently. |
| 68 | + |
| 69 | +For i = 3, we will just check if the last 3 letters form a good string and then we will update it. |
| 70 | + |
| 71 | +Suppose xyz is a good string, then we will set f(3, xyz) = 1 |
| 72 | + |
| 73 | +--- |
| 74 | + |
| 75 | +From i >= 4, it is very easy |
| 76 | + |
| 77 | +else if(ok(last_4, last_3, last_2, last_1)) |
| 78 | +{ |
| 79 | + no_of_strings[i][last_1][last_2][last_3] += no_of_strings[i - 1][last_2][last_3][last_4]; |
| 80 | + } |
| 81 | + |
| 82 | +---- |
| 83 | + |
| 84 | +Actually, the constraints of the problem start from 3 so they will not test the corner cases of N = 1 and N = 2 but it is still good to have ! |
0 commit comments