【字符串】单词替换

题目描述

给定一个数组:dictionary[]、一个句子:sentence;如果sentence的单词词根是:dictionary;那么使用dictionary中最短的词根进行替换。

示例1:

输入:

dictionary = ["cat","bat","rat"], sentence = "the cattle was rattled by the battery"

输出:"the cat was rat by the bat"

解题思路

1. 对sentence做遍历操作,如果遇到空格或者已经结束,则获取一个单词。

2. 对这个单词和dictionary中的词根做遍历,选择最小长度的词根,替换当前单词。

3. 构造完成新的句子后,返回该句子。

代码实现


import java.util.Arrays;
import java.util.List;

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

        StringBuilder res = new StringBuilder();

        for (int i = 0; i < sentence.length(); ) {
            int start = i;
            StringBuilder sb = new StringBuilder();
            while (start < sentence.length() && sentence.charAt(start) != ' ') {
                sb.append(sentence.charAt(start));
                start++;
            }

            String s = sb.toString();
            int minLength = s.length();
            for (String d : dictionary) {
                if (s.startsWith(d) && d.length() < minLength) {
                    s = d;
                    minLength = s.length();
                }
            }
            res.append(s);

            i = start;
            if (i >= sentence.length()) {
                break;
            }
            if (sentence.charAt(i) == ' ') {
                res.append(' ');
                i++;
            }
        }
        return res.toString();
    }

    public static void main(String[] args) {

        Solution solution = new Solution();

        System.out.println(solution.replaceWords(Arrays.asList("cat", "bat", "rat"), "the cattle was rattled by the battery"));
    }
}

总结

这道题也可以采用HashSet来做比较,新的代码执行效率和老代码执行效率相似,其他高效率方法欢迎给出思路;HashSet的实现代码如下:


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

        StringBuilder res = new StringBuilder();
        Set<String> set = new HashSet<>(dictionary);

        for (int i = 0; i < sentence.length(); ) {
            int start = i;
            boolean find = false;
            StringBuilder sb = new StringBuilder();
            while (start < sentence.length() && sentence.charAt(start) != ' ') {
                sb.append(sentence.charAt(start));

                if(!find && set.contains(sb.toString())) {
                    res.append(sb);
                    find = true;
                }
                start++;
            }

            if(!find) {
                res.append(sb);
            }


            i = start;
            if (i >= sentence.length()) {
                break;
            }
            if (sentence.charAt(i) == ' ') {
                res.append(' ');
                i++;
            }
        }
        return res.toString();
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值