Skip to content

Commit d509ed3

Browse files
authored
Update find-and-replace-in-string.cpp
1 parent 31aafd3 commit d509ed3

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

C++/find-and-replace-in-string.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ class Solution {
1111
}
1212
}
1313
string result;
14-
int i = 0, last = 0;
15-
for (i = 0; i < S.length(); ++i) {
14+
int last = 0;
15+
for (int i = 0; i < S.length(); ++i) {
1616
if (bucket[i].first) {
17-
result += S.substr(last, i - last);
1817
result += bucket[i].second;
1918
last = i + bucket[i].first;
19+
} else if (i >= last) {
20+
result.push_back(S[i]);
2021
}
2122
}
22-
result += S.substr(last, i - last);
2323
return result;
2424
}
2525
};

0 commit comments

Comments
 (0)