Skip to content

Commit 927c48a

Browse files
authored
Create 2707. Extra Characters in a String1 (#593)
2 parents d505d83 + da9f47a commit 927c48a

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

2707. Extra Characters in a String1

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int minExtraChar(string s, vector<string>& d) {
4+
int n = s.size(), m;
5+
vector<int> dp(n + 1);
6+
for (int i{1}; i <= n; ++i) {
7+
dp[i] = dp[i - 1] + 1;
8+
for (const string& word : d) {
9+
m = word.size();
10+
if (i >= m && s.substr(i - m, m) == word) dp[i] = min(dp[i], dp[i - m]);
11+
}
12+
}
13+
return dp[n];
14+
}
15+
};
16+
auto io_opt = [] { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); return 0; }();

0 commit comments

Comments
 (0)