Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static List<List<string>> PalindromePartitioning(string str)
- {
- var list = new List<List<string>>();
- if (str == string.Empty) return list;
- Backtrack(list, new List<string>(), new List<string>() { str });
- return list;
- }
- private static void Backtrack(List<List<string>> list, List<string> temp, List<string> strs)
- {
- var next = new List<string>();
- next.Print();
- for (int i = 0; i < strs.Count; i++)
- {
- if (IsPalindrome(strs[i]))
- {
- temp.Add(strs[i]);
- }
- int low = 0;
- int high = strs[i].Length - 1;
- int mid = low + (high - low) / 2;
- if (high < low) break;
- next.Add(strs[i].Substring(low, mid+1));
- if (mid + 1 <= high) next.Add(strs[i].Substring(mid + 1, high-mid));
- }
- if (temp.Count != 0) list.Add(temp);
- Backtrack(list, new List<string>(), next);
- }
- private static bool IsPalindrome(string str)
- {
- int low = 0;
- int high = str.Length - 1;
- while (high > low)
- {
- char front = str[low];
- char back = str[high];
- if (front != back) return false;
- low++;
- high--;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement