LeetCode-131 split palindrome string

Posted by depraved on Sun, 28 Nov 2021 03:09:39 +0100

Split palindrome string

Title Description: give you a string s. please divide s into some substrings so that each substring is a palindrome string. Returns s all possible segmentation schemes.

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

See LeetCode's official website for an example.

Source: LeetCode
Link: https://leetcode-cn.com/problems/palindrome-partitioning/
The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

Solution 1: recursive method

First, two special cases are handled. If the string is null, an empty result set is returned directly; If the length of the string is 1, there is only one segmentation case, which is returned directly.

When the length of the string is greater than 1, it is processed recursively, in which a method isHuiwen is used to judge whether the string is a palindrome string. The recursion process is as follows:

  • Judging from the first character of the string, the parameters include the palindrome string list that has been partitioned in the front, the current position and the substring to be judged;
  • First, judge if the last character of the string has been processed. If the current partition string is a palindrome string, add the current partition string to partitions, and then add it to the result set. Otherwise, return directly;
  • Otherwise, first judge whether the current partition string is a palindrome string. There are two possibilities:
    • If yes, add the current partition string to partitions, and start recursive judgment with the next character as a new partition string;
    • If not, add the next character to the current partition string and judge recursively.

Finally, the result set is returned.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class LeetCode_131 {
    // Result set
    private static List<List<String>> result = new ArrayList<>();

    public static List<List<String>> partition(String s) {
        // If the string is null, an empty result set is returned directly
        if (s == null) {
            return new ArrayList<>();
        }
        // If the string has only one character, there may only be one result, which is returned directly
        if (s.length() == 1) {
            List<String> partition = new ArrayList<>();
            partition.add(s);
            result.add(partition);
            return result;
        }

        partition(s, 0, new ArrayList<>(), s.substring(0, 1));
        return result;
    }

    /**
     * Recursive Method 
     *
     * @param s            Original string
     * @param pos          current location
     * @param partitions   Palindrome string that has been segmented before the current position
     * @param curPartition Current partition string
     */
    private static void partition(String s, int pos, List<String> partitions, String curPartition) {
        // The last character of the string has been processed
        if (pos == s.length() - 1) {
            if (isHuiwen(curPartition)) {
                // If the current partition string is a palindrome string, add the current partition string to partitions and then add it to the result set
                List<String> newPartitions = new ArrayList<>(Arrays.asList(new String[partitions.size()]));
                Collections.copy(newPartitions, partitions);
                newPartitions.add(curPartition);
                result.add(newPartitions);
            }
            return;
        }

        // If the current partition string is a palindrome string, add the current partition string to partitions, and then recursively judge the next character
        if (isHuiwen(curPartition)) {
            List<String> newPartitions = new ArrayList<>(Arrays.asList(new String[partitions.size()]));
            Collections.copy(newPartitions, partitions);
            newPartitions.add(curPartition);
            partition(s, pos + 1, newPartitions, s.substring(pos + 1, pos + 2));
        }

        // Recursively process the next string
        partition(s, pos + 1, partitions, curPartition + s.substring(pos + 1, pos + 2));
    }

    /**
     * Determine whether the string is a palindrome string
     *
     * @param str character string
     * @return
     */
    private static boolean isHuiwen(String str) {
        if (str == null || str.length() < 2) {
            return true;
        }
        for (int i = 0; i < str.length() / 2; i++) {
            if (str.charAt(i) != str.charAt(str.length() - 1 - i)) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        for (List<String> string : partition("aab")) {
            System.out.println(string);
        }
    }
}

[daily message] abandon the little ambition of swallows and birds, admire swans and soar high.

Topics: Java Algorithm leetcode string recursion