【LeetCode】Palindrome Partitioning

133 篇文章 0 订阅
121 篇文章 2 订阅
Palindrome Partitioning 
Total Accepted: 4268 Total Submissions: 17532 My Submissions
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"]
]
Discuss
这种题目应该就是搜索结果了,因为不考虑重复的,所以BFS和DFS都可以解决。
BFS比DFS慢了将近200ms。

Java DFS AC

public class Solution {
    public ArrayList<ArrayList<String>> partition(String s) {
        ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
        if(s == null || "".equals(s.trim())){
            return list;
        }
        ArrayList<String> tempList = new ArrayList<String>();
        dfs(list, tempList, s);
        return list;
    }
    public void dfs(ArrayList<ArrayList<String>> list, 
					ArrayList<String> tempList, String s ){
        if(s.length() == 0){
            list.add(new ArrayList<String>(tempList));
        }
        int len = s.length();
        for(int i = 1; i <= len; i++){
            String tempStr = s.substring(0,i);
            if(isPalindromic(tempStr)){
                tempList.add(tempStr);
                dfs(list,tempList,s.substring(i));
                tempList.remove(tempList.size()-1);
            }
        }
    }
    public boolean isPalindromic(String s){
        int len = s.length();
        int start = 0;
        int end = len-1;
        while(start <= end && start < len && end >= 0 ){
            if(s.charAt(start) != s.charAt(end)){
                return false;
            }
            start++;
            end--;
        }
        return true;
    }
}

Java BFS AC

public class Solution {
    public ArrayList<ArrayList<String>> partition(String s) {
        ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
        if(s == null || "".equals(s.trim())){
            return list;
        }
        bfs(list,s);
        return list;
    }
    public void bfs(ArrayList<ArrayList<String>> list, String s){
        Queue<Node> queue = new LinkedList<Node>();
        queue.offer(new Node(new StringBuffer(),s));
        while(!queue.isEmpty()){
            Node node = queue.peek();
            queue.poll();
            String news = node.s;
            int len = news.length();
            if(len == 0){
                StringBuffer sb = new StringBuffer(node.sb);
                String tempStr = sb.toString().trim();
                if(tempStr.replace(" ","").equals(s)){
                    String msg[] = tempStr.split(" ");
                    ArrayList<String> tempList = new ArrayList<String>();
                    for(int i = 0; i < msg.length; i++){
                        tempList.add(msg[i]);
                    }
                    list.add(tempList);
                }
            }
            for(int i = 1; i <= len; i++){
                String tempStr = news.substring(0,i);
                StringBuffer newsb = new StringBuffer(node.sb);
                if(isPalindromic(tempStr)){
                    newsb.append(tempStr+" ");
                    queue.offer(new Node(newsb,news.substring(i)));
                }
            }
        }
    }
    public boolean isPalindromic(String s){
        int len = s.length();
        int start = 0;
        int end = len-1;
        while(start <= end && start < len && end >= 0 ){
            if(s.charAt(start) != s.charAt(end)){
                return false;
            }
            start++;
            end--;
        }
        return true;
    }
    public class Node{
        StringBuffer sb;
        String s;
        Node(StringBuffer sb, String s){
            this.sb = sb;
            this.s = s;
        }
    }
}
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值