classSolution { public: intshortestWay(string source, string target){ int m = source.length(), n = target.length(); vector<vector<int>> f(m, vector<int>(26, -1)); f[m - 1][source[m - 1] - 'a'] = m - 1; for (int i = m - 2; i >= 0; --i) { f[i] = f[i + 1]; // 先copy后一个index的内容 f[i][source[i] - 'a'] = i; // 再修改当前index所对应的字符的下标 } int res = 1, i = 0; // res要从1开始因为target最后一个字符结束以后没法更新res for (char c : target) { if (f[0][c - 'a'] == -1) return-1; // 如果source所有字符里都不包含c if (i == m || f[i][c - 'a'] == -1) { // 如果source所有字符都比对完或者尚未比对完但是source当前位置之后没有c(之前可能有)则重置指针并更新res i = 0; ++res; } i = f[i][c - 'a'] + 1; // 移动指针到下一个可能的位置(跳过中间不符合要求的字符) } return res; } };
greedy O(m*n) time worst case比如[“abcd”, “ddddd”]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { public: intshortestWay(string source, string target){ int m = size(source), n = size(target); int res = 0; for (int i = 0; i < n;) { int t = i; for (int j = 0; j < m; ++j) { if (i < n && source[j] == target[i]) { ++i; } } if (i == t) return-1; ++res; } return res; } };