Check the equality for each subset of a string S against the target T and return a count
up vote
1
down vote
favorite
I wrote the following code to solve this leetcode problem:
var numDistinct = function(s, t) {
if (!s.length) return +(!t.length);
let res = 0;
for (let seq of subseq(s)) res += +(t===seq);
return res;
};
var subseq = function(a) {
if (a.length==1) return [a[0], ''];
let subsets = subseq(a.substr(0, a.length-1));
let n = subsets.length;
for (let i=0; i < n; i++) {
subsets.push(subsets[i]+a[a.length-1]);
}
return subsets;
};
Obviously, this code is not efficient, as it generates each of $2^n$ subsets of the string s
and checks each one for equality against t
.
I would much appreciate it if someone could explain how to improve this solution using memoization. I am aware that there is a dynamic programming solution. I am more interested in learning how to extend this easy-to-understand solution into something more efficient by using a memo table. Any and all help would be graciously appreciated.
javascript recursion dynamic-programming memoization
bumped to the homepage by Community♦ 13 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
up vote
1
down vote
favorite
I wrote the following code to solve this leetcode problem:
var numDistinct = function(s, t) {
if (!s.length) return +(!t.length);
let res = 0;
for (let seq of subseq(s)) res += +(t===seq);
return res;
};
var subseq = function(a) {
if (a.length==1) return [a[0], ''];
let subsets = subseq(a.substr(0, a.length-1));
let n = subsets.length;
for (let i=0; i < n; i++) {
subsets.push(subsets[i]+a[a.length-1]);
}
return subsets;
};
Obviously, this code is not efficient, as it generates each of $2^n$ subsets of the string s
and checks each one for equality against t
.
I would much appreciate it if someone could explain how to improve this solution using memoization. I am aware that there is a dynamic programming solution. I am more interested in learning how to extend this easy-to-understand solution into something more efficient by using a memo table. Any and all help would be graciously appreciated.
javascript recursion dynamic-programming memoization
bumped to the homepage by Community♦ 13 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
It seems more like stackoverflow question...
– Dmitry Yaremenko
Feb 13 at 7:34
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I wrote the following code to solve this leetcode problem:
var numDistinct = function(s, t) {
if (!s.length) return +(!t.length);
let res = 0;
for (let seq of subseq(s)) res += +(t===seq);
return res;
};
var subseq = function(a) {
if (a.length==1) return [a[0], ''];
let subsets = subseq(a.substr(0, a.length-1));
let n = subsets.length;
for (let i=0; i < n; i++) {
subsets.push(subsets[i]+a[a.length-1]);
}
return subsets;
};
Obviously, this code is not efficient, as it generates each of $2^n$ subsets of the string s
and checks each one for equality against t
.
I would much appreciate it if someone could explain how to improve this solution using memoization. I am aware that there is a dynamic programming solution. I am more interested in learning how to extend this easy-to-understand solution into something more efficient by using a memo table. Any and all help would be graciously appreciated.
javascript recursion dynamic-programming memoization
I wrote the following code to solve this leetcode problem:
var numDistinct = function(s, t) {
if (!s.length) return +(!t.length);
let res = 0;
for (let seq of subseq(s)) res += +(t===seq);
return res;
};
var subseq = function(a) {
if (a.length==1) return [a[0], ''];
let subsets = subseq(a.substr(0, a.length-1));
let n = subsets.length;
for (let i=0; i < n; i++) {
subsets.push(subsets[i]+a[a.length-1]);
}
return subsets;
};
Obviously, this code is not efficient, as it generates each of $2^n$ subsets of the string s
and checks each one for equality against t
.
I would much appreciate it if someone could explain how to improve this solution using memoization. I am aware that there is a dynamic programming solution. I am more interested in learning how to extend this easy-to-understand solution into something more efficient by using a memo table. Any and all help would be graciously appreciated.
javascript recursion dynamic-programming memoization
javascript recursion dynamic-programming memoization
edited Feb 13 at 5:54
Martin R
15.4k12264
15.4k12264
asked Feb 13 at 4:23
Fahd Ahmed
61
61
bumped to the homepage by Community♦ 13 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 13 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
It seems more like stackoverflow question...
– Dmitry Yaremenko
Feb 13 at 7:34
add a comment |
It seems more like stackoverflow question...
– Dmitry Yaremenko
Feb 13 at 7:34
It seems more like stackoverflow question...
– Dmitry Yaremenko
Feb 13 at 7:34
It seems more like stackoverflow question...
– Dmitry Yaremenko
Feb 13 at 7:34
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
I'd just like to point out, that the code IMHO has far too many "length optimizations". Just take the first line:
if (!s.length) return +(!t.length);
I've been a JS developer for over 20 years and I couldn't say off the top of my head what +
does to a boolean. It would be much more readable as:
if (s.length === 0) {
return (t.length === 0) ? 1 : 0;
}
Also a comment why these are the correct return values would be very helpful.
Thanks for the feedback. Normally, for these 'competitive programming' sites I like to keep the code as short as possible as others submit their code this way and in many cases it's made it quicker to understand a solution. In this case, I agree it may obscure the intention of the code. The reasoning is that only one subset of the empty string matches empty string (the set including the empty string), so return 1 in that scenario. As you can probably tell, it's 'that one annoying edge case'.
– Fahd Ahmed
Feb 13 at 12:53
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I'd just like to point out, that the code IMHO has far too many "length optimizations". Just take the first line:
if (!s.length) return +(!t.length);
I've been a JS developer for over 20 years and I couldn't say off the top of my head what +
does to a boolean. It would be much more readable as:
if (s.length === 0) {
return (t.length === 0) ? 1 : 0;
}
Also a comment why these are the correct return values would be very helpful.
Thanks for the feedback. Normally, for these 'competitive programming' sites I like to keep the code as short as possible as others submit their code this way and in many cases it's made it quicker to understand a solution. In this case, I agree it may obscure the intention of the code. The reasoning is that only one subset of the empty string matches empty string (the set including the empty string), so return 1 in that scenario. As you can probably tell, it's 'that one annoying edge case'.
– Fahd Ahmed
Feb 13 at 12:53
add a comment |
up vote
0
down vote
I'd just like to point out, that the code IMHO has far too many "length optimizations". Just take the first line:
if (!s.length) return +(!t.length);
I've been a JS developer for over 20 years and I couldn't say off the top of my head what +
does to a boolean. It would be much more readable as:
if (s.length === 0) {
return (t.length === 0) ? 1 : 0;
}
Also a comment why these are the correct return values would be very helpful.
Thanks for the feedback. Normally, for these 'competitive programming' sites I like to keep the code as short as possible as others submit their code this way and in many cases it's made it quicker to understand a solution. In this case, I agree it may obscure the intention of the code. The reasoning is that only one subset of the empty string matches empty string (the set including the empty string), so return 1 in that scenario. As you can probably tell, it's 'that one annoying edge case'.
– Fahd Ahmed
Feb 13 at 12:53
add a comment |
up vote
0
down vote
up vote
0
down vote
I'd just like to point out, that the code IMHO has far too many "length optimizations". Just take the first line:
if (!s.length) return +(!t.length);
I've been a JS developer for over 20 years and I couldn't say off the top of my head what +
does to a boolean. It would be much more readable as:
if (s.length === 0) {
return (t.length === 0) ? 1 : 0;
}
Also a comment why these are the correct return values would be very helpful.
I'd just like to point out, that the code IMHO has far too many "length optimizations". Just take the first line:
if (!s.length) return +(!t.length);
I've been a JS developer for over 20 years and I couldn't say off the top of my head what +
does to a boolean. It would be much more readable as:
if (s.length === 0) {
return (t.length === 0) ? 1 : 0;
}
Also a comment why these are the correct return values would be very helpful.
answered Feb 13 at 9:57
RoToRa
6,1961236
6,1961236
Thanks for the feedback. Normally, for these 'competitive programming' sites I like to keep the code as short as possible as others submit their code this way and in many cases it's made it quicker to understand a solution. In this case, I agree it may obscure the intention of the code. The reasoning is that only one subset of the empty string matches empty string (the set including the empty string), so return 1 in that scenario. As you can probably tell, it's 'that one annoying edge case'.
– Fahd Ahmed
Feb 13 at 12:53
add a comment |
Thanks for the feedback. Normally, for these 'competitive programming' sites I like to keep the code as short as possible as others submit their code this way and in many cases it's made it quicker to understand a solution. In this case, I agree it may obscure the intention of the code. The reasoning is that only one subset of the empty string matches empty string (the set including the empty string), so return 1 in that scenario. As you can probably tell, it's 'that one annoying edge case'.
– Fahd Ahmed
Feb 13 at 12:53
Thanks for the feedback. Normally, for these 'competitive programming' sites I like to keep the code as short as possible as others submit their code this way and in many cases it's made it quicker to understand a solution. In this case, I agree it may obscure the intention of the code. The reasoning is that only one subset of the empty string matches empty string (the set including the empty string), so return 1 in that scenario. As you can probably tell, it's 'that one annoying edge case'.
– Fahd Ahmed
Feb 13 at 12:53
Thanks for the feedback. Normally, for these 'competitive programming' sites I like to keep the code as short as possible as others submit their code this way and in many cases it's made it quicker to understand a solution. In this case, I agree it may obscure the intention of the code. The reasoning is that only one subset of the empty string matches empty string (the set including the empty string), so return 1 in that scenario. As you can probably tell, it's 'that one annoying edge case'.
– Fahd Ahmed
Feb 13 at 12:53
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f187437%2fcheck-the-equality-for-each-subset-of-a-string-s-against-the-target-t-and-return%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It seems more like stackoverflow question...
– Dmitry Yaremenko
Feb 13 at 7:34