Skip to content

Instantly share code, notes, and snippets.

@adamkorg
Created October 25, 2019 20:07
Show Gist options
  • Save adamkorg/5cee9720741ec9cbe9f83ba2391f99b8 to your computer and use it in GitHub Desktop.
Save adamkorg/5cee9720741ec9cbe9f83ba2391f99b8 to your computer and use it in GitHub Desktop.
Leetcode 115: Distinct Subsequences (recursive)
#include <iostream>
#include <string>
using namespace std;
void match(const string& s, const string& t, int idxs, int idxt, int& matches) {
if (idxt == t.size()) {
matches++;
return;
}
if (idxs == s.size()) return;
for (int i=idxs; i<s.size(); ++i) {
if (s[i] == t[idxt]) match(s, t, i+1, idxt+1, matches);
}
}
int numDistinct(string s, string t) {
if (s=="" || t=="") return 0;
int matches = 0;
match(s, t, 0, 0, matches);
return matches;
}
int main() {
//string s="babgbag"; //"rabbbit";
//string t="bag"; //"rabbit";
string s="adbdadeecadeadeccaeaabdabdbcdabddddabcaaadbabaaedeeddeaeebcdeabcaaaeeaeeabcddcebddebeebedaecccbdcbcedbdaeaedcdebeecdaaedaacadbdccabddaddacdddc";
string t="bcddceeeebecbc";
cout << numDistinct(s, t) << "\n";
return 0;
}
@QureshiAbraham
Copy link

It is a best solution found that very popular and helpful
https://www.youtube.com/watch?v=6ngJWBA-nZk&ab_channel=EricProgramming

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment