We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I'm not sure why i'm wrong. I think is in my understanding of subsequence. Even by reading further I think my algorithm is ok, but I only get right the testcase.
My result is just this one:
result = (a * b) + (c * d) + (a * b * c * d);
Note: a, b, c and d represent the number of times that each letter appears in the original string.
first part (a * b) represents the number of different perfect subsequences with only a and b.
second part (c * d), perfect subsequences with c and d.
and third part ( a * b * c * d), the perfect subsequences with a, b, c and d.
E X A M P L E S
Example 1: abcd
a = 1, b = 1, c = 1, d = 1
perfect subsequences = ab, cd, abcd
Those are 3 perfect subsequences.
if we do (a*b)+(c*d)+(a*b*c*d), this is 1 + 1 + 1 = 3, so in here we're right!
Example 2: aabbccdd
a = 2, b = 2, c = 2, d = 2
perfect subsequences ab:
.
b1
b2
a1
a1b1
a1b2
a2
a2b1
a2b2
which is the same as a * b.
perfect subsequences cd:
.
d1
d2
c1
c1d1
c1d2
c2
c2d1
c2d2
which is the same as c * d.
perfect subsequences abcd:
.
c1d1
c1d2
c2d1
c2d2
a1b1
a1b1c1d1
a1b1c1d2
[...
a1b2
a1b2c1d1
a1b2c1d2
...
a2b1
a2b1c1d1
a2b1c1d2
...
a2b2
b2b2c1d1
b2b2c1d2
... ]
which is the same as (ab subsequences) * (cd subsequences)
and ab = a * b, cd = c * d, so,
is the same as a * b * c * d.
tl;dr
ab perfect subsequences = a * b,
cd perfect subsequences = c * d,
abcd perfect subsequences = a * b * c * d.
so all possible perfect subsequences = ab + cd + abcd
= a * b + c * d + a * b * c * d.
But obviously I'm doing something wrong, but I don't know what. My guess is that I'm underestimating the difficulty of this problem, that it isn't so straigth forward.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Counting Perfect Subsequences
You are viewing a single comment's thread. Return to all comments →
I'm not sure why i'm wrong. I think is in my understanding of subsequence. Even by reading further I think my algorithm is ok, but I only get right the testcase.
My result is just this one:
result = (a * b) + (c * d) + (a * b * c * d);
Note: a, b, c and d represent the number of times that each letter appears in the original string.
first part (a * b) represents the number of different perfect subsequences with only a and b.
second part (c * d), perfect subsequences with c and d.
and third part ( a * b * c * d), the perfect subsequences with a, b, c and d.
E X A M P L E S
Example 1: abcd
a = 1, b = 1, c = 1, d = 1
perfect subsequences = ab, cd, abcd
Those are 3 perfect subsequences.
if we do (a*b)+(c*d)+(a*b*c*d), this is 1 + 1 + 1 = 3, so in here we're right!
Example 2: aabbccdd
a = 2, b = 2, c = 2, d = 2
which is the same as a * b.
which is the same as c * d.
which is the same as (ab subsequences) * (cd subsequences)
and ab = a * b, cd = c * d, so,
is the same as a * b * c * d.
tl;dr
ab perfect subsequences = a * b,
cd perfect subsequences = c * d,
abcd perfect subsequences = a * b * c * d.
so all possible perfect subsequences = ab + cd + abcd = a * b + c * d + a * b * c * d.
But obviously I'm doing something wrong, but I don't know what. My guess is that I'm underestimating the difficulty of this problem, that it isn't so straigth forward.