The split palindrome string of the backtracking of the sixty-six force button brush questions

"Continue to create, accelerate growth! This is the 8th day of my participation in the "Nuggets Daily New Plan · June Update Challenge", click to view the details of the event "

foreword

Before, Xiao Liuliu always felt that his algorithm was relatively bad, and it was a shortcoming. In the past, it was really a three-day fishing, two sets of nets, and a few days of brushing, and then slowly stopped, so this Second, with the help of the platform's activities, I plan to start brushing slowly, and I will also summarize the brushing questions, talk about some of my own thinking, and my own ideas, etc. I hope it can be helpful to my friends. You can also take this opportunity to make up for your shortcomings. I hope you can stick to it.

topic

Given a string  s, please ssplit ** into some substrings so that each substring is a  palindrome  . Returns  s all possible split schemes.

A palindrome is a string  that reads the same forward and backward.

输入: s = "aab"
输出: [["a","a","b"],["aa","b"]]
复制代码

analyze

image.png

First of all, you can look at the tree we drew. When doing the retrospective problem, it is definitely recommended to draw an image of a tree. In fact, it is cutting, cutting layer by layer, and cutting the substrings, we will keep cutting, cutting When we reach a minimum unit and then cut to each layer, we judge whether the current is a palindrome. If so, we continue to cut down. If not, we end the cut.

Of course, one of the previous questions designed here is to give you a string to let you judge whether the current string is a palindrome.

backtracking template

void backtracking(参数) {
    if (终止条件) {
        存放结果;
        return;
    }

    for (选择:本层集合中元素(树中节点孩子的数量就是集合的大小)) {
        处理节点;
        backtracking(路径,选择列表); // 递归
        回溯,撤销处理结果
    }
}

复制代码

answer


package com.six.finger.leetcode.five;

import java.util.ArrayList;
import java.util.List;


/*
   给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。

回文串 是正着读和反着读都一样的字符串。

输入:s = "aab"
输出:[["a","a","b"],["aa","b"]]
 */


public class seventeen {


    public static void main(String[] args) {

        System.out.println(partition("aab"));
        System.out.println("bb");


    }


    public static List<List<String>> partition(String s) {

        List<List<String>> res = new ArrayList<>();

        List<String> path = new ArrayList<>();

        backtracking(res, path, s, 0);

        return res;


    }

    private static void backtracking(List<List<String>> res, List<String> path, String s, int index) {

        //结束条件
        if (index>=s.length()){
            res.add(new ArrayList<>(path));
            return;
        }

        //回溯部分
        for (int i=index;i<s.length();i++){
            //判断是不是回文数

            if (isHuiWen(s,index,i)){
                path.add(s.substring(index,i+1));
            }else {
                continue;
            }
            backtracking(res,path,s,i+1);
            path.remove(path.size()-1);

        }

    }

    //判断是否回文
    private static boolean isHuiWen(String s, int index, int i) {
        char[] chars = s.toCharArray();

        //其实这个是我们的双指针发,就是给一个头,一个尾,我们就给他遍历

        while (index<i){
            if (chars[index]!=chars[i]){
                return false;
            }
            index++;
            i--;
        }

        return true;


    }
}
复制代码

Let's face the template, let's talk about it

  • We create 2 sets, one to store the result and one to store our path,
  • The first is how to find my ending condition. I have been thinking about this for a long time before, and the last answer I read is actually our index. You think, where does it cut at most? Is it the length of >=s at most, so it is end condition
  • After the end condition, it is the first time to the horizontal length of the tree, which is what we call the breadth, that is, the direct for loop,
  • Then add the first character to the path, and then continue to traverse,
  • When it finally comes out, discard the last character, we can use remove.(s.length()-1) to achieve it

So let's see, in fact, the template of backtracking is definitely similar, only some parts are different, and most of them are the same, but in fact, it is not that simple. I still think it is a process that practice makes perfect.

Finish

Well, that's all for today's sharing. I'm Xiao Liuliu. I fished for three days and posted nets for two days!

Guess you like

Origin juejin.im/post/7104663567953035295