diff --git a/C++/Subsets ||.cpp b/C++/Subsets ||.cpp new file mode 100644 index 00000000..a560331b --- /dev/null +++ b/C++/Subsets ||.cpp @@ -0,0 +1,40 @@ +/*Problem url : https://leetcode.com/problems/subsets-ii/ */ + +// Given an integer array nums that may contain duplicates, return all possible subsets (the power set). +// The solution set must not contain duplicate subsets. Return the solution in any order. + + + +class Solution { +public: + set> stt; + void print(int index, vector &v, vector> &ans, vector &nums,int n) + { + if(index == n) + { + if(stt.find(v)==stt.end()) + { + ans.push_back(v); + stt.insert(v); + } + + return; + } + + print(index+1,v,ans,nums,n); + + v.push_back(nums[index]); + print(index+1,v,ans,nums,n); + v.pop_back(); + } + + vector> subsetsWithDup(vector& nums) { + sort(nums.begin(),nums.end()); + vector> ans; + vector v; + int n = nums.size(); + print(0,v,ans,nums,n); + + return ans; + } +}; diff --git a/C++/min_insertion_to_make_string_palindrome.cpp b/C++/min_insertion_to_make_string_palindrome.cpp new file mode 100644 index 00000000..8382de70 --- /dev/null +++ b/C++/min_insertion_to_make_string_palindrome.cpp @@ -0,0 +1,62 @@ +// + + + +class Solution { +public: + int longestCommonSubsequence(string text1, string text2) { + int n1 = text1.size(); + int n2 = text2.size(); + // vector> dp(n1+1,vector(n2+1,0)); + vector prev(n2+1,0), cur(n2+1,0); + + for(int i=1;i<=n1;i++) + { + for(int j=1;j<=n2;j++) + { + if(text1[i-1] == text2[j-1]) + { + cur[j] = 1 + prev[j-1]; + } + + else + { + cur[j] = max(prev[j] , cur[j-1]); + } + } + prev = cur; + } + + return prev[n2]; + } + + string reverseStr(string str) + { + int n = str.length(); + + // Swap character starting from two + // corners + for (int i = 0; i < n / 2; i++) + swap(str[i], str[n - i - 1]); + + return str; + } + + int longestPalindromeSubseq(string s) { + string t = reverseStr(s); + return longestCommonSubsequence(s,t); + } + + int minInsertions(string s) { + int n = s.size(); + return n - longestPalindromeSubseq(s); + } +}; \ No newline at end of file