subject
Given a string s, s is divided into several substrings so that each substring is a palindrome string.
Returns all possible splitting schemes of s.
Example:
Input: "aab" Output: [ ["aa","b"], ["a","a","b"] ]
Title Solution
No official questions
After seeing this, it's easy to think of the Violence Act: Recursively retrieve the text string, then find the last one and it's done. Push a String-compliant List into the last returned List, and then retrieve all String List s by analogy. The computational complexity is O (n3) O (n ^ 3) O (n3), and this method can be optimized.
It's obviously possible for you to decide whether you can optimize the palindrome string by horse-drawn carriage. The carriage obtains the auxiliary array p[] (complexity O(n)O(n)O(n)O(n)), then constructs a palindrome one-way transition graph between''(only from left to right) (complexity O(n2)O(n^2)O(n2)), and traverses the palindrome one-way transition graph (complexity O(n2)O(n2) by dfs to get all the required string lists.
But if this is achieved, I won't go to dfs... Java doesn't have global variables, and I'm not very good at Java data structure implementation myself, so I put it aside and look at the big guys in the comment area.
Then you find one. The Big Boy Solved by the Method of Return and Retrospect (6 ms, over 92%, the complexity should also be O (n2) O (n ^ 2) O (n2)?)
//Execution time: 6 ms, defeating 92.79% of users in Palindrome Partitioning's Java submission //Memory consumption: 50.5 MB, beating 39.73% of Palindrome Partitioning's Java submissions class Solution { int len; ArrayList<List<String>> res = new ArrayList<>(); String s; boolean[][] dp; public List<List<String>> partition(String s) { this.s = s; len = s.length(); if (len < 1) return res; // dp[i][j] denotes a substring, s.substring(i, j + 1) // For example, s= "babad", DP [0] [0]= "b", DP [0] [4]= "babad" dp = new boolean[len][len]; // one character // Oblique traversal [0,0] - > [1,1] - >... // Each character is palindrome for (int i = 0; i < len; i++) { dp[i][i] = true; } // two character // Oblique traversal [0,1] - > [1,2] - >... // Palindrome is when both characters are identical for (int i = 0; i < len - 1; i++) { dp[i][i + 1] = s.charAt(i) == s.charAt(i + 1); } // others // Begin dp, this substring = character + substring in the lower left corner + character // A palindrome is a palindrome only if the lower left corner is palindrome and the characters added at both ends are the same. for (int i = 2; i < len; i++) { for (int j = 0; j < len - i; j++) { dp[j][j + i] = dp[j + 1][j + i - 1] && s.charAt(j) == s.charAt(j + i); } } //Backtracking foo(new LinkedList<>(),0); return res; } void foo(LinkedList<String> path, int level) { if (level >= len) { res.add(new ArrayList<>(path)); return; } for (int i = level; i < len; i++) { if (dp[level][i]) { path.add(s.substring(level, i + 1)); foo(path, i + 1); path.removeLast(); } } } }
What's your feeling?
I don't know where I'm going to be after my idea is realized. The horse-drawn carriage should be part of the palindrome decision that the DP simplifies. Hopefully, it's not much worse than Big Boy's 6ms. I will have the chance to come back and try it later. Anyway, other comment area answers - those recursive backtracking or dfs backtracking are basically 16ms. It's still dp.