diff --git a/problem1.cpp b/problem1.cpp new file mode 100644 index 00000000..113769b9 --- /dev/null +++ b/problem1.cpp @@ -0,0 +1,29 @@ +// Time Complexity : O(n*2^n) +// Space Complexity : O(h) = O(n) +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + + +// Your code here along with comments explaining your approach +// Start at idx 0 and at every recursive call, add the path to the result +// Start the for loop from the pivot and add the element at idx to path and recurse +// After the recursive call, backtrack the last element added + +class Solution { +private: + void helper(vector& nums, int idx, vector& path, vector>& res) { + res.push_back(path); + for(int i=idx; i> subsets(vector& nums) { + vector path; + vector> res; + helper(nums, 0, path, res); + return res; + } +}; \ No newline at end of file diff --git a/problem2.cpp b/problem2.cpp new file mode 100644 index 00000000..0740f847 --- /dev/null +++ b/problem2.cpp @@ -0,0 +1,48 @@ +// Time Complexity : O(n*n*2^n) +// Space Complexity : O(n^2) +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + + +// Your code here along with comments explaining your approach +// start at index 0, break string into substrings at each index +// only if the substring is a palindrome, make a recursive call to process the rest of the string +// backtrack after the recursive call to explore different length substrings +// push the partitions into the result when we reach the end of the string + +class Solution { +private: + bool isPal(string str) { + int left = 0; + int right = str.length()-1; + while(left < right) { + if(str[left] != str[right]) { + return false; + } + left++; + right--; + } + return true; + } + void helper(string s, int idx, vector& part, vector>& res) { + if(idx == s.length()) { + res.push_back(part); + return; + } + for(int i=idx; i> partition(string s) { + vector part; + vector> res; + helper(s, 0, part, res); + return res; + } +}; \ No newline at end of file