• [LeetCode#131]Palindrome Partitioning


    Problem:

    Given a string s, partition s such that every substring of the partition is a palindrome.

    Return all possible palindrome partitioning of s.

    For example, given s = "aab",
    Return

      [
        ["aa","b"],
        ["a","a","b"]
      ]

    Analysis:

    This problem is a perfect combination of dynamic programming and DFS search.
    It involves many skills, and it is also easy to make mistakes if some logic is not clear and robust enough.
    
    Basic idea:
    step 1: identify all palindrome sub strings in the original string.
    step 2: use DFS to search along the checkboard, and record all available pathes.
    
    ----------------------------------------------------------------------------------
    Step 1 : dynamic programming.
    The palindrome string has a very good characteristic to use dynamic programming. The substring(p[i+1, j-1]) of a palindrome string(p[i, j]) is also a palindrome string, which means the result of a substring is palindrome or not would be used for afterward checking.
    Key: since every substring s[i, j] could be a palidrome, and i, j is random, this is a typical two dimensional dynamic programming problem. 
    Transitional function (3 cases)
    case 1: i == j      "a"
    if (i == j) {
        check_board[i][j] = true;
    } 
    
    case 2: j - i == 1    "...aa..."
    if(j - i == 1) {
        check_board[i][j] = (s.charAt(i) == s.charAt(j));
    }
    
    case 3: j - i > 1     "...aba..."
    check_board[i][j] = ((s.charAt(i) == s.charAt(j)) && check_board[i+1][j-1]);
    
    Code implementation.
    It's very easy to make mistakes when calculating the checkboard.
    Wrong way: caculate the checkboard vertically rather than horizontally. 
    for (int i = 0; i < s.length(); i++) {
        for (int j = i; j < s.length(); j++) {
            if (i == j) {
                check_board[i][j] = true;
            } else if(j - i == 1) {
                check_board[i][j] = (s.charAt(i) == s.charAt(j));
            } else{
                check_board[i][j] = ((s.charAt(i) == s.charAt(j)) && check_board[i+1][j-1]);
            }
        }
    }
    * i is the row index, j is the column index
    ----*
    00  01  02  03
        11  12  13
            22  23
                33
    The pitfall in the above solution: when we want to calculate 03, we need 12. 
    However, since we calculate row by row(vertically), 12 has not been calcualted when we reach 03. (Big pitfall)
    
    
    
    Right solution, fill the checkboard horizontally rather than vertically.
    |    00  01  02  03
    |        11  12  13
    |            22  23
    *                33
    
    Right solution:
    for (int j = 0; j < s.length(); j++) {
        for (int i = 0; i <= j; i++) {
            if (i == j) {
                check_board[i][j] = true;
            } else if(j - i == 1) {
                check_board[i][j] = (s.charAt(i) == s.charAt(j));
            } else{
                check_board[i][j] = ((s.charAt(i) == s.charAt(j)) && check_board[i+1][j-1]);
            }
        }
    }
    Skill: i is the left side of a substring, the j is the right side. 
    
    
    Step 2: DFS to search and record right path
    The same idea as subset and permutation problem.
    When we reach helper, it means s[0, start-1] all the standard of palidrome string.
    Base case: 
    if (start == len) {
        ret.add(new ArrayList<String> (path));
        return;
    }
    
    Cut-off checking and backtracking
    for (int i = start; i < len; i++) {
        if (check_board[start][i]) {
            path.add(s.substring(start, i+1));
            record_path(i+1, s, len, check_board, path, ret);
            path.remove(path.size()-1);
        }
    }

    Solution:

    public class Solution {
        public List<List<String>> partition(String s) {
            List<List<String>> ret = new ArrayList<List<String>> ();
            if (s == null || s.length() == 0) {
                ret.add(new ArrayList<String> ());
                return ret;
            }
            boolean[][] check_board = new boolean[s.length()][s.length()];
            //note the direction to reach the answer
            //vertical rather than horizontal
            for (int j = 0; j < s.length(); j++) {
                for (int i = 0; i <= j; i++) {
                    if (i == j) {
                        check_board[i][j] = true;
                    } else if(j - i == 1) {
                        check_board[i][j] = (s.charAt(i) == s.charAt(j));
                    } else{
                        check_board[i][j] = ((s.charAt(i) == s.charAt(j)) && check_board[i+1][j-1]);
                    }
                }
            }
            ArrayList<String> path = new ArrayList<String> ();
            record_path(0, s, s.length(), check_board, path, ret);
            return ret;
        }
        
        private void record_path(int start, String s, int len, boolean[][] check_board, ArrayList<String>path, List<List<String>>ret) {
            if (start == len) {
                ret.add(new ArrayList<String> (path));
                return;
            }
            for (int i = start; i < len; i++) {
                if (check_board[start][i]) {
                    path.add(s.substring(start, i+1));
                    record_path(i+1, s, len, check_board, path, ret);
                    path.remove(path.size()-1);
                }
            }
        }
    }
  • 相关阅读:
    yii AR 模式操作
    sql 注入命令大全
    PHP 防xss攻击
    yii rbac管理
    yii2.0 表单小部件常用的默认选中
    yii 表单小部件使用
    多个API接口
    iwebshop 增删改查
    搜索引擎接口
    2003终端服务器授权,120天试用期限制
  • 原文地址:https://www.cnblogs.com/airwindow/p/4751760.html
Copyright © 2020-2023  润新知