Advertisement
rishu110067

Untitled

Jan 27th, 2022
809
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.     public static ArrayList<String> generate_palindromic_decompositions(String s) {
  3.         ArrayList<String> result = new ArrayList<String>();
  4.         helper(s, 0, new StringBuilder(), result);
  5.         return result;
  6.     }
  7.        
  8.     private static void helper(String s, int idx, StringBuilder slate, ArrayList<String> result) {
  9.         if (idx == s.length()) {
  10.             result.add(new StringBuilder(slate.substring(0, slate.length()-1)).toString());
  11.             return;
  12.         }
  13.        
  14.         for (int i = idx; i < s.length(); i++) {
  15.             if (isPal(s, idx, i)) {
  16.                 StringBuilder new_slate = new StringBuilder(slate);
  17.                 new_slate.append(s.substring(idx, i + 1) + "|");
  18.                 helper(s, i + 1, new_slate, result);
  19.             }
  20.         }
  21.     }
  22.    
  23.     private static boolean isPal(String s, int idx, int i) {
  24.         boolean result = true;
  25.         while (idx < i) {
  26.            if (s.charAt(idx) != s.charAt(i)) {
  27.                result =false;
  28.                break;
  29.            }
  30.            idx++;
  31.            i--;
  32.         }
  33.         return result;
  34.     }
  35.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement