leetcode 1055 - Shortest Way to Form String

https://leetcode.com/problems/shortest-way-to-form-string/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public:
int shortestWay(string source, string target) {
int start = 0;
int n = 0;

while (start < target.size()) {
int next = get_next(source, target, start);
if (next == start)
return -1;
start = next;
n++;
}

return n;
}

int get_next(string &source, string &target, int start) {
for (int i = 0; i < source.size(); i++) {
if (source[i] == target[start])
start++;

if (start == target.size())
break;
}

return start;
}
};