Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int shortestWay(string source, string target) {
- int slen = source.length(), tlen = target.length();
- int ans=0, i=0, pos=-1;
- while(i<tlen && ans<=i){
- pos = source.find(target[i], pos+1);
- if(pos == string::npos){
- ans++;
- pos = source.find(target[i]);
- if(pos == string::npos) return -1;
- }
- i++;
- }
- return ans+1;
- }
- };
- ---------------------
- class Solution {
- public:
- int shortestWay(string s, string t, int res = 0) {
- vector<int> ch_ids[26] = {};
- for (auto j = 0; j < s.size(); ++j) ch_ids[s[j] - 'a'].push_back(j);
- for (auto i = 0, j = -1; i < t.size(); ) {
- auto &ids = ch_ids[t[i] - 'a'];
- if (ids.empty()) return -1;
- auto it = upper_bound(begin(ids), end(ids), j);
- if(it == end(ids)) ++res, j = -1;
- else ++i, j = *it;
- }
- return res + 1;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement