-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ00_Preview.cpp
More file actions
258 lines (217 loc) · 6.57 KB
/
Copy pathJ00_Preview.cpp
File metadata and controls
258 lines (217 loc) · 6.57 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/******************** Preview ********************/
// Longest Palindrome, LeetCode 409
class Solution {
public:
int longestPalindrome(string s) {
if (s.empty()) return 0;
unordered_set<char> charSet;
for (int i = 0; i < s.size(); i++) {
if (charSet.count(s[i]))
charSet.erase(s[i]);
else charSet.insert(s[i]);
}
int oddAppr = charSet.size();
if (oddAppr > 0) oddAppr -= 1;
return s.size() - oddAppr;
}
};
// Implement strStr(), LeetCode 28
class Solution {
public:
int strStr(string haystack, string needle) {
if (needle.empty()) return 0;
if (haystack.empty()) return -1;
int n = haystack.size() - needle.size() + 1;
for (int i = 0; i < n; i++) {
int j = 0;
for (j = 0; j < needle.size(); j++) {
if (haystack[i + j] != needle[j])
break;
}
if (j == needle.size()) return i;
}
return -1;
}
};
// Implement strStr(), Rabin Karp Algorithm, LeetCode 28
class Solution {
public:
int strStr(string haystack, string needle) {
if (needle.empty()) return 0;
if (haystack.empty()) return -1;
const int BASE = 1000000;
int m = needle.size();
// 31 ^ m
int power = 1;
for (int i = 0; i < m; i++)
power = (power * 31) % BASE;
int needleCode = 0;
for (int i = 0; i < m; i++)
needleCode = (needleCode * 31 + needle[i]) % BASE;
int hashCode = 0;
for (int i = 0; i < haystack.size(); i++) {
// abc + d
hashCode = (hashCode * 31 + haystack[i]) % BASE;
if (i < m - 1) continue;
// abcd - a
if (i >= m) {
hashCode -= (haystack[i - m] * power) % BASE;
if (hashCode < 0) hashCode += BASE;
}
// double check the string
if (hashCode == needleCode) {
if (haystack.substr(i - m + 1, m) == needle)
return i - m + 1;
}
}
return -1;
}
};
// Valid Palindrome, Two Pointers, LeetCode 125
class Solution {
public:
/**
* @param s: A string
* @return: Whether the string is a valid palindrome
*/
bool isPalindrome(string &s) {
if (s.empty()) {
return true;
}
int left = 0, right = s.size() - 1;
while (left < right) {
while (left < s.size() && !isalnum(s[left])) {
left++;
}
while (right >= 0 && !isalnum(s[right])) {
right--;
}
if (tolower(s[left]) != tolower(s[right])) {
break;
} else {
left++;
right--;
}
}
return left >= right;
}
};
// Longest Palindromic Substring, Center Enumeration, LeetCode 5
class Solution {
public:
string longestPalindrome(string s) {
if (s.size() <= 1) return s;
int start = 0, len = 0, longest = 0;
for (int i = 0; i < s.size(); i++) {
len = _longestPalindrome(s, i, i);
if (len > longest) {
longest = len;
start = i - len / 2;
}
len = _longestPalindrome(s, i, i + 1);
if (len > longest) {
longest = len;
start = i - len / 2 + 1;
}
}
return s.substr(start, longest);
}
private:
int _longestPalindrome(string &s, int left, int right) {
int len = 0;
while (left >= 0 && right < s.size()) {
if (s[left] != s[right]) break;
len += (left == right ? 1 : 2);
left--;
right++;
}
return len;
}
};
// Longest Palindromic Substring, Center Enumeration, LeetCode 5
class Solution {
public:
string longestPalindrome(string s) {
if (s.size() <= 1) return s;
int n = s.size();
vector<vector<bool>> opt(n, vector<bool>(n, false));
int longest = 1, start = 0;
for (int i = 0; i < n; i++)
opt[i][i] = true;
// use upper triangle
for (int i = 0; i < n - 1; i++) {
opt[i][i + 1] = s[i] == s[i + 1];
if (opt[i][i + 1]) {
start = i;
longest = 2;
}
}
// must start from bottom to top
for (int i = n - 3; i >= 0; i--) {
for (int j = i + 2; j < n; j++) {
opt[i][j] = opt[i + 1][j - 1] && s[i] == s[j];
if (opt[i][j] && j - i + 1 > longest) {
start = i;
longest = j - i + 1;
}
}
}
return s.substr(start, longest);
}
};
// Longest Palindrome Subsequence, Dynamic Programming, LeetCode 516
class Solution {
public:
int longestPalindromeSubseq(string s) {
if (s.empty()) return 0;
if (s.size() == 1) return 1;
int n = s.size();
vector<vector<int>> dp(n, vector<int>(n, 0));
for (int i = 0; i < n; i++)
dp[i][i] = 1;
for (int i = n - 2; i >= 0; i--) {
for (int j = i + 1; j < n; j++) {
if (s[i] == s[j])
dp[i][j] = dp[i + 1][j - 1] + 2;
else
dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]);
}
}
return dp[0][n - 1];
}
};
// Valid Palindrome II, Two Pointers, LeetCode 680
class Solution {
public:
/**
* @param s: a string
* @return: nothing
*/
bool validPalindrome(string &s) {
if (s.size() <= 2) return true;
int left = 0, right = s.size() - 1;
while (left < right) {
if (s[left] != s[right]) {
break;
}
left++;
right--;
}
if (left >= right) {
return true;
}
return _isSubPalindrome(s, left + 1, right) ||
_isSubPalindrome(s, left, right - 1);
}
private:
bool _isSubPalindrome(string &s, int left, int right) {
while (left < right) {
if (s[left] != s[right]) {
return false;
}
left++;
right--;
}
return true;
}
};