-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1055.cpp
More file actions
28 lines (28 loc) · 723 Bytes
/
1055.cpp
File metadata and controls
28 lines (28 loc) · 723 Bytes
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
class Solution {
public:
int shortestWay(string source, string target) {
int m = source.size();
int n = target.size();
int sourceIdx = 0;
int targetIdx = 0;
int penalty = 0;
int count = 1;
while (targetIdx < n) {
if (source[sourceIdx] == target[targetIdx]) {
sourceIdx++;
targetIdx++;
penalty = 0;
}
else {
sourceIdx++;
penalty++;
}
if (penalty == m) return -1;
if (sourceIdx == m && targetIdx < n) {
sourceIdx = 0;
count++;
}
}
return count;
}
};