Longest Common Subsequence
Given two strings, find the longest common subsequence (LCS).
Your code should return the length of LCS.
Have you met this question in a real interview?For "ABCD"
and "EDCA"
, the LCS is "A"
(or "D"
, "C"
), return 1
.
For "ABCD"
and "EACB"
, the LCS is "AC"
, return 2
.
class Solution {
public:
/**
* @param A, B: Two strings.
* @return: The length of longest common subsequence of A and B.
*/
int longestCommonSubsequence(string A, string B) {
// write your code here
int m=A.size();
int n=B.size();
vector<vector<int>> ma(m+1,vector<int>(n+1,0));
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
if(A[i-1]==B[j-1]){
ma[i][j]=ma[i-1][j-1]+1;
}
else{
ma[i][j]=max(ma[i][j-1],ma[i-1][j]);
}
}
}
return ma[m][n];
}
};
Longest Increasing Subsequence
Given a sequence of integers, find the longest increasing subsequence (LIS).
You code should return the length of the LIS.
For [5, 4, 1, 2, 3], the LIS is [1, 2, 3], return 3
For [4, 2, 4, 5, 3, 7], the LIS is [4, 4, 5, 7], return 4
Time complexity O(n^2) or O(nlogn)
class Solution {
public:
int longestCommonSubsequence(vector<int>& A, vector<int>& B) {
// write your code here
int m=A.size();
int n=B.size();
vector<vector<int>> ma(m+1,vector<int>(n+1,0));
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
if(A[i-1]==B[j-1]){
ma[i][j]=ma[i-1][j-1]+1;
}
else{
ma[i][j]=max(ma[i][j-1],ma[i-1][j]);
}
}
}
return ma[m][n];
}
/**
* @param nums: The integer array
* @return: The length of LIS (longest increasing subsequence)
*/
int longestIncreasingSubsequence(vector<int> nums) {
// write your code here
// vector<int>num2(nums.begin(),nums.end());
//sort(num2.begin(),num2.end());
//return longestCommonSubsequence(nums,num2);
int size=nums.size();
vector<int> v(size,INT_MAX);
int cnum,len=0;
for(int i=0;i<size;i++){
cnum=nums[i];
//upper_bound return the first element bigger than the value
//in the 1,1,1,1 case it can work
auto it=upper_bound(v.begin(),v.begin()+len+1,cnum);
*it=cnum;
len=max(len,(int)(it-v.begin())+1);
}
return len;
}
};
Longest Common Prefix
Given k strings, find the longest common prefix ( LCP ).
Longest Common Substring
Given two strings, find the longest common substring.
Return the length of it.
class Solution {
public:
/**
* @param A, B: Two string.
* @return: the length of the longest common substring.
*/
int longestCommonSubstring(string &A, string &B) {
// write your code here
// write your code here
int m=A.size();
int n=B.size();
vector<vector<int>> ma(m+1,vector<int>(n+1,0));
int mmx=0;
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
if(A[i-1]==B[j-1]){
ma[i][j]=ma[i-1][j-1]+1;
}
mmx=max(mmx,ma[i][j]);
}
}
return mmx;
}
};
Longest Increasing Continuous subsequence
Give you an integer array (index from 0 to n-1, where n is the size of this array),find the longest increasing continuous subsequence in this array. (The definition of the longest increasing continuous subsequence here can be from right to left or from left to right)
Example
For [5, 4, 2, 1, 3]
, the LICS is [5, 4, 2, 1]
, return 4
.
For [5, 1, 2, 3, 4]
, the LICS is [1, 2, 3, 4]
, return 4
.
Longest Palindromic Substring
Given a stringS
, find the longest palindromic substring in
S
. You may assume that the maximum length of
S
is 1000, and there exists one unique longest palindromic substring.
Longest Increasing Continuous subsequence II
Example
Given a matrix:
[
[1 ,2 ,3 ,4 ,5],
[16,17,24,23,6],
[15,18,25,22,7],
[14,19,20,21,8],
[13,12,11,10,9]
]
return 25