-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathExtra Characters in a String.cpp
39 lines (27 loc) · 1.09 KB
/
Extra Characters in a String.cpp
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
30
31
32
33
34
35
36
37
38
39
/*
Solution by Rahul Surana
***********************************************************
You are given a 0-indexed string s and a dictionary of words dictionary.
You have to break s into one or more non-overlapping substrings such that each substring is present in dictionary.
There may be some extra characters in s which are not present in any of the substrings.
Return the minimum number of extra characters left over if you break up s optimally.
***********************************************************
*/
class Solution {
public:
int minExtraChar(string s, vector<string>& dictionary) {
set<string> ss(dictionary.begin(),dictionary.end());
int ans = 0, n = s.length();
vector<int> dp(n+1,n+1);
dp[0] = 0;
for(int i = 1 ; i <= n; i++){
dp[i] = dp[i-1]+1;
for(int j = 1; j <= i; j++){
if(ss.count(s.substr(i-j,j)))
dp[i] = min(dp[i], dp[i-j]);
}
// cout << i <<" "<< ans <<"\n";
}
return dp[n];
}
};