A greedy strategy should work on this issue.
- Find the first letter of a suspicious substring (blr) in a large string (* b * angalore)
- Find the second letter starting with the index of the first letter plus one (anga * l * ore)
- Find the third letter starting with the index of the second letter plus one (o * r * e)
- Continue until you can no longer find the next letter blr in the string (no match), or you have run out of letters in a subsequence (you have a match).
Here is a sample code in C ++:
#include <iostream> #include <string> using namespace std; int main() { string txt = "quick brown fox jumps over the lazy dog"; string s = "brownfoxzdog"; int pos = -1; bool ok = true; for (int i = 0 ; ok && i != s.size() ; i++) { ok = (pos = txt.find(s[i], pos+1)) != string::npos; } cerr << (ok ? "Found" : "Not found") << endl; return 0; }
source share