Skip to content

Commit aab839b

Browse files
committed
questions added
1 parent 42b5e17 commit aab839b

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

C++/Subsets ||.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*Problem url : https://leetcode.com/problems/subsets-ii/ */
2+
3+
// Given an integer array nums that may contain duplicates, return all possible subsets (the power set).
4+
// The solution set must not contain duplicate subsets. Return the solution in any order.
5+
6+
7+
8+
class Solution {
9+
public:
10+
set<vector<int>> stt;
11+
void print(int index, vector<int> &v, vector<vector<int>> &ans, vector<int> &nums,int n)
12+
{
13+
if(index == n)
14+
{
15+
if(stt.find(v)==stt.end())
16+
{
17+
ans.push_back(v);
18+
stt.insert(v);
19+
}
20+
21+
return;
22+
}
23+
24+
print(index+1,v,ans,nums,n);
25+
26+
v.push_back(nums[index]);
27+
print(index+1,v,ans,nums,n);
28+
v.pop_back();
29+
}
30+
31+
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
32+
sort(nums.begin(),nums.end());
33+
vector<vector<int>> ans;
34+
vector<int> v;
35+
int n = nums.size();
36+
print(0,v,ans,nums,n);
37+
38+
return ans;
39+
}
40+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// <!-- Leetcode
2+
3+
// 1312. Minimum Insertion Steps to Make a String Palindrome
4+
5+
// Given a string s. In one step you can insert any character at any index of the string.
6+
7+
// Return the minimum number of steps to make s palindrome.
8+
9+
// A Palindrome String is one that reads the same backward as well as forward. -->
10+
11+
12+
13+
class Solution {
14+
public:
15+
int longestCommonSubsequence(string text1, string text2) {
16+
int n1 = text1.size();
17+
int n2 = text2.size();
18+
// vector<vector<int>> dp(n1+1,vector<int>(n2+1,0));
19+
vector<int> prev(n2+1,0), cur(n2+1,0);
20+
21+
for(int i=1;i<=n1;i++)
22+
{
23+
for(int j=1;j<=n2;j++)
24+
{
25+
if(text1[i-1] == text2[j-1])
26+
{
27+
cur[j] = 1 + prev[j-1];
28+
}
29+
30+
else
31+
{
32+
cur[j] = max(prev[j] , cur[j-1]);
33+
}
34+
}
35+
prev = cur;
36+
}
37+
38+
return prev[n2];
39+
}
40+
41+
string reverseStr(string str)
42+
{
43+
int n = str.length();
44+
45+
// Swap character starting from two
46+
// corners
47+
for (int i = 0; i < n / 2; i++)
48+
swap(str[i], str[n - i - 1]);
49+
50+
return str;
51+
}
52+
53+
int longestPalindromeSubseq(string s) {
54+
string t = reverseStr(s);
55+
return longestCommonSubsequence(s,t);
56+
}
57+
58+
int minInsertions(string s) {
59+
int n = s.size();
60+
return n - longestPalindromeSubseq(s);
61+
}
62+
};

0 commit comments

Comments
 (0)