Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. private static List<List<string>> PalindromePartitioning(string str)
  2. {
  3. var list = new List<List<string>>();
  4.  
  5. if (str == string.Empty) return list;
  6.  
  7. Backtrack(list, new List<string>(), new List<string>() { str });
  8.  
  9. return list;
  10. }
  11.  
  12. private static void Backtrack(List<List<string>> list, List<string> temp, List<string> strs)
  13. {
  14. var next = new List<string>();
  15.  
  16. next.Print();
  17.  
  18. for (int i = 0; i < strs.Count; i++)
  19. {
  20. if (IsPalindrome(strs[i]))
  21. {
  22. temp.Add(strs[i]);
  23. }
  24.  
  25. int low = 0;
  26. int high = strs[i].Length - 1;
  27. int mid = low + (high - low) / 2;
  28.  
  29. if (high < low) break;
  30.  
  31. next.Add(strs[i].Substring(low, mid+1));
  32. if (mid + 1 <= high) next.Add(strs[i].Substring(mid + 1, high-mid));
  33. }
  34.  
  35. if (temp.Count != 0) list.Add(temp);
  36.  
  37. Backtrack(list, new List<string>(), next);
  38. }
  39.  
  40. private static bool IsPalindrome(string str)
  41. {
  42. int low = 0;
  43. int high = str.Length - 1;
  44.  
  45. while (high > low)
  46. {
  47. char front = str[low];
  48. char back = str[high];
  49.  
  50. if (front != back) return false;
  51.  
  52. low++;
  53. high--;
  54. }
  55.  
  56. return true;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement