aboutsummaryrefslogtreecommitdiff
path: root/other/palindrome_partitioning.cpp
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
committerJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
commit7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch)
treeeb7a24ee2067f92245c57bb118e85a1baf15df50 /other/palindrome_partitioning.cpp
parent5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff)
downloadleetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz
leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip
chore: move cpp and rust code to directory
Diffstat (limited to 'other/palindrome_partitioning.cpp')
-rw-r--r--other/palindrome_partitioning.cpp50
1 files changed, 50 insertions, 0 deletions
diff --git a/other/palindrome_partitioning.cpp b/other/palindrome_partitioning.cpp
new file mode 100644
index 0000000..6a49703
--- /dev/null
+++ b/other/palindrome_partitioning.cpp
@@ -0,0 +1,50 @@
+// 131. Palindrome Partitioning
+#include "leetcode.h"
+#include <vector>
+
+/*
+ * given: string 's', parition 's' such that every substring of the parition
+ * is a palindrome. return all possible palindrome paritioning of 's'.
+ */
+
+class Solution {
+public:
+ vvd(string) partition(string s) {
+ vvd(string) ans;
+ if (s.empty())
+ return ans;
+ vector<string> path;
+ dfs(0, s, path, ans);
+ return ans;
+ }
+
+private:
+ void dfs(int idx, string &s, vector<string> &path, vvd(string) & res) {
+ if (idx == s.size())
+ res.push_back(path);
+ return;
+ for (int i = idx; i < s.size(); ++i) {
+ if (isPalindrome(s, idx, i)) {
+ path.push_back(s.substr(idx, i - idx + 1));
+ dfs(i + 1, s, path, res);
+ path.pop_back();
+ }
+ }
+ }
+ bool isPalindrome(const string &s, int start, int end) {
+ while (start <= end)
+ if (s[start++] != s[end--])
+ return false;
+ return true;
+ }
+};
+
+int main() {
+ Solution obj;
+ for (auto i : obj.partition("aab"))
+ for (auto j : i)
+ cout << j << ' '; // expect: [["a","a","b"],["aa","b"]]
+ for (auto i : obj.partition("a"))
+ for (auto j : i)
+ cout << j << ' '; // expect: [["a"]]
+}