Skip to content

Commit 56862c8

Browse files
committed
Practice 11-Jun-2020
1 parent ec36d4a commit 56862c8

8 files changed

+518
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
2+
3+
You may assume no duplicates in the array.
4+
5+
Example 1:
6+
7+
Input: [1,3,5,6], 5
8+
Output: 2
9+
Example 2:
10+
11+
Input: [1,3,5,6], 2
12+
Output: 1
13+
Example 3:
14+
15+
Input: [1,3,5,6], 7
16+
Output: 4
17+
Example 4:
18+
19+
Input: [1,3,5,6], 0
20+
Output: 0
21+
22+
23+
24+
25+
26+
27+
28+
class Solution {
29+
public:
30+
int searchInsert(vector<int>& nums, int target) {
31+
int s=0, e=nums.size()-1, mid;
32+
while(s<=e){
33+
mid=s+(e-s)/2;
34+
if(nums[mid]==target) return mid;
35+
else if(nums[mid]>target) e=mid-1;
36+
else s=mid+1;
37+
}
38+
return s;
39+
}
40+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
2+
3+
You may assume no duplicates in the array.
4+
5+
Example 1:
6+
7+
Input: [1,3,5,6], 5
8+
Output: 2
9+
Example 2:
10+
11+
Input: [1,3,5,6], 2
12+
Output: 1
13+
Example 3:
14+
15+
Input: [1,3,5,6], 7
16+
Output: 4
17+
Example 4:
18+
19+
Input: [1,3,5,6], 0
20+
Output: 0
21+
22+
23+
24+
25+
26+
27+
class Solution {
28+
public:
29+
int searchInsert(vector<int>& nums, int target) {
30+
int s=0, e=nums.size()-1, mid;
31+
while(s<=e){
32+
mid=s+(e-s)/2;
33+
if(nums[mid]==target) return mid;
34+
else if(nums[mid]>target) e=mid-1;
35+
else s=mid+1;
36+
}
37+
return s;
38+
}
39+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
Given two sequences, find the length of longest subsequence present in both of them. Both the strings are of uppercase.
3+
4+
Input:
5+
First line of the input contains no of test cases T,the T test cases follow.
6+
Each test case consist of 2 space separated integers A and B denoting the size of string str1 and str2 respectively
7+
The next two lines contains the 2 string str1 and str2 .
8+
9+
Output:
10+
For each test case print the length of longest common subsequence of the two strings .
11+
12+
Constraints:
13+
1<=T<=200
14+
1<=size(str1),size(str2)<=100
15+
16+
Example:
17+
Input:
18+
2
19+
6 6
20+
ABCDGH
21+
AEDFHR
22+
3 2
23+
ABC
24+
AC
25+
26+
Output:
27+
3
28+
2
29+
30+
Explanation
31+
LCS for input Sequences “ABCDGH” and “AEDFHR” is “ADH” of length 3.
32+
33+
LCS of "ABC" and "AC" is "AC" of length 2
34+
*/
35+
36+
37+
38+
39+
#include<bits/stdc++.h>
40+
using namespace std;
41+
42+
int LCS(string s1, string s2, int n, int m){
43+
int dp[n+1][m+1];
44+
45+
for(int i=0;i<n+1;i++) dp[i][0]=0;
46+
for(int j=0;j<m+1;j++) dp[0][j]=0;
47+
48+
for(int i=1;i<n+1;i++){
49+
for(int j=1;j<m+1;j++){
50+
if(s1[i-1]==s2[j-1])
51+
dp[i][j]=1 + dp[i-1][j-1];
52+
else dp[i][j]=max(dp[i][j-1], dp[i-1][j]);
53+
}
54+
}
55+
return dp[n][m];
56+
}
57+
58+
int main(){
59+
ios_base::sync_with_stdio(false);
60+
cin.tie(NULL);
61+
cout.tie(NULL);
62+
63+
int t;
64+
cin>>t;
65+
while(t--){
66+
int n,m;
67+
cin>>n>>m;
68+
string s1,s2;
69+
cin>>s1>>s2;
70+
cout<<LCS(s1,s2,n,m)<<endl;
71+
}
72+
73+
return 0;
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Given two strings X and Y. The task is to find the length of the longest common substring.
3+
4+
Input:
5+
First line of the input contains number of test cases T. Each test case consist of three lines, first of which contains 2 space separated integers N and M denoting the size of string X and Y strings respectively. The next two lines contains the string X and Y.
6+
7+
Output:
8+
For each test case print the length of longest common substring of the two strings .
9+
10+
Constraints:
11+
1 <= T <= 200
12+
1 <= N, M <= 100
13+
14+
Example:
15+
Input:
16+
2
17+
6 6
18+
ABCDGH
19+
ACDGHR
20+
3 2
21+
ABC
22+
AC
23+
24+
Output:
25+
4
26+
1
27+
28+
Example:
29+
Testcase 1: CDGH is the longest substring present in both of the strings.
30+
*/
31+
32+
33+
34+
35+
#include<bits/stdc++.h>
36+
using namespace std;
37+
38+
int LCSubstring(string x, string y, int m, int n){
39+
int dp[m+1][n+1];
40+
41+
for(int i=0;i<m+1;i++) dp[i][0]=0;
42+
for(int j=0;j<n+1;j++) dp[0][j]=0;
43+
44+
int maxi=0;
45+
46+
for(int i=1;i<m+1;i++){
47+
for(int j=1;j<n+1;j++){
48+
if(x[i-1]==y[j-1])
49+
dp[i][j]=1+dp[i-1][j-1];
50+
else dp[i][j]=0;
51+
maxi=max(maxi, dp[i][j]);
52+
}
53+
}
54+
return maxi;
55+
}
56+
57+
int main(){
58+
ios_base::sync_with_stdio(false);
59+
cin.tie(NULL);
60+
cout.tie(NULL);
61+
int t;
62+
cin>>t;
63+
while(t--){
64+
int m,n;
65+
cin>>m>>n;
66+
string x, y;
67+
cin>>x>>y;
68+
cout<<LCSubstring(x,y,m,n)<<endl;
69+
}
70+
71+
return 0;
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Given a String, find the longest palindromic subsequence
3+
4+
Input:
5+
The first line of input contains an integer T, denoting no of test cases. The only line of each test case consists of a string S(only lowercase)
6+
7+
Output:
8+
Print the Maximum length possible for palindromic subsequence.
9+
10+
Constraints:
11+
1<=T<=100
12+
1<=|Length of String|<=1000
13+
14+
15+
Examples:
16+
Input:
17+
2
18+
bbabcbcab
19+
abbaab
20+
Output:
21+
7
22+
4
23+
*/
24+
25+
26+
27+
28+
#include<bits/stdc++.h>
29+
using namespace std;
30+
31+
int LCS(string s1, string s2, int n, int m){
32+
int dp[n+1][m+1];
33+
34+
for(int i=0;i<n+1;i++) dp[i][0]=0;
35+
for(int j=0;j<m+1;j++) dp[0][j]=0;
36+
37+
for(int i=1;i<n+1;i++){
38+
for(int j=1;j<m+1;j++){
39+
if(s1[i-1]==s2[j-1])
40+
dp[i][j]=1 + dp[i-1][j-1];
41+
else dp[i][j]=max(dp[i][j-1], dp[i-1][j]);
42+
}
43+
}
44+
return dp[n][m];
45+
}
46+
47+
int main(){
48+
ios_base::sync_with_stdio(false);
49+
cin.tie(NULL);
50+
cout.tie(NULL);
51+
52+
int t;
53+
cin>>t;
54+
while(t--){
55+
string s1,s2;
56+
cin>>s1;
57+
int n=s1.length();
58+
for(int i=n-1;i>=0;i--) s2.push_back(s1[i]);
59+
cout<<LCS(s1,s2,n,n)<<endl;
60+
}
61+
62+
return 0;
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
Given two strings ‘str1’ and ‘str2’ of size m and n respectively. The task is to remove/delete and insert minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
3+
4+
Input:
5+
6+
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. The first line of each test case contains integers N and M denoting the length of the strings str1 and str2. Both the strings consist of only small letter alphabets.
7+
8+
The second line of each test case contains the strings str1 and str2.
9+
10+
11+
Output:
12+
13+
Print the total no of insertions and deletions to be done to convert str1 to str2. Output the answer in a new line.
14+
15+
16+
Constraints:
17+
18+
1<= T <=100
19+
20+
1<= N, M <=1000
21+
22+
23+
Example:
24+
25+
Input:
26+
27+
1
28+
29+
4 3
30+
31+
heap pea
32+
33+
Output:
34+
35+
3
36+
*/
37+
38+
39+
40+
#include<bits/stdc++.h>
41+
using namespace std;
42+
43+
int LCS(string s1, string s2, int n, int m){
44+
int dp[n+1][m+1];
45+
46+
for(int i=0;i<n+1;i++) dp[i][0]=0;
47+
for(int j=0;j<m+1;j++) dp[0][j]=0;
48+
49+
for(int i=1;i<n+1;i++){
50+
for(int j=1;j<m+1;j++){
51+
if(s1[i-1]==s2[j-1])
52+
dp[i][j]=1 + dp[i-1][j-1];
53+
else dp[i][j]=max(dp[i][j-1], dp[i-1][j]);
54+
}
55+
}
56+
return dp[n][m];
57+
}
58+
59+
int main(){
60+
ios_base::sync_with_stdio(false);
61+
cin.tie(NULL);
62+
cout.tie(NULL);
63+
64+
int t;
65+
cin>>t;
66+
while(t--){
67+
int n,m;
68+
cin>>n>>m;
69+
string s1,s2;
70+
cin>>s1>>s2;
71+
cout<<n+m-2*LCS(s1,s2,n,m)<<endl;
72+
}
73+
74+
return 0;
75+
}

0 commit comments

Comments
 (0)