132 Palindrome Partitioning II
// ----------------Seeing Discuss-----------
/*
copying codes
url
https://discuss.leetcode.com/topic/2840/my-solution-does-not-need-a-table-for-palindrome-is-it-right-it-uses-only-o-n-space
Analysis:
分成奇数和偶数分别对待。
*/
class Solution {
public:
int minCut(string s)
{
int n = s.size();
vector<int> cut(n+1,0);
for(int i=0;i<=n;i++) cut[i]=i-1;
for(int i=0;i<n;i++)
{
for(int j=0;i-j>=0 && i+j<n &&s[i-j]==s[i+j];j++)
cut[i+j+1] = min(cut[i+j+1],1+cut[i-j]);
for(int j=1;i-j+1>=0 && i+j<n && s[i-j+1]==s[i+j];j++)
cut[i+j+1] = min(cut[i+j+1],1+cut[i-j+1]);
}
return cut[n];
}
};
// ----------------Seeing Discuss-----------
/*
copying codes
url
https://discuss.leetcode.com/topic/4189/share-my-concise-c-solution-less-than-20-lines
Analysis:
*/
class Solution {
public:
vector<string> fullJustify(vector<string> &words, int L) {
vector<string> res;
for(int i = 0, k, l; i < words.size(); i += k) {
for(k = l = 0; i + k < words.size() and l + words[i+k].size() <= L - k; k++) {
l += words[i+k].size();
}
string tmp = words[i];
for(int j = 0; j < k - 1; j++) {
if(i + k >= words.size()) tmp += " ";
else tmp += string((L - l) / (k - 1) + (j < (L - l) % (k - 1)), ' ');
tmp += words[i+j+1];
}
tmp += string(L - tmp.size(), ' ');
res.push_back(tmp);
}
return res;
}
};
71 Simplify Path
// ---------------Seeing Discuss------------
/*
copying codes
看不懂例子是如何变换的。。。
Because '.' means 'right here in the current directory'. It means no change.
只有一个点,就是还是在当前目录的意思。
*/
class Solution {
public:
string simplifyPath(string path)
{
string result = "",token;
stringstream ss(path);
vector<string> tokens;
while(getline(ss,token,'/'))
{
if (token=="." || token=="") continue;
else if (token=="..")
{
if (tokens.size()!=0) tokens.pop_back();
}
else tokens.push_back(token);
}
if(tokens.size()==0) return "/";
for(int i=0;i<tokens.size();i++)
result = result +'/'+tokens[i];
return result;
}
};
97 Interleaving String
// -------------Seeing Discuss --------------
/*
copying codes
明明这样的代码自己是写的出来的。。这么简单的dp,我居然都写不出来,弱爆了!
url
https://discuss.leetcode.com/topic/3532/my-dp-solution-in-c/2
DP table represents if s3 is interleaving at (i+j)th position when s1 is at ith position,
and s2 is at jth position. 0th position means empty string.
So if both s1 and s2 is currently empty, s3 is empty too, and it is considered interleaving.
If only s1 is empty, then if previous s2 position is interleaving and current s2 position char
is equal to s3 current position char, it is considered interleaving. similar idea applies to
when s2 is empty. when both s1 and s2 is not empty, then if we arrive i, j from i-1, j, then
if i-1,j is already interleaving and i and current s3 position equal, it s interleaving.
If we arrive i,j from i, j-1, then if i, j-1 is already interleaving and j and current
s3 position equal. it is interleaving.
*/
class Solution {
public:
bool isInterleave(string s1, string s2, string s3)
{
if (s3.length() != s1.length()+s2.length()) return false;
bool table[s1.length()+1][s2.length()+1];
for(int i=0;i<s1.length()+1;i++)
for(int j=0;j<s2.length()+1;j++)
{
if (i==0 && j==0) table[i][j]=true;//[0][0]时额外处理
else if (i==0)
table[i][j] = (table[i][j-1] && s2[j-1]==s3[i+j-1]);// 当s1空时,比较s2和s3
else if (j==0)
table[i][j]= (table[i-1][j] && s1[i-1]==s3[i+j-1]); // 当s2空时,比较s1和s3
else table[i][j] =(table[i-1][j] && s1[i-1]==s3[i+j-1]) ||(table[i][j-1] && s2[j-1]==s3[i+j-1]);
// 两种情况: 用s1和s3匹配;用s2和s3匹配
}
return table[s1.length()][s2.length()];
}
};
// ------------------------------ TLE --------------------
/*
99 / 101 test cases passed.
TLE points:
"bbbbbabbbbabaababaaaabbababbaaabbabbaaabaaaaababbbababbbbbabbbbababbabaabababbbaabababababbbaaababaa"
"babaaaabbababbbabbbbaabaabbaabbbbaabaaabaababaaaabaaabbaaabaaaabaabaabbbbbbbbbbbabaaabbababbabbabaab"
"babbbabbbaaabbababbbbababaabbabaabaaabbbbabbbaaabbbaaaaabbbbaabbaaabababbaaaaaabababbababaababbababbbababbbbaaaabaabbabbaaaaabbabbaaaabbbaabaaabaababaababbaaabbbbbabbbbaabbabaabbbbabaaabbababbabbabbab"
Code:
class Solution {
public:
bool flag;
bool isInterleave(string s1, string s2, string s3)
{
int l1=s1.size(),l2=s2.size(),l3=s3.size();
if (l1+l2!=l3) return false;
flag = false;
broadSearch(s1,s2,s3);
return flag;
}
void broadSearch(string s1,string s2,string s3)
{
if (flag) return ;
if (s1=="" && s2=="" && s3=="")
{
flag = true;
return;
}
if (s1.size()!=0 && s1[0]==s3[0])
{
broadSearch(s1.substr(1),s2,s3.substr(1));
}
if (s2.size()!=0 && s2[0]==s3[0])
{
broadSearch(s1,s2.substr(1),s3.substr(1));
}
return ;
}
};
*/
10 Regular Expression Matching
// ----------------Seeing Discuss-----------
/*
copying codes
url
https://discuss.leetcode.com/topic/6183/my-concise-recursive-and-dp-solutions-with-full-explanation-in-c
*/
//方法一:
class Solution {
public:
bool isMatch(string s, string p) {
if (p.empty()) return s.empty();
if ('*' == p[1])
// x* matches empty string or at least one character: x* -> xx*
// *s is to ensure s is non-empty
return (isMatch(s, p.substr(2)) || !s.empty() && (s[0] == p[0] || '.' == p[0]) && isMatch(s.substr(1), p));
else
return !s.empty() && (s[0] == p[0] || '.' == p[0]) && isMatch(s.substr(1), p.substr(1));
}
};
//方法二
class Solution {
public:
bool isMatch(string s, string p) {
/**
* f[i][j]: if s[0..i-1] matches p[0..j-1]
* if p[j - 1] != '*'
* f[i][j] = f[i - 1][j - 1] && s[i - 1] == p[j - 1]
* if p[j - 1] == '*', denote p[j - 2] with x
* f[i][j] is true iff any of the following is true
* 1) "x*" repeats 0 time and matches empty: f[i][j - 2]
* 2) "x*" repeats >= 1 times and matches "x*x": s[i - 1] == x && f[i - 1][j]
* '.' matches any single character
*/
int m = s.size(), n = p.size();
vector<vector<bool>> f(m + 1, vector<bool>(n + 1, false));
f[0][0] = true;
for (int i = 1; i <= m; i++)
f[i][0] = false;
// p[0.., j - 3, j - 2, j - 1] matches empty iff p[j - 1] is '*' and p[0..j - 3] matches empty
for (int j = 1; j <= n; j++)
f[0][j] = j > 1 && '*' == p[j - 1] && f[0][j - 2];
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
if (p[j - 1] != '*')
f[i][j] = f[i - 1][j - 1] && (s[i - 1] == p[j - 1] || '.' == p[j - 1]);
else
// p[0] cannot be '*' so no need to check "j > 1" here
f[i][j] = f[i][j - 2] || (s[i - 1] == p[j - 2] || '.' == p[j - 2]) && f[i - 1][j];
return f[m][n];
}
};
44 Wildcard Matching
// ----------------Seeing Discuss-----------
/*
copying codes
url
https://discuss.leetcode.com/topic/17901/accepted-c-dp-solution-with-a-trick
Analysis:
*/
class Solution {
public:
bool isMatch(string s, string p) {
int m = s.length(), n = p.length();
if (n > 30000) return false; // the trick
vector<bool> cur(m + 1, false);
cur[0] = true;
for (int j = 1; j <= n; j++) {
bool pre = cur[0]; // use the value before update
cur[0] = cur[0] && p[j - 1] == '*';
for (int i = 1; i <= m; i++) {
bool temp = cur[i]; // record the value before update
if (p[j - 1] != '*')
cur[i] = pre && (s[i - 1] == p[j - 1] || p[j - 1] == '?');
else cur[i] = cur[i - 1] || cur[i];
pre = temp;
}
}
return cur[m];
}
};