Created
October 25, 2019 20:07
-
-
Save adamkorg/5cee9720741ec9cbe9f83ba2391f99b8 to your computer and use it in GitHub Desktop.
Leetcode 115: Distinct Subsequences (recursive)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It is a best solution found that very popular and helpful
https://www.youtube.com/watch?v=6ngJWBA-nZk&ab_channel=EricProgramming