2020-08-22

Leetcode 648  单词替换

地址: https://leetcode-cn.com/problems/replace-words/

代码:

/**
 * @Author linchen.wcy
 */
class Solution {
    class TreeNode {
        char data;
        List<TreeNode> child;
        TreeNode parent;
        boolean isWord;

        public TreeNode() {
            this.child = new ArrayList<>();
            this.data='#';
        }
    }

    public TreeNode build(List<String> dict) {
        TreeNode root = new TreeNode();
        for (int i = 0; i < dict.size(); i++) {
            insertOnebyOne(dict.get(i), root);
        }

        return root;
    }

    private void insertOnebyOne(String s, TreeNode root) {
        int pos = 0;
        // 找共性
        while (pos < s.length()) {
            int pre = pos;
            for (int i = 0; i < root.child.size()&&pos<s.length(); i++) {
                if (root.child.get(i).data == s.charAt(pos)) {

                    root = root.child.get(i);
                    if(pos == s.length()-1){
                        root.isWord=true;
                    }
                    pos++;
                }
            }
            if (pre == pos) break;
        }
        // 共性如果包含不全,则添加节点
        while (pos < s.length()) {

            TreeNode temp = new TreeNode();
            temp.data = s.charAt(pos);
            pos++;
            if (pos == s.length()) temp.isWord = true;
            temp.parent = root;
            root.child.add(temp);
            root = temp;

        }


    }

    public String calc(TreeNode root, String sentence) {
        StringBuilder sb = new StringBuilder();
        String[] words = sentence.split(" ");
        for (int i = 0; i < words.length; i++) {
            words[i] = replaceWords0(root, words[i]);
            sb.append(words[i] + " ");
        }
        return sb.substring(0, sb.length() - 1);
    }

    // bfs is faster!
    public String replaceWords0(TreeNode root, String word) {

        TreeNode current = root;
        LinkedList<TreeNode> queue = new LinkedList<>();
        for (int i = 0; i < root.child.size(); i++) {
            if (root.child.get(i).data == word.charAt(0)) {
                queue.add(root.child.get(i));
                break;
            }
        }
        if (queue.isEmpty()) return word;
        int pos = 1;
        while (!queue.isEmpty() && pos <word.length()) {
            TreeNode first = queue.poll();

            if (first.isWord) {
                StringBuilder sb = new StringBuilder();
                populate(sb, first);
                return sb.toString();
            } else {
                for (int i = 0; i < first.child.size(); i++) {
                    {
                        if (first.child.get(i).data == word.charAt(pos)) {
                            queue.add(first.child.get(i));
                            pos++;
                            break;
                        }
                    }
                }
            }

        }
        return word;

    }

    private void populate(StringBuilder sb, TreeNode first) {

        while (first.data != '#') {
            sb.insert(0,first.data);
            first=first.parent;

        }


    }

    public String replaceWords(List<String> dictionary, String sentence) {

        TreeNode root = build(dictionary);
        return calc(root, sentence);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值