-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2707-Extra_Characters_in_a_String.cpp
More file actions
110 lines (101 loc) · 2.78 KB
/
2707-Extra_Characters_in_a_String.cpp
File metadata and controls
110 lines (101 loc) · 2.78 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*******************************************************************************
* 2707-Extra_Characters_in_a_String.cpp
* Billy.Ljm
* 02 September 2023
*
* =======
* Problem
* =======
* https://leetcode.com/problems/extra-characters-in-a-string/
*
* 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.
*
* ===========
* My Approach
* ===========
* We can use std::string::find to find the first instance of each substring
* in the dictionary. Then we just count the number of preceding characters
* and recursively repeat the process for the subsequent strings. To make it
* more efficient, we can memoise it.
*
* This has a time complexity of O(n*m), and a space complexity of O(n), where
* n length of the string and m is the length of the dictionary.
******************************************************************************/
#include <iostream>
#include <vector>
#include <map>
using namespace std;
/**
* << operator for vectors
*/
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
os << "[";
for (const auto elem : v) {
os << elem << ",";
}
if (v.size() > 0) os << "\b";
os << "]";
return os;
}
/**
* Solution
*/
class Solution {
public:
int minExtraChar(string s, vector<string>& dictionary) {
map<int, int> memo;
return recurse(0, s, dictionary, memo);
}
int recurse(int start, string& s, vector<string>& dictionary,
map<int, int>& memo) {
// base case, string searched
if (start >= s.size()) {
return 0;
}
// if memoised, return
else if (memo.find(start) != memo.end()) {
return memo[start];
}
// else recurse
else {
int out = s.size() - start;
int idx; // output of string::find
for (string substr : dictionary) {
idx = s.find(substr, start);
if (idx >= 0) {
cout << start << substr << idx << endl;
out = min(out, idx - start +
recurse(idx + substr.size(), s, dictionary, memo));
}
}
memo[start] = out;
return memo[start];
}
}
};
/**
* Test cases
*/
int main(void) {
Solution sol;
string s;
vector<string> dictionary;
// test case 1
s = "leetscode";
dictionary = { "leet","code","leetcode" };
std::cout << "minExtraChar(" << s << "," << dictionary << ") = ";
std::cout << sol.minExtraChar(s, dictionary) << std::endl;
// test case 2
s = "sayhelloworld";
dictionary = { "hello","world" };
std::cout << "minExtraChar(" << s << "," << dictionary << ") = ";
std::cout << sol.minExtraChar(s, dictionary) << std::endl;
return 0;
}