131. 分割回文串
难度中等1350收藏分享切换为英文接收动态反馈
给你一个字符串 s
,请你将 s
分割成一些子串,使每个子串都是 回文串 。返回 s
所有可能的分割方案。
回文串 是正着读和反着读都一样的字符串。
示例 1:
输入:s = "aab"
输出:[["a","a","b"],["aa","b"]]
示例 2:
输入:s = "a"
输出:[["a"]]
提示:
1 <= s.length <= 16
s
仅由小写英文字母组成
通过次数256,230
提交次数349,272
func partition(s string) [][]string {
res,path=make([][]string,0),make([]string,0)
dfs(s,0)
return res
}
var(
res [][]string
path []string
)
func dfs(s string,startindex int){
if startindex==len(s){
tmp:=make([]string,len(path))
copy(tmp,path)
res=append(res,tmp)
}
for i:=startindex;i<len(s);i++{
if isPalindrome(s,startindex,i) {
path=append(path,s[startindex:i+1])
}else{
continue
}
dfs(s,i+1)
path=path[:len(path)-1]
}
}
func isPalindrome(s string ,startindex int,endindex int) bool{
for i,j:=startindex,endindex;i<j;i,j = i+1,j-1{
if s[i]!=s[j]{
return false
}
}
return true
}
标签:分割,string,res,131,path,回文
From: https://www.cnblogs.com/suehoo/p/17043455.html