Leetcode_648 词根替换单词

题目描述

Leetcode_648 词根替换单词_第1张图片

题解

java

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

        Set<String> set = new HashSet<>(dict);
        StringBuilder sb = new StringBuilder();
        String[] word = sentence.split(" ");
        for(int i = 0;ifor (int j = 1; j <= word[i].length(); j++) {
                if (set.contains(word[i].substring(0, j)))
                    word[i] = word[i].substring(0, j);
            }
            sb.append(word[i] +" ");
        }
        return sb.substring(0,sb.length()-1);

    }

你可能感兴趣的:(编码,Leetcode)