LeetCode 131. 分割回文串 (搜索
原题链接
中等
作者:
JustDoIt11
,
2023-11-01 08:50:03
,
所有人可见
,
阅读 48
package leetcode;
import java.util.ArrayList;
import java.util.List;
public class lec131 {
List<List<String>> ans = new ArrayList<>();
String s;
public List<List<String>> partition(String _s) {
s = _s;
dfs(0, new ArrayList<>());
return ans;
}
public void dfs(int nowL, List<String> lis) {
if (nowL == s.length()) {
ans.add(lis);
return ;
}
for (int i = nowL + 1; i <= s.length(); i ++ ) {
List<String> temp = new ArrayList<>(lis);
String t = s.substring(nowL, i);
if (!check(t)) continue;
temp.add(t);
dfs(i, temp);
}
}
public boolean check(String t) {
StringBuilder sb = new StringBuilder(t);
sb.reverse();
return t.contentEquals(sb);
}
}