diff --git a/palindrome_partitioning.cpp b/palindrome_partitioning.cpp new file mode 100644 index 00000000..45314a8b --- /dev/null +++ b/palindrome_partitioning.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + vector> answer; + void backtrack(vector temp, int start, string s) { + + if (start >= s.length()) { + answer.push_back(temp); + } + + auto isPalindrome = [=] (string substring) -> bool { + int start = 0; + int end = substring.length()-1; + while (start < end) { + if (substring[start++] != substring[end--]) { + return false; + } + } + return true; + }; + + for (int i=start;i creates substring from index+1 of size length + if (isPalindrome(str)) { + temp.push_back(str); + backtrack(temp, i+1, s); + temp.pop_back(); + } + } + } + vector> partition(string s) { + vector temp; + backtrack(temp, 0, s); + return answer; + } +}; diff --git a/subset.cpp b/subset.cpp new file mode 100644 index 00000000..90e95937 --- /dev/null +++ b/subset.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + vector> answer; + void backtrack(vector temp, vector& nums, int index) { + answer.push_back(temp); + + if (index == nums.size()) { + return; + } + + for (int i=index;i> subsets(vector& nums) { + vector temp; + backtrack(temp, nums, 0); + return answer; + } +};