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.










share|improve this question
















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















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.










share|improve this question
















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













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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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










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.






share|improve this answer





















  • 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











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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.






share|improve this answer





















  • 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















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.






share|improve this answer





















  • 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













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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Quarter-circle Tiles

build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

Mont Emei